3.7 KiB
WooCommerce End to End Tests
Automated end-to-end tests for WooCommerce.
Table of contents
Pre-requisites
Install NodeJS
brew install node #MacOS
Install dependencies
npm install
Configuration
Test Configuration
The tests use environment variables to specify values for the tests.
To login to the site, loginUser()
utility function of e2e-test-utils
package is being used. The function relies on the following config.js
file to specify base URL, Admin user details and Test user details (could be different from Admin. For example, customer):
const WP_ADMIN_USER = {
username: 'admin',
password: 'password',
};
const {
WP_USERNAME = WP_ADMIN_USER.username,
WP_PASSWORD = WP_ADMIN_USER.password,
WP_BASE_URL = 'http://localhost:8889',
} = process.env;
export {
WP_ADMIN_USER,
WP_USERNAME,
WP_PASSWORD,
WP_BASE_URL,
};
As per above, create an Admin user on the site and set its username and password:
- username:
admin
- password:
password
Specify base URL and Test user details using environment variables.
Environment variables
Set environmental variables as shown below. Note that you don't need to add the trailing slash ('/') at the end of the site URL:
export WP_BASE_URL={your site URL}
export WP_USERNAME={your Test user username}
export WP_PASSWORD={your Test user password}
You can unset the variables when you are done:
unset WP_BASE_URL
unset WP_USERNAME
unset WP_PASSWORD
Running tests
How to run tests
To run e2e tests use the following command:
npm run test:e2e
Puppeteer runs headless by default. However, sometimes it's useful to observe the browser while running tests. To do so you can use these environment variables:
PUPPETEER_HEADLESS=false PUPPETEER_SLOWMO=50 npm run test:e2e
SlowMo slows down Puppeteer’s operations so we can see what’s going on in the browser. PUPPETEER_SLOWMO=50
means test actions will be slowed down by 50 milliseconds.
To run an individual test, use the direct path to the spec. For example:
npm run test-e2e ./tests/e2e-tests/specs/activate-woocommerce.test.js
You can also provide the base URL, Test username and Test password like this:
WP_BASE_URL={your site URL} WP_USERNAME={your Test user username} WP_PASSWORD={your Test user password} npm run test:e2e
Writing tests
We use the following tools to write e2e tests:
- Puppeteer – a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol
- jest-puppeteer – provides all required configuration to run tests using Puppeteer
- expect-puppeteer – assertion library for Puppeteer
Tests are kept in tests/e2e-tests/specs
folder.
The following packages are being used to write tests:
e2e-test-utils
- End-To-End (E2E) test utils for WordPress. You can find the full list of utils here.