Create end-to-end tests
End-to-end (E2E) testing ensures seamless system functionality and identifies errors before they reach production. This guide explores how to write end-to-end tests using Playwright. We'll cover everything from configuring Playwright to setting up Single Sign-On (SSO) and common pitfalls to avoid. By the end of this guide, you'll have a solid understanding of creating maintainable test suites that will help you catch errors before they become a problem.
What to consider when writing E2E tests
When writing E2E tests, you need to consider the following:
- Install the app on a target environment. It ensures that you're testing the app in a similar environment to the user (for example, having SSO in place, not running the app on a development server, or similar).
- Avoid hardcoded environment URLs and login information for a smooth login flow to an arbitrary environment. Instead, pass the necessary information as environment variables or read them from configuration files. It allows you to run your tests on different environments, even at various stages (such as development or hardening).
Install dependencies
To install the required dependencies, run the following command in your terminal:
npm init playwright@latest
Follow the prompt and select the following options:
Where to put your end-to-end tests?
- write
e2e
and press enter
- write
Add a GitHub Actions workflow?
- select
false
and press enter
- select
Install Playwright browsers (can be done manually via 'npx playwright install')?
- select
true
and press enter
- select
Next, run the following command:
npm install --save-dev dotenv
The above command will install Playwright and the dotenv
package. You'll need Playwright to run tests and the dotenv
package to use .env
files in your tests.