Links

WPBrowser

This module should be used in acceptance and functional tests, see levels of testing for more information.
This module extends the PHPBrowser module adding WordPress-specific configuration parameters and methods.
The module simulates a user interaction with the site without Javascript support; if you need to test your project with Javascript support use the WPWebDriver module.

Module requirements for Codeception 4.0+

This module requires the codeception/module-phpbrowser Composer package to work when wp-browser is used with Codeception 4.0.
To install the package run:
composer require --dev codeception/module-phpbrowser:^1.0

Configuration

Since this module extends the PHPBrowser module provided by Codeception, please refer to the PHPBrowser configuration section for more information about the base configuration parameters.
  • url required - Start URL of your WordPress project, e.g. http://wp.test.
  • headers - Default headers are set before each test; this might be useful to simulate a specific user agent during the tests or to identify the request source. Note that the headers defined in the config should be prefaced with HTTP_ in your wp-config.php file. This can be used to select which database to use.
  • handler (default: curl) - The Guzzle handler to use. By default curl is used, also possible to pass stream, or any valid class name as Handler.
  • middleware - The Guzzle middlewares to add. An array of valid callables is required; see here for more information.
  • curl - curl options; only applied if using the curl handler; more options are available.
  • adminUsername required - This is the login name, not the "nice" name, of the administrator user of the WordPress test site. This will be used to fill the username field in WordPress login page.
  • adminPassword required - This is the the password of the administrator use of the WordPress test site. This will be used to fill the password in WordPress login page.
  • adminPath required - The path, relative to the WordPress test site home URL, to the administration area, usually /wp-admin.

Example configuration

modules:
enabled:
- WPBrowser
config:
WPBrowser:
url: 'http://wordpress.localhost'
adminUsername: 'admin'
adminPassword: 'password'
adminPath: '/wp-admin'
headers:
X_TEST_REQUEST: 1
X_WPBROWSER_REQUEST: 1

Public API

activatePlugin

In the plugin administration screen activates a plugin clicking the "Activate" link. The method will not handle authentication to the admin area.
// Activate a plugin.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->activatePlugin('hello-dolly');
// Activate a list of plugins.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->activatePlugin(['hello-dolly','another-plugin']);
Parameters
  • string/\Codeception\Module\array $pluginSlug - The plugin slug, like "hello-dolly" or a list of plugin slugs.

amEditingPostWithId

Go to the admin page to edit the post with the specified ID. The method will not handle authentication the admin area.
$I->loginAsAdmin();
$postId = $I->havePostInDatabase();
$I->amEditingPostWithId($postId);
$I->fillField('post_title', 'Post title');
Parameters
  • int $id - The post ID.

amOnAdminAjaxPage

Go to the admin-ajax.php page to start a synchronous, and blocking, GET AJAX request. The method will not handle authentication, nonces or authorization.
$I->amOnAdminAjaxPage(['action' => 'my-action', 'data' => ['id' => 23], 'nonce' => $nonce]);
Parameters
  • string/\Codeception\Module\array $queryVars - A string or array of query variables to append to the AJAX path.

amOnAdminPage

Go to a page in the admininstration area of the site. This method will not handle authentication to the administration area.
$I->loginAs('user', 'password');
// Go to the plugins management screen.
$I->amOnAdminPage('/plugins.php');
Parameters
  • string $page - The path, relative to the admin area URL, to the page.

amOnCronPage

Go to the cron page to start a synchronous, and blocking, GET request to the cron script.
// Triggers the cron job with an optional query argument.
$I->amOnCronPage('/?some-query-var=some-value');
Parameters
  • string/\Codeception\Module\array $queryVars - A string or array of query variables to append to the AJAX path.

amOnPagesPage

Go the "Pages" administration screen. The method will not handle authentication.
$I->loginAsAdmin();
$I->amOnPagesPage();
$I->see('Add New');

amOnPluginsPage

Go to the plugins administration screen. The method will not handle authentication.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->activatePlugin('hello-dolly');

deactivatePlugin

In the plugin administration screen deactivate a plugin clicking the "Deactivate" link. The method will not handle authentication and navigation to the plugins administration page.
// Deactivate one plugin.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->deactivatePlugin('hello-dolly');
// Deactivate a list of plugins.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->deactivatePlugin(['hello-dolly', 'my-plugin']);
Parameters
  • string/\Codeception\Module\array $pluginSlug - The plugin slug, like "hello-dolly", or a list of plugin slugs.

dontSeePluginInstalled

Assert a plugin is not installed in the plugins administration screen. The method will not handle authentication and navigation to the plugin administration screen.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->dontSeePluginInstalled('my-plugin');
Parameters
  • string $pluginSlug - The plugin slug, like "hello-dolly".

grabCookiesWithPattern

Returns all the cookies whose name matches a regex pattern.
$I->loginAs('customer','password');
$I->amOnPage('/shop');
$cartCookies = $I->grabCookiesWithPattern("#^shop_cart\\.*#");
Parameters
  • string $cookiePattern - The regular expression pattern to use for the matching.

grabWordPressTestCookie

Returns WordPress default test cookie object if present.
// Grab the default WordPress test cookie.
$wpTestCookie = $I->grabWordPressTestCookie();
// Grab a customized version of the test cookie.
$myTestCookie = $I->grabWordPressTestCookie('my_test_cookie');
Parameters
  • string $name - Optional, overrides the default cookie name.

logOut

Navigate to the default WordPress logout page and click the logout link.
// Log out using the `wp-login.php` form and return to the current page.
$I->logOut(true);
// Log out using the `wp-login.php` form and remain there.
$I->logOut(false);
// Log out using the `wp-login.php` form and move to another page.
$I->logOut('/some-other-page');
Parameters
  • bool/bool/string $redirectTo - Whether to redirect to another (optionally specified) page after the logout.

loginAs

Login as the specified user. The method will not follow redirection, after the login, to any page.
$I->loginAs('user', 'password');
$I->amOnAdminPage('/');
$I->see('Dashboard');
Parameters
  • string $username - The user login name.
  • string $password - The user password in plain text.

loginAsAdmin

Login as the administrator user using the credentials specified in the module configuration. The method will not follow redirection, after the login, to any page.
$I->loginAsAdmin();
$I->amOnAdminPage('/');
$I->see('Dashboard');

seeErrorMessage

In an administration screen look for an error admin notice. The check is class-based to decouple from internationalization. The method will not handle authentication and navigation the administration area. .notice.notice-error ones.
$I->loginAsAdmin()
$I->amOnAdminPage('/');
$I->seeErrorMessage('.my-plugin');
Parameters
  • string/string/\Codeception\Module\array $classes - A list of classes the notice should have other than the

seeMessage

In an administration screen look for an admin notice. The check is class-based to decouple from internationalization. The method will not handle authentication and navigation the administration area.
$I->loginAsAdmin()
$I->amOnAdminPage('/');
$I->seeMessage('.missing-api-token.my-plugin');
Parameters
  • string/\Codeception\Module\array/string $classes - A list of classes the message should have in addition to the .notice one.

seePluginActivated

Assert a plugin is activated in the plugin administration screen. The method will not handle authentication and navigation to the plugin administration screen.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->seePluginActivated('my-plugin');
Parameters
  • string $pluginSlug - The plugin slug, like "hello-dolly".

seePluginDeactivated

Assert a plugin is not activated in the plugins administration screen. The method will not handle authentication and navigation to the plugin administration screen.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->seePluginDeactivated('my-plugin');
Parameters
  • string $pluginSlug - The plugin slug, like "hello-dolly".

seePluginInstalled

Assert a plugin is installed, no matter its activation status, in the plugin adminstration screen. The method will not handle authentication and navigation to the plugin administration screen.
$I->loginAsAdmin();
$I->amOnPluginsPage();
$I->seePluginInstalled('my-plugin');
Parameters
  • string $pluginSlug - The plugin slug, like "hello-dolly".

seeWpDiePage

Checks that the current page is one generated by the wp_die function. The method will try to identify the page based on the default WordPress die page HTML attributes.
$I->loginAs('user', 'password');
$I->amOnAdminPage('/forbidden');
$I->seeWpDiePage();
This class extends \Codeception\Module\PhpBrowser
This class implements \Codeception\Lib\Interfaces\MultiSession, \Codeception\Lib\Interfaces\Remote, \Codeception\Lib\Interfaces\Web, \Codeception\Lib\Interfaces\PageSourceSaver, \Codeception\Lib\Interfaces\ElementLocator, \Codeception\Lib\Interfaces\ConflictsWithModule
Last modified 2yr ago