A community-driven Cypress testing suite for Magento 2
This testing suite was announced in Peter Jaap his talk about Cypress testing in Magento 2 at Reacticon 4. Watch the talk for more information about Cypress testing.
A successful Cypress run which tests the store at https://hyva-demo.elgentos.io
-
Magento 2 runs in Single Store Mode
-
Default language is English
-
Viewport is 1920x1280, with support for mobile viewports
This test suite is not plug & play for your store. A number of tests rely on Magento's default sample data. These tests will fail when you don't have the sample data. It is up to you to change the fixtures/selectors/tests to make them pass for your store.
We don't do Commerce builds over at elgentos so we haven't spent time creating tests for Commerce-only functionality.
We do not particularly strive for 100% test coverage. We have identified the most common and most revenue-dependent scenarios. For example, we do test viewing products, filtering categories, adding products to the cart, etcetera but we do (currently) not test the Email a Friend or Compare Products feature since these are rarely used in an average Magento store. We are perfectly willing to merge a PR with these tests of course.
You need to copy the whole suite into your project. We are open to suggestions on how to solve this, see Discussions.
We haven't made tests for the Hyvä React Checkout yet. PR's are definitely welcome now that we have a stable version.
We are at 72%; 56 out of the proposed 78 tests are done.
Wording & naming are subject to change.
Spec file | Group | Test |
---|---|---|
account.spec.js |
Account test creation | ✔️ it can create an account to log in with |
Account activities | ✔️ it creates an account to log in with and use for further testing | |
✔️ it can log out | ||
✔️ it can show the account information page and display the name of the customer | ||
✔️ it can change the password | ||
✔️ it can change the name of the customer on the account information page | ||
✔️ it can navigate to all customer account pages and displays the correct titles | ||
✔️ it can navigate to order history and displays that there are no placed orders | ||
✔️ it can add a new address | ||
✔️ it can change an existing address | ||
✔️ it can remove an address | ||
✔️ it subscribe through the newsletter subscription page | ||
✔️ it can add an address automatically when an order is placed | ||
✔️ it can add a product to the wishlist of the logged in customer on a productpage and store it | ||
✔️ it can edit the wishlist on the wishlist page | ||
🔲 it can reset the password when it is forgotten | ||
Do not login before these tests | ✔️ it can login from cart without making changes to the cart | |
✔️ it can login from checkout | ||
product-searches.spec.js |
Perform searches | ✔️ it can perform search with multiple hits |
✔️ it can find a single product | ||
✔️ it can show a page for no search results when the searchterm cannot give any results | ||
✔️ it can show suggestions when entering search terms | ||
category.spec.js |
Category page tests | ✔️ it can navigate to the category page and filter products on the color red |
✔️ it can sort the products on price from lowest to highest | ||
✔️ it can change the number of products to be displayed | ||
✔️ it checks if the breadcrumb is displayed correctly | ||
✔️ it checks if the pagination is working | ||
✔️ it can switch from list to grid view | ||
homepage.spec.js |
Home page tests | ✔️ it can navigate to the homepage |
✔️ it can perform search from homepage | ||
✔️ it can open a category | ||
🔲 it can show the header correctly and all links work | ||
🔲 it can show the footer correctly and all links work | ||
🔲 it can show the main section of the homepage correctly and all links work | ||
✔️ it can subscribe to the newsletter | ||
🔲 it can add products shown on the homepage to the cart when a add to cart button is present | ||
🔲 it shows the cookie banner when cookies are not accepted yet (vanilla hyva shows no cookie banner) | ||
checkout.spec.js |
Checkout tests (guest) | 🔲 it shows the correct products and quantities previously added to the cart |
✔️ it shows correct prices like subtotal, VAT, shipping costs and total | ||
✔️ it can see coupon discount in checkout | ||
🔲 it can properly choose and use all listed payment methods | ||
🔲 it can properly choose and use all listed shipping methods | ||
🔲 it a conformation mail is send to the customer after placing the order | ||
🔲 it an invoice is created and sent to the customer after placing the order | ||
✔️ it after placing the order it is properly processed in the backend' | ||
Checkout tests (logged in) | 🔲 it can automatically fill in the name and address of the customer | |
✔️ it can find and order in the customer order history after having placed an order | ||
cart.spec.js |
Cart tests | ✔️ it can add a product to the cart |
✔️ it can change the quantity in the cart | ||
✔️ it can remove a product from the cart | ||
✔️ it can add a coupon to the cart | ||
✔️ it can delete an added coupon from the cart | ||
✔️ it cannot add a non existing coupon | ||
✔️ it displays the correct productprices and totals | ||
🔲 it merges an already existing cart when a customer logs in | ||
minicart.spec.js |
Minicart tests | 🔲 it can open the cart slider by clicking on the cart icon in the header |
✔️ it checks if the items and prices in the slider are displayed correctly | ||
✔️ it can delete an item in the cart slider | ||
🔲 it can change the quantity of an item in the cart slider | ||
✔️ it can navigate to the cart with a link in the slider | ||
✔️ it can navigate to the checkout with a link in the slider | ||
product-page.spec.js |
Product page tests | ✔️ it can display the titel and image of the product |
✔️ it shows the product price | ||
✔️ it can configure the product when it is an configurable product | ||
✔️ it can add the product to the cart | ||
✔️ it can't add the product to the cart if it is a configurable product and no options are selected | ||
✔️ it can add the product to the wislist | ||
✔️ it shows the correct breadcrumb | ||
✔️ it can show reviews made by logged in customers | ||
✔️ it can add a review when logged in | ||
✔️ it can indicate if a product is in stock | ||
✔️ it can't add a product to the cart when the product is out of stock (commented out, needs admin token in the cypress.env.json) | ||
cms-page.spec.js |
CMS page tests | 🔲 it shows the default 404 page on an non-existent route |
🔲 it can open the default CMS page correctly | ||
contact-form.spec.js |
Contact form tests | 🔲 it shows the contact form correctly |
🔲 it cannot submit a form when no valid email address is entered | ||
🔲 it can submit the form when all validation passes | ||
back-end.spec.js |
Back-end tests | 🔲 it can login on the administration panel of the magento environment |
🔲 it can show customer data | ||
🔲 it processes orders and invoices correctly | ||
🔲 it can edit an order |
First, install Cypress in the root of your Magento 2 project;
npm init
npm install cypress --save-dev
npm install cypress-file-upload cypress-localstorage-commands --save-dev
The easiest way to install the tests is to clone this repository and move the cypress
folder into your project. As of right now, we do not provide a fallback mechanism for customizations to the tests, see Limitations.
git clone [email protected]:elgentos/magento2-cypress-testing-suite.git
mv magento2-cypress-testing-suite/cypress .
mv magento2-cypress-testing-suite/cypress.json .
rm -rf magento2-cypress-testing-suite
Also see Tags below about how to set up using tags.
Then edit the cypress.json
file in the root of your project to update your baseUrl and possibly some other defaults we've set:
{
"baseUrl": "https://example.com",
"watchForFileChanges": false,
"viewportWidth": 1920,
"viewportHeight": 1080,
"env": {
"mobileViewportWidthBreakpoint": 768
}
}
Also add these lines to your .gitignore
to avoid cluttering your Git repo;
node_modules
cypress/screenshots
cypress/videos
cypress.env.json
Some tests are dependent on making changes in the database. This is done through the Magento 2 REST API. You will need to create an admin token for these tests. This is easily done using magerun2.
Get a list of all the admin users: magerun2 admin:user:list
If you don't have any admin users, create one; magerun2 admin:user:create --admin-user=john --admin-firstname=John --admin-lastname=Doe --admin-password=JohnDoe123 --admin-email="[email protected]"
Then create a token: magerun2 admin:token:create username_goes_here
You then can add the token to cypress.env.json
as an environment variable:
{
"MAGENTO2_ADMIN_TOKEN": "token_goes_here"
}
Or you can set it in your CI/CD variables by prefixing the environment variable name with CYPRESS_
: CYPRESS_MAGENTO2_ADMIN_TOKEN: <token_goes_here>
.
We use tags.to discern between hot tests and cold tests. You need to install the 3rd party tag support package. Following the setup for that package, you need to add it to the plugins/index.js
and support/index.d.ts
.
npm install cypress-tags typescript --save-dev
npm ci
npx cypress run
# npx cypress open # if you want to use the GUI
Individual specs can be run using the following command
npx cypress run --spec ./cypress/integration/path/to/some.spec.js
Set up your local URL in cypress/plugins/index.js
. Then run Cypress with NODE_ENV=develop; npx cypress run
.
We are very open to contributions! We would love to have mobile viewport support for Hyvä, tests for Commerce functionality, additional tests, code improvements, a fallback mechanism, etcetera etcetera. See the Issues tab for issues to pick up.
We will be updating this readme soon with extensive contribution guidelines, but here is a short summary:
- Avoid creating global
cy
functions (Custom Commands), instead put functions in utils/helpers and import them - Avoid creating aliases that are only used once
- Use
cy.get()
as much as possible, only usecy.contains()
in specific cases - try to avoid it - Do not write assertions in page objects, move those to the spec files. Red flag;
should()
in a page object - Every test (an
it()
function) has to be able to run stand-alone; it should not depend on any other test. You can test this by add.only
(see Cypress docs).