lucatume / wp-browser

The easy and reliable way to test WordPress with Codeception. 10 years of proven success.
https://wpbrowser.wptestkit.dev/
MIT License
603 stars 86 forks source link

Using wp-browser within docker #140

Closed indietravel closed 6 years ago

indietravel commented 6 years ago

Hi,

I'm using the 10up local docker with everything as default.

I can install Codeception and wp-browser using composer following the QuickStart instructions and successfully use init wp-browser to scaffold things. The problem lies with running a basic acceptance test to look at the homepage.

In my browser, outside of docker, I access the homepage at localhost. Within docker, a curl of that fails.

root@5970229fa5b9:/var/www/html# curl localhost
curl: (7) Failed to connect to localhost port 80: Connection refused

Within docker, I can see the homepage by using curl nginx.

My acceptance.suite.yml file reads:

# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WPWebDriver or WPBrowser.
# Use WPDb to set up your initial database fixture.
# If you need both WPWebDriver and WPBrowser tests - create a separate suite.

actor: AcceptanceTester
modules:
    enabled:
        - WPDb
        - WPBrowser
        - \Helper\Acceptance
    config:
        WPDb:
            dsn: 'mysql:host=mysql;dbname=wordpress'
            user: 'wordpress'
            password: 'password'
            url: 'localhost'
            tablePrefix: 'wp_'
        WPBrowser:
            url: 'localhost'
            adminUsername: 'test'
            adminPassword: 'test'
            adminPath: '/wp-admin'

But php vendor/codeception/codeception/codecept run acceptance returns:

root@5970229fa5b9:/var/www/html# php vendor/codeception/codeception/codecept run acceptance
Codeception PHP Testing Framework v2.3.8
Powered by PHPUnit 6.5.6 by Sebastian Bergmann and contributors.

Acceptance Tests (2) ----------------------------------------------------------------------------------------------------------------------------------------------------------------
E WPFirstCest: Try to see home (0.11s)
E WPFirstCest: Try to test (0.01s)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 2.21 seconds, Memory: 12.00MB

There were 2 errors:

---------
1) WPFirstCest: Try to see home
 Test  tests/acceptance/WPFirstCest.php:tryToSeeHome

  [GuzzleHttp\Exception\ConnectException] cURL error 7: Failed to connect to localhost port 80: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)  

Scenario Steps:

 1. $I->amOnPage("/") at tests/acceptance/WPFirstCest.php:16

#1  /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186
#2  /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:150
#3  /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:103
#4  /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php:43
#5  /var/www/html/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php:37
#6  /var/www/html/vendor/guzzlehttp/guzzle/src/Middleware.php:36
#7  /var/www/html/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php:54
#8  /var/www/html/vendor/guzzlehttp/guzzle/src/Middleware.php:59
#9  /var/www/html/vendor/guzzlehttp/guzzle/src/HandlerStack.php:67
#10 /var/www/html/vendor/guzzlehttp/guzzle/src/Client.php:277

(and the same for test 2). if I switch localhostfor nginx, I get

---------
1) WPFirstCest: Try to see home
 Test  tests/acceptance/WPFirstCest.php:tryToSeeHome
 Step  See "This is your first post. Edit or delete it, then start writing!"
 Fail  Failed asserting that  on page nginx/
-->  400 Bad Request 400 Bad Request nginx/1.13.8 
--> contains "this is your first post. edit or delete it, then start writing!".

I obviously need to do something to allow codeception / wp-browser to see the browser, but I'm stuck as to what that is. Any help appreciated.

lucatume commented 6 years ago

Thanks for reporting the issue, I've still not looked into this but am very curious to do. Did you make any progress or found out something new?

indietravel commented 6 years ago

Sadly, no progress. I can run basic tests from outside Docker, looking at the front end of localhost, but I cannot run them from inside docker to do proper tests. On Thu, 1 Mar 2018 at 21:39, theAverageDev (Luca Tumedei) < notifications@github.com> wrote:

Thanks for reporting the issue, I've still not looked into this but am very curious to do. Did you make any progress or found out something new?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lucatume/wp-browser/issues/140#issuecomment-369516254, or mute the thread https://github.com/notifications/unsubscribe-auth/AByOfvq3GwAXdSxvMl9Ig5VXhv84OrDfks5tZ7PAgaJpZM4SKM99 .

-- Craig Martin Owner/Director, Performance Foundry http://performancefoundry.com

Owner, Indie Travel Media http://indietravelmedia.com

lucatume commented 6 years ago

Ok, thanks for the update anyway. I will take some time, in the weekend, to install your setup and run some test and get back to you. Did you do any relevant modification to the local-docker setup I should be aware of?

lucatume commented 6 years ago

Ok, I've installed the setup and spun it up.

I've run all the commands below from within the php_fpm container of the stack:

  1. I've required lucatume/wp-browser:^2.0 and vlucas/phpdotenv as dev dependencies
  2. I've created the tests database that I will use to run the wpunit tests; mysql -u root -p -h mysql -e "create database if not exists tests;", root password is password (from the docker-compose.yml file)
  3. I've bootstrapped wp-browser with vendor/bin/codecept init wpbrowser; relevant answers are http://nginx as URL and /var/ww/html as WP root folder
  4. I've modified the bootstrapped suites configuration files to use the .env based configuration; to do so I've added this line in the codeception.yml file:
    params:
      - .env
  5. I've modified all the suites configuration files to use the env placeholders (see below).

This is my .env file to run the tests from within the Docker php_fpm container:

ROOT_FOLDER="/var/www/html"
URL="http://nginx"
DOMAIN="nginx"
DB_HOST="mysql"
DB_NAME="wordpress"
DB_USER="root"
DB_PASSWORD="password"
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="admin"
TEST_DB_NAME="tests"

The codeception.yml file:

paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
    commands:
        - Codeception\Command\GenerateWPUnit
        - Codeception\Command\GenerateWPRestApi
        - Codeception\Command\GenerateWPRestController
        - Codeception\Command\GenerateWPRestPostTypeController
        - Codeception\Command\GenerateWPAjax
        - Codeception\Command\GenerateWPCanonical
        - Codeception\Command\GenerateWPXMLRPC
        - Codeception\Command\DbSnapshot
        - tad\Codeception\Command\SearchReplace
params:
  - .env

And this is the tests/acceptance.suite.yml file:

# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WPWebDriver or WPBrowser.
# Use WPDb to set up your initial database fixture.
# If you need both WPWebDriver and WPBrowser tests - create a separate suite.

actor: AcceptanceTester
modules:
    enabled:
        - WPDb
        - WPBrowser
        - \Helper\Acceptance
    config:
        WPDb:
            dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%'
            user: '%DB_USER%'
            password: '%DB_PASSWORD%'
            dump: 'tests/_data/dump.sql'
            populate: true #import the dump before the tests
            cleanup: true #import the dump between tests
            url: '%URL%'
            urlReplacement: true #replace the hardcoded dump URL with the one above
            tablePrefix: 'wp_'
        WPBrowser:
            url: '%URL%'
            adminUsername: '%ADMIN_USERNAME%'
            adminPassword: '%ADMIN_PASSWORD%'
            adminPath: '/wp-admin'

The tests/functional.suite.yml file:

# Codeception Test Suite Configuration
#
# Suite for functional tests
# Emulate web requests and make WordPress process them

actor: FunctionalTester
modules:
    enabled:
        - WPDb
        - WordPress
        - Asserts
        - \Helper\Functional
    config:
        WPDb:
            dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%'
            user: '%DB_USER%'
            password: '%DB_PASSWORD%'
            dump: 'tests/_data/dump.sql'
            populate: true
            cleanup: true
            url: '%URL%'
            urlReplacement: true
            tablePrefix: 'wp_'
        WordPress:
            depends: WPDb
            wpRootFolder: '%ROOT_FOLDER%'
            adminUsername: '%ADMIN_USERNAME%'
            adminPassword: '%ADMIN_PASSWORD%'
            adminPath: '/wp-admin'

The tests/wpunit.suite.yml file:

# Codeception Test Suite Configuration
#
# Suite for unit or integration tests that require WordPress functions and classes.

actor: WpunitTester
modules:
    enabled:
        - WPLoader
        - \Helper\Wpunit
    config:
        WPLoader:
            wpRootFolder: "%ROOT_FOLDER%"
            dbName: "%TEST_DB_NAME%"
            dbHost: "%DB_HOST%"
            dbUser: "%DB_USER%"
            dbPassword: "%DB_PASSWORD%"
            tablePrefix: "wp_"
            domain: "%DOMAIN%"
            adminEmail: "amdin@wplocal.local"
            title: "Test"        
            plugins: ['test-plugin/plugin.php']
            activatePlugins: ['test-plugin/plugin.php']

I had no issues running this test:

<?php
class WPFirstCest {

    /**
     * It should be able to reach the homepage
     *
     * @test
     */
    public function should_be_able_to_reach_the_homepage( AcceptanceTester $I ) {
        $I->havePostInDatabase(['post_title' => 'A test post']);
        $I->amOnPage( '/' );
        $I->see('A test post');
    }
}

Let me know if this helps you.

indietravel commented 6 years ago

Unfortunately a busted arm has stopped me from coming back to this in a timely manner. I’ll feed back as soon as I can. On Sun, 4 Mar 2018 at 03:53, theAverageDev (Luca Tumedei) < notifications@github.com> wrote:

Ok, I've installed the setup and spun it up.

I've run all the commands below from within the php_fpm container of the stack:

  1. I've required lucatume/wp-browser:^2.0 and vlucas/phpdotenv as dev dependencies

  2. I've created the tests databasethat I will use to run thewpunit tests;mysql -u root -p -h mysql -e "create database if not exists tests;", root password ispassword(from thedocker-compose.yml` file)

  3. I've bootstrapped wp-browser with vendor/bin/codecept init wpbrowser; relevant answers are http://nginx as URL and /var/ww/html as WP root folder

  4. I've modified the bootstrapped suites configuration files to use the .env based configuration; to do so I've added this line in the codeception.yml file:

    params:

    • .env
  5. I've modified all the suites configuration files to use the env placeholders (see below).

This is my .env file to run the tests from within the Docker php_fpm container:

ROOT_FOLDER="/var/www/html" URL="http://nginx" DOMAIN="nginx" DB_HOST="mysql" DB_NAME="wordpress" DB_USER="root" DB_PASSWORD="password" ADMIN_USERNAME="admin" ADMIN_PASSWORD="admin" TEST_DB_NAME="tests"

The codeception.yml file:

paths: tests: tests output: tests/_output data: tests/_data support: tests/_support envs: tests/_envsactor_suffix: Testerextensions: enabled:

  • Codeception\Extension\RunFailed commands:
  • Codeception\Command\GenerateWPUnit
  • Codeception\Command\GenerateWPRestApi
  • Codeception\Command\GenerateWPRestController
  • Codeception\Command\GenerateWPRestPostTypeController
  • Codeception\Command\GenerateWPAjax
  • Codeception\Command\GenerateWPCanonical
  • Codeception\Command\GenerateWPXMLRPC
  • Codeception\Command\DbSnapshot
  • tad\Codeception\Command\SearchReplaceparams:
    • .env

And this is the tests/acceptance.suite.yml file:

Codeception Test Suite Configuration## Suite for acceptance tests.# Perform tests in browser using the WPWebDriver or WPBrowser.# Use WPDb to set up your initial database fixture.# If you need both WPWebDriver and WPBrowser tests - create a separate suite.

actor: AcceptanceTestermodules: enabled:

  • WPDb
  • WPBrowser
  • \Helper\Acceptance config: WPDb: dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%' user: '%DB_USER%' password: '%DB_PASSWORD%' dump: 'tests/data/dump.sql' populate: true #import the dump before the tests cleanup: true #import the dump between tests url: '%URL%' urlReplacement: true #replace the hardcoded dump URL with the one above tablePrefix: 'wp' WPBrowser: url: '%URL%' adminUsername: '%ADMIN_USERNAME%' adminPassword: '%ADMIN_PASSWORD%' adminPath: '/wp-admin'

The tests/functional.suite.yml file:

Codeception Test Suite Configuration## Suite for functional tests# Emulate web requests and make WordPress process them

actor: FunctionalTestermodules: enabled:

  • WPDb
  • WordPress
  • Asserts
  • \Helper\Functional config: WPDb: dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%' user: '%DB_USER%' password: '%DB_PASSWORD%' dump: 'tests/data/dump.sql' populate: true cleanup: true url: '%URL%' urlReplacement: true tablePrefix: 'wp' WordPress: depends: WPDb wpRootFolder: '%ROOT_FOLDER%' adminUsername: '%ADMIN_USERNAME%' adminPassword: '%ADMIN_PASSWORD%' adminPath: '/wp-admin'

The tests/wpunit.suite.yml file:

Codeception Test Suite Configuration## Suite for unit or integration tests that require WordPress functions and classes.

actor: WpunitTestermodules: enabled:

  • WPLoader
  • \Helper\Wpunit config: WPLoader: wpRootFolder: "%ROOT_FOLDER%" dbName: "%TEST_DB_NAME%" dbHost: "%DB_HOST%" dbUser: "%DB_USER%" dbPassword: "%DBPASSWORD%" tablePrefix: "wp" domain: "%DOMAIN%" adminEmail: "amdin@wplocal.local" title: "Test" plugins: ['test-plugin/plugin.php'] activatePlugins: ['test-plugin/plugin.php']

I had no issues running this test:

<?phpclass WPFirstCest { /* It should be able to reach the homepage @test */ public function should_be_able_to_reach_the_homepage( AcceptanceTester $I ) { $I->havePostInDatabase(['post_title' => 'A test post']); $I->amOnPage( '/' ); $I->see('A test post'); }}

Let me know if this helps you.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lucatume/wp-browser/issues/140#issuecomment-370153037, or mute the thread https://github.com/notifications/unsubscribe-auth/AByOfvoxQi33fG10RFEJZmXIoD44TImRks5taq5VgaJpZM4SKM99 .

-- Craig Martin Owner/Director, Performance Foundry http://performancefoundry.com

Owner, Indie Travel Media http://indietravelmedia.com

lucatume commented 6 years ago

Any update on this one?