The WordPress Helper comes inbuilt with WPSelenium. It is particularly useful when building custom plugins or themes in WordPress that require UI testing. See Helper features below to see added functionality.
To test a WordPress site with the helper you would need to specify the path to the root of your WordPress Site in the
wpselenium.xml config. An example of this is below:
<wpselenium> <siteUrl>http://localhost:3000</siteUrl> <sitePath>/opt/lampp/htdocs/wpseleniumdemosite</sitePath> </wpselenium>
Having done this you can run the wpselenium command with the
--type arguement set to
wordpress. An example of this is below:
/vendor/bin/wpselenium chrome --type wordpress
Similar to testing on a standard site, on first run, WPSelenium will configure all the necessary files, but in addition to those files, it will also install the WPSeleniumTestPlugin to your WordPress site. You will need to go activate this plugin before you can test your WordPress site.
Once activated everything else should be like testing a standard site.
- As discussed in the Getting Started section, wpselenium needs to be installed at the root of the project you want to test. In WordPress this is usually in the root of the plugin or theme you want to test. By default WordPress plugins/themes are not composer projects. This means you will need to make the plugin/theme you want to test a composer project, as discussed in the Getting Started section (which is also good practice).
- If you are using xampp/lammp, there is a potential that you might have two versions of php on your system. If you get a database related error (or any other error in general) make sure your system PATH variable points to the php version WordPress is using first. See Other Notes > Troubleshooting for more details.
Path to the root of your WordPress site.
Note:- This setting is only necessary if you are going to test a WordPress with the
--type wordpressoption. If you dont need the extra features this provides you can still use the basic configuration to test your WordPress site as a standard site.
As already discussed in the Introduction helpers add framework specific functionality to the site your testing. Below are the features the WordPress helper provides.
1. Extending WordPress Core
When using this helper, all your tests extend WordPress-Core. This means all the WordPress-Core functionalities available to you when building your plugin or theme will be available to you when writing your tests. For instance you can call
get_template_directory() (which is a WordPress-Core function) inside your WPSelenium Test Case. This also means you can also combine your logical unit test with your selenium unit tests in a Test Case.
2. wpBeforeRun method annotation
The helper also provides the
wpBeforeRun method annotation. This annotation allows you to point to a static function that is run within the WordPress context before your tests run. In essence it dynamically 'extends' your WordPress functions.php file for each testcase.
There are many cases where this can be useful. For instance if you want to compare how long 2 different plugins take to process somthing. Using
wpBeforeRun you can dynamically switch between them before your tests run (without having to modify any of your source code ).
For an example of this please see Writing Tests > Example (WordPress).