From 52bd9e72be6d2e03ef8470d3d6f3f864e2b8c65c Mon Sep 17 00:00:00 2001 From: miya0001 Date: Sun, 25 Dec 2016 02:24:03 +0900 Subject: [PATCH] add support multiple session --- .travis.yml | 2 +- README.md | 17 +- behat.yml.dist | 3 + composer.json | 3 +- composer.lock | 277 +++++++++++++++++++++++++++- features/http-response.feature | 2 + src/Context/RawWordPressContext.php | 68 +++---- src/Context/WordPressContext.php | 6 +- 8 files changed, 329 insertions(+), 49 deletions(-) diff --git a/.travis.yml b/.travis.yml index ac7ddd5..57e9082 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: php php: -# - '7.1' +- '7.1' - '5.6' env: diff --git a/README.md b/README.md index 983138b..8787d98 100644 --- a/README.md +++ b/README.md @@ -93,13 +93,28 @@ Login as the administrator role and I should see "Dashboard". ``` Feature: I login as the specfic role - @javascript Scenario: Login as the "administrator" role When I login as the "administrator" role Then I should see "Welcome to WordPress!" ``` +Selenium2 driver can't retrieve the HTTP response. +So you have to use `@mink::goutte` tag like following. + +``` +Feature: HTTP response + + @mink:goutte + Scenario: Check http status code + + When I am on "/" + Then the HTTP status should be 200 + + When I am on "/the-page-not-found" + Then the HTTP status should be 404 +``` + Run to see contexts. ``` diff --git a/behat.yml.dist b/behat.yml.dist index 91d2f89..178d75a 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -18,7 +18,10 @@ default: password: editor Behat\MinkExtension: base_url: http://127.0.0.1:8080 + default_session: default sessions: default: selenium2: wd_host: http://127.0.0.1:4444/wd/hub + goutte: + goutte: ~ diff --git a/composer.json b/composer.json index c04536d..9d86d9b 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "behat/mink-extension": "~2.2", "behat/mink-selenium2-driver": "~1.3.1", "phpunit/phpunit": "^5.6", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/guzzle": "^6.2", + "behat/mink-goutte-driver": "^1.2" }, "require-dev": { "phpspec/phpspec": "^3.1" diff --git a/composer.lock b/composer.lock index 23601bc..b99d78c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "576a4a3eca3f3279f2c21ecac9392b5f", - "content-hash": "c05a635476e5954a248f557496c6342c", + "hash": "dec07608b2a42c1e0f90b18a32cd6921", + "content-hash": "9e78a78d767c2cb692b5e82339adcea9", "packages": [ { "name": "behat/behat", @@ -205,6 +205,62 @@ ], "time": "2016-03-05 08:26:18" }, + { + "name": "behat/mink-browserkit-driver", + "version": "v1.3.2", + "source": { + "type": "git", + "url": "https://github.com/minkphp/MinkBrowserKitDriver.git", + "reference": "10e67fb4a295efcd62ea0bf16025a85ea19534fb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/minkphp/MinkBrowserKitDriver/zipball/10e67fb4a295efcd62ea0bf16025a85ea19534fb", + "reference": "10e67fb4a295efcd62ea0bf16025a85ea19534fb", + "shasum": "" + }, + "require": { + "behat/mink": "^1.7.1@dev", + "php": ">=5.3.6", + "symfony/browser-kit": "~2.3|~3.0", + "symfony/dom-crawler": "~2.3|~3.0" + }, + "require-dev": { + "silex/silex": "~1.2", + "symfony/phpunit-bridge": "~2.7|~3.0" + }, + "type": "mink-driver", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Behat\\Mink\\Driver\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + } + ], + "description": "Symfony2 BrowserKit driver for Mink framework", + "homepage": "http://mink.behat.org/", + "keywords": [ + "Mink", + "Symfony2", + "browser", + "testing" + ], + "time": "2016-03-05 08:59:47" + }, { "name": "behat/mink-extension", "version": "v2.2", @@ -264,6 +320,61 @@ ], "time": "2016-02-15 07:55:18" }, + { + "name": "behat/mink-goutte-driver", + "version": "v1.2.1", + "source": { + "type": "git", + "url": "https://github.com/minkphp/MinkGoutteDriver.git", + "reference": "8b9ad6d2d95bc70b840d15323365f52fcdaea6ca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/minkphp/MinkGoutteDriver/zipball/8b9ad6d2d95bc70b840d15323365f52fcdaea6ca", + "reference": "8b9ad6d2d95bc70b840d15323365f52fcdaea6ca", + "shasum": "" + }, + "require": { + "behat/mink": "~1.6@dev", + "behat/mink-browserkit-driver": "~1.2@dev", + "fabpot/goutte": "~1.0.4|~2.0|~3.1", + "php": ">=5.3.1" + }, + "require-dev": { + "symfony/phpunit-bridge": "~2.7|~3.0" + }, + "type": "mink-driver", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Behat\\Mink\\Driver\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + } + ], + "description": "Goutte driver for Mink framework", + "homepage": "http://mink.behat.org/", + "keywords": [ + "browser", + "goutte", + "headless", + "testing" + ], + "time": "2016-03-05 09:04:22" + }, { "name": "behat/mink-selenium2-driver", "version": "v1.3.1", @@ -419,6 +530,55 @@ ], "time": "2015-06-14 21:17:01" }, + { + "name": "fabpot/goutte", + "version": "v3.2.0", + "source": { + "type": "git", + "url": "https://github.com/FriendsOfPHP/Goutte.git", + "reference": "8cc89de5e71daf84051859616891d3320d88a9e8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/FriendsOfPHP/Goutte/zipball/8cc89de5e71daf84051859616891d3320d88a9e8", + "reference": "8cc89de5e71daf84051859616891d3320d88a9e8", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.0", + "php": ">=5.5.0", + "symfony/browser-kit": "~2.1|~3.0", + "symfony/css-selector": "~2.1|~3.0", + "symfony/dom-crawler": "~2.1|~3.0" + }, + "type": "application", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Goutte\\": "Goutte" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "A simple PHP Web Scraper", + "homepage": "https://github.com/FriendsOfPHP/Goutte", + "keywords": [ + "scraper" + ], + "time": "2016-11-15 16:27:29" + }, { "name": "guzzlehttp/guzzle", "version": "6.2.2", @@ -1894,6 +2054,63 @@ "homepage": "https://github.com/sebastianbergmann/version", "time": "2016-10-03 07:35:21" }, + { + "name": "symfony/browser-kit", + "version": "v3.2.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/browser-kit.git", + "reference": "34348c2691ce6254e8e008026f4c5e72c22bb318" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/browser-kit/zipball/34348c2691ce6254e8e008026f4c5e72c22bb318", + "reference": "34348c2691ce6254e8e008026f4c5e72c22bb318", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/dom-crawler": "~2.8|~3.0" + }, + "require-dev": { + "symfony/css-selector": "~2.8|~3.0", + "symfony/process": "~2.8|~3.0" + }, + "suggest": { + "symfony/process": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\BrowserKit\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony BrowserKit Component", + "homepage": "https://symfony.com", + "time": "2016-10-13 13:35:11" + }, { "name": "symfony/class-loader", "version": "v3.2.1", @@ -2242,6 +2459,62 @@ "homepage": "https://symfony.com", "time": "2016-12-08 15:27:33" }, + { + "name": "symfony/dom-crawler", + "version": "v3.2.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/dom-crawler.git", + "reference": "1638c7534a8a2fa0bf9e979f9aacb6d7e8e9e24e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/dom-crawler/zipball/1638c7534a8a2fa0bf9e979f9aacb6d7e8e9e24e", + "reference": "1638c7534a8a2fa0bf9e979f9aacb6d7e8e9e24e", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/polyfill-mbstring": "~1.0" + }, + "require-dev": { + "symfony/css-selector": "~2.8|~3.0" + }, + "suggest": { + "symfony/css-selector": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\DomCrawler\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony DomCrawler Component", + "homepage": "https://symfony.com", + "time": "2016-12-10 14:24:53" + }, { "name": "symfony/event-dispatcher", "version": "v3.2.1", diff --git a/features/http-response.feature b/features/http-response.feature index 264a063..4a3691f 100644 --- a/features/http-response.feature +++ b/features/http-response.feature @@ -1,5 +1,6 @@ Feature: HTTP response + @mink:goutte Scenario: Check http status code When I am on "/" @@ -8,6 +9,7 @@ Feature: HTTP response When I am on "/the-page-not-found" Then the HTTP status should be 404 + @mink:goutte Scenario: Check http response headers When I am on "/" diff --git a/src/Context/RawWordPressContext.php b/src/Context/RawWordPressContext.php index 98ed743..2afce81 100644 --- a/src/Context/RawWordPressContext.php +++ b/src/Context/RawWordPressContext.php @@ -72,51 +72,39 @@ public function get_variables( $key ) /** * Get http status code from the current page. * - * @param string $url The URL. - * @param string $method The request method. Default is 'GET'. - * @param array $params An array for the request. * @return int HTTP status code. */ - protected function get_http_status( $url, $method = 'GET', $params = array() ) + protected function get_http_status() { - $params = $params + $this->guzzle_params; - $params['stream'] = true; - $response = $this->guzzle->request( $method, $url, $params ); - - return intval( $response->getStatusCode() ); + $session = $this->getSession(); + return intval( $session->getStatusCode() ); } /** - * Get contents from $url. + * Get http response headers from the current page. * - * @param string $url The URL. - * @param string $method The request method. Default is 'GET'. - * @param array $params An array for the request. - * @return string The contents. + * @return array HTTP response headers. */ - protected function get_contents( $url, $method = 'GET', $params = array() ) + protected function get_http_headers() { - $params = $params + $this->guzzle_params; - $response = $this->guzzle->request( $method, $url, $params ); - - return $response->getBody(); + $session = $this->getSession(); + return $session->getResponseHeaders(); } /** - * Get http response headers from the current page. + * Get contents from $url. * - * @param string $url The URL. - * @param string $method The request method. Default is 'GET'. - * @param array $params An array for the request. - * @return array HTTP response headers. + * @param string $url The URL. + * @param string $method The request method. + * @param array $params An array of the http request. + * @return string The contents. */ - protected function get_http_headers( $url, $method = 'GET', $params = array() ) + protected function get_contents( $url, $method = 'GET', $params = array() ) { $params = $params + $this->guzzle_params; - $params['stream'] = true; $response = $this->guzzle->request( $method, $url, $params ); - return $response->getHeaders(); + return $response->getBody(); } /** @@ -129,10 +117,10 @@ protected function get_http_headers( $url, $method = 'GET', $params = array() ) */ protected function login( $user, $password ) { - $this->getSession()->visit( $this->locatePath( '/wp-login.php' ) ); + $this->getSession( 'default' )->visit( $this->locatePath( '/wp-login.php' ) ); $this->wait_the_element( "#loginform" ); - $element = $this->getSession()->getPage(); + $element = $this->getSession( 'default' )->getPage(); $element->fillField( "user_login", $user ); $element->fillField( "user_pass", $password ); @@ -166,7 +154,7 @@ protected function login( $user, $password ) */ protected function is_current_url( $url ) { - $current_url = $this->getSession()->getCurrentUrl(); + $current_url = $this->getSession( 'default' )->getCurrentUrl(); if ( $url === substr( $current_url, 0 - strlen( $url ) ) ) { return true; @@ -188,15 +176,15 @@ protected function logout() return; // user isn't login. } - $page = $this->getSession()->getPage(); + $page = $this->getSession( 'default' )->getPage(); $logout = $page->find( "css", "#wp-admin-bar-logout a" ); if ( ! empty( $logout ) ) { - $this->getSession()->visit( $this->locatePath( $logout->getAttribute( "href" ) ) ); + $this->getSession( 'default' )->visit( $this->locatePath( $logout->getAttribute( "href" ) ) ); for ( $i = 0; $i < $this->timeout; $i++ ) { try { - $url = $this->getSession()->getCurrentUrl(); + $url = $this->getSession( 'default' )->getCurrentUrl(); if ( strpos( $url, "loggedout=true" ) ) { return true; } @@ -219,7 +207,7 @@ protected function logout() */ protected function is_logged_in() { - $page = $this->getSession()->getPage(); + $page = $this->getSession( 'default' )->getPage(); if ( $page->find( "css", ".logged-in" ) ) { return true; } elseif ( $page->find( "css", ".wp-admin" ) ) { @@ -238,7 +226,7 @@ protected function is_logged_in() */ protected function wait_the_element( $selector ) { - $page = $this->getSession()->getPage(); + $page = $this->getSession( 'default' )->getPage(); $element = $page->find( 'css', $selector ); for ( $i = 0; $i < $this->timeout; $i++ ) { @@ -269,7 +257,7 @@ protected function get_plugins() throw new \Exception( "You are not logged in" ); } - $session = $this->getSession(); + $session = $this->getSession( 'default' ); $session->visit( $this->locatePath( $this->get_admin_url() . '/plugins.php' ) ); $page = $session->getPage(); $e = $page->findAll( 'css', "#the-list tr" ); @@ -307,8 +295,8 @@ protected function get_current_theme() throw new \Exception( "You are not logged in" ); } - $this->getSession()->visit( $this->locatePath( $this->get_admin_url() . '/themes.php' ) ); - $page = $this->getSession()->getPage(); + $this->getSession( 'default' )->visit( $this->locatePath( $this->get_admin_url() . '/themes.php' ) ); + $page = $this->getSession( 'default' )->getPage(); $e = $page->find( 'css', ".theme.active" ); if ( $e ) { $theme = $e->getAttribute( "data-slug" ); @@ -328,8 +316,8 @@ protected function get_current_theme() */ protected function get_wp_version() { - $this->getSession()->visit( $this->locatePath( '/' ) ); - $page = $this->getSession()->getPage(); + $this->getSession( 'default' )->visit( $this->locatePath( '/' ) ); + $page = $this->getSession( 'default' )->getPage(); $meta = $page->find( 'css', "meta[name=generator]" ); if ( $meta ) { $version = $meta->getAttribute( "content" ); diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index c5ae3fa..f89e400 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -32,8 +32,7 @@ public function save_env_as_var( $env, $var ) */ public function the_http_status_should_be( $expect ) { - $current_url = $this->getSession()->getCurrentUrl(); - $status = $this->get_http_status( $current_url ); + $status = $this->get_http_status(); $this->assertSame( $status, intval( $expect ), sprintf( 'The HTTP status is %1$s, but it should be %2$s', $status, @@ -56,8 +55,7 @@ public function the_http_status_should_be( $expect ) */ public function the_http_headers_should_be( TableNode $table ) { - $current_url = $this->getSession()->getCurrentUrl(); - $headers = $this->get_http_headers( $current_url ); + $headers = $this->get_http_headers(); foreach ( $table->getHash() as $row ) { if ( ! empty( $headers[ $row['header'] ] ) ) {