End-to-end (E2E) testing is a crucial part of the software development lifecycle, ensuring that the entire application flow works as expected from start to finish. In this section, we will explore how to perform E2E testing using Playwright with TypeScript.

Key Concepts of End-to-End Testing

  1. User Journey Simulation: E2E tests simulate real user scenarios to verify that the application behaves correctly.
  2. Full System Testing: These tests cover the entire application stack, including the frontend, backend, and any integrated services.
  3. Environment Setup: E2E tests often require a specific environment setup that mirrors production as closely as possible.

Setting Up Playwright for E2E Testing

Before writing E2E tests, ensure that Playwright is installed and configured in your project. If you haven't set up Playwright yet, refer to Module 2 for installation instructions.

Example: Basic E2E Test

Let's create a simple E2E test to verify the login functionality of a web application.

import { chromium, Browser, Page } from 'playwright';

(async () => {
  const browser: Browser = await chromium.launch();
  const page: Page = await browser.newPage();

  // Navigate to the login page
  await page.goto('https://example.com/login');

  // Fill in the login form
  await page.fill('#username', 'testuser');
  await page.fill('#password', 'password123');

  // Submit the form
  await page.click('button[type="submit"]');

  // Verify successful login by checking the URL or a specific element
  await page.waitForSelector('#welcome-message');
  const welcomeMessage = await page.textContent('#welcome-message');
  console.log(welcomeMessage); // Should print a welcome message

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

Explanation

  • Browser and Page: We launch a Chromium browser instance and create a new page.
  • Navigation: The script navigates to the login page of the application.
  • Form Interaction: We fill in the username and password fields and submit the form.
  • Verification: After submission, we wait for a specific element that indicates a successful login and print its content.

Practical Exercise

Exercise: Write an E2E test to verify the search functionality on a website.

  1. Navigate to the homepage of a website with a search feature.
  2. Enter a search query in the search input field.
  3. Submit the search form.
  4. Verify that the search results page displays results related to the query.

Solution:

import { chromium, Browser, Page } from 'playwright';

(async () => {
  const browser: Browser = await chromium.launch();
  const page: Page = await browser.newPage();

  // Navigate to the homepage
  await page.goto('https://example.com');

  // Enter a search query
  await page.fill('#search-input', 'Playwright');

  // Submit the search form
  await page.click('button[type="submit"]');

  // Verify search results
  await page.waitForSelector('.search-results');
  const results = await page.$$eval('.search-result-item', items => items.map(item => item.textContent));
  console.log(results); // Should print an array of search result titles

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

Common Mistakes and Tips

  • Element Selectors: Ensure that the selectors used in your tests are accurate and stable. Use data attributes or unique IDs when possible.
  • Timing Issues: Use waitForSelector or similar methods to handle asynchronous operations and ensure elements are available before interacting with them.
  • Environment Consistency: Run tests in an environment that closely resembles production to catch environment-specific issues.

Conclusion

End-to-end testing with Playwright allows you to automate complex user interactions and verify the complete functionality of your application. By simulating real user scenarios, you can ensure that your application behaves as expected in production. In the next section, we will explore visual testing with Playwright, which complements E2E testing by verifying the visual aspects of your application.

© Copyright 2024. All rights reserved