Back to Test Automation
2026-03-085 min read

end-to-end (E2E) tests (Test Automation)

Learn end-to-end (E2E) tests (Test Automation) step by step with clear examples and exercises.

Why This Matters

In this full guide, we'll look closely at end-to-end (E2E) testing using popular JavaScript libraries—Selenium, Cypress, and Playwright. We'll provide a worked example, common mistakes to avoid, practice questions, and FAQs to help you master test automation.

Why This Matters

In the fast-paced world of web development, ensuring software quality is crucial. E2E tests simulate user interactions with your application, verifying that everything functions as intended. They are particularly valuable for:

  1. Regression Testing: E2E tests help maintain code stability by identifying any unintended changes or bugs introduced during updates or refactoring.
  2. User Interface Verification: E2E tests ensure the UI remains consistent and functions correctly across various devices and browsers.
  3. Reducing Manual Testing Effort: Automated testing reduces the need for manual testing, making it easier to scale your application.

Prerequisites

To follow along with this guide, you'll need:

  1. Basic understanding of JavaScript and HTML/CSS
  2. Familiarity with one or more front-end frameworks (Angular, React, Vue)
  3. Knowledge of Node.js and npm (Node Package Manager)
  4. A code editor like Visual Studio Code, Atom, or Sublime Text
  5. A web application for testing purposes (you can create a simple one if needed)

Core Concept

Selenium

Selenium is the most established and widely-used test automation framework. It supports multiple programming languages, including JavaScript through WebDriverJS.

To set up Selenium with JavaScript:

  1. Install WebDriverJS using npm: npm install webdriverio
  2. Create a new file (e.g., test.js) and write your test script.
  3. Initialize the test by setting up the desired capabilities (browser, version, etc.).
  4. Write test steps using the WebDriverIO API to interact with the application under test.
  5. Assert the results of each step to verify that the application behaves as expected.

Cypress

Cypress is a newer, faster, and easier-to-use E2E testing library. It runs directly in the browser, making it more efficient and reliable than Selenium.

To set up Cypress:

  1. Install Cypress using npm: npm install cypress
  2. Create a new file (e.g., integration/example_spec.js) for your test script.
  3. Write your test steps using the Cypress API, which includes commands for interacting with elements and verifying their state.
  4. Run the tests by opening the Cypress Test Runner (cypress open).

Playwright

Playwright is a modern E2E testing library developed by Microsoft. It supports multiple browsers (Chromium, Firefox, WebKit) and platforms (desktop, mobile web, Android, iOS).

To set up Playwright:

  1. Install Playwright using npm: npm install playwright
  2. Write your test script in JavaScript (or TypeScript).
  3. Initialize the test by setting up the browser type and version.
  4. Use the Playwright API to interact with the application under test and verify its behavior.

Worked Example

We'll create a simple E2E test for a login form using Selenium, Cypress, and Playwright. The form will accept valid credentials (username: testuser, password: testpassword) and display an error message for invalid ones.

Selenium

const webdriver = require('selenium-webdriver');

(async function() {
const driver = new webdriver.Builder()
.forBrowser('chrome')
.build();

await driver.get('http://yourwebsite.com/login');

// Find the username and password fields, enter credentials, and click submit
const userNameField = await driver.findElement(webdriver.By.id('username'));
const passwordField = await driver.findElement(webdriver.By.id('password'));
const submitButton = await driver.findElement(webdriver.By.id('submit'));

await userNameField.sendKeys('testuser');
await passwordField.sendKeys('testpassword');
await submitButton.click();

// Verify that the login was successful
const welcomeMessage = await driver.findElement(webdriver.By.id('welcome-message'));
const welcomeText = await welcomeMessage.getText();
expect(welcomeText).toEqual('Welcome, testuser!');
})();

Cypress

describe('Login', function() {
it('should allow valid credentials', function() {
cy.visit('http://yourwebsite.com/login');

// Find the username and password fields, enter credentials, and click submit
cy.get('#username').type('testuser');
cy.get('#password').type('testpassword');
cy.get('#submit').click();

// Verify that the login was successful
cy.get('#welcome-message').should('contain', 'Welcome, testuser!');
});
});

Playwright

const { chromium } = require('playwright');

(async function() {
const browser = await chromium.launch();
const page = await browser.newPage();

// Navigate to the login page and fill in the form
await page.goto('http://yourwebsite.com/login');
await page.fill('#username', 'testuser');
await page.fill('#password', 'testpassword');
await page.click('#submit');

// Verify that the login was successful
const welcomeMessage = await page.$eval('#welcome-message', (el) => el.textContent);
expect(welcomeMessage).toEqual('Welcome, testuser!');

await browser.close();
})();

Common Mistakes

  1. Not waiting for elements to load: Ensure that you wait for elements to appear on the page before interacting with them.
  2. Ignoring browser-specific quirks: Different browsers may handle certain elements or behaviors differently, so test across multiple browsers if possible.
  3. Not handling errors gracefully: Make sure your tests can recover from common errors like network issues or timeouts.
  4. Not using assertions correctly: Assertions should be used to verify that the application behaves as expected, not just to check for the presence of elements.
  5. Writing brittle tests: Avoid writing tests that depend on specific element IDs or locations—instead, use CSS selectors or other more robust methods.

Practice Questions

  1. Write an E2E test using Selenium, Cypress, or Playwright to verify the functionality of a search bar on your website.
  2. Implement an E2E test for a shopping cart that checks the total price and quantity of items after adding multiple products.
  3. Write an E2E test using any of the libraries to ensure that user registration and login work correctly on your application.

FAQ

  1. Why should I use E2E testing? E2E testing helps maintain software quality, reduces manual testing effort, and ensures a consistent user experience across devices and browsers.
  2. What are the differences between Selenium, Cypress, and Playwright? Selenium is more established but less efficient than Cypress and Playwright, which offer faster performance and better browser integration.
  3. How can I set up E2E testing for my application using JavaScript? Install the necessary library (WebDriverIO for Selenium, Cypress, or Playwright), create a test script, and run your tests using the provided test runner.
  4. What are some common mistakes to avoid when writing E2E tests? Avoid writing brittle tests, ignoring browser-specific quirks, not handling errors gracefully, and not using assertions correctly.
  5. How can I ensure that my tests run smoothly across multiple browsers? Test your application on various browsers (Chrome, Firefox, Safari) to identify any inconsistencies in behavior.
end-to-end (E2E) tests (Test Automation) | Test Automation | XQA Learn