In this section, we will explore how to execute Playwright tests effectively. Running tests is a crucial part of the development cycle, allowing you to verify that your code behaves as expected. We will cover the following key concepts:

  1. Understanding the Playwright Test Runner
  2. Executing Tests from the Command Line
  3. Configuring Test Options
  4. Interpreting Test Results

  1. Understanding the Playwright Test Runner

Playwright comes with its own test runner, which is designed to work seamlessly with its API. The test runner provides features such as parallel test execution, test retries, and more.

Key Features:

  • Parallel Execution: Run multiple tests simultaneously to speed up the testing process.
  • Retries: Automatically retry failed tests to handle flaky tests.
  • Reporters: Generate test reports in various formats.

  1. Executing Tests from the Command Line

To run Playwright tests, you will typically use the command line. Here’s how you can execute your tests:

Step-by-Step Guide:

  1. Navigate to Your Project Directory: Open your terminal and navigate to the root directory of your Playwright project.

  2. Run the Test Command: Use the following command to execute your tests:

    npx playwright test
    
  3. Specify a Specific Test File: If you want to run a specific test file, use:

    npx playwright test path/to/your/test-file.spec.ts
    
  4. Run Tests with a Specific Tag: You can tag your tests and run them selectively:

    npx playwright test --grep @tagName
    

  1. Configuring Test Options

Playwright allows you to configure various test options to customize the test execution process.

Configuration File:

  • playwright.config.ts: This file is used to set global configurations for your tests, such as browser settings, timeouts, and more.

Example Configuration:

import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  timeout: 30000,
  use: {
    headless: false,
    viewport: { width: 1280, height: 720 },
  },
  retries: 2,
};

export default config;

Explanation:

  • timeout: Sets the maximum time a test can run.
  • headless: Runs the browser in headless mode if set to true.
  • viewport: Defines the browser window size.
  • retries: Number of times to retry a failed test.

  1. Interpreting Test Results

After running your tests, you will receive output in the terminal that indicates the success or failure of each test.

Understanding the Output:

  • Passed Tests: Indicated by a green checkmark.
  • Failed Tests: Indicated by a red cross, along with an error message and stack trace.
  • Skipped Tests: Indicated by a yellow dash.

Example Output:

Running 3 tests using 1 worker

✓ test1.spec.ts:3:1 › should load the homepage
✖ test2.spec.ts:5:1 › should display error message
- test3.spec.ts:7:1 › should navigate to about page (skipped)

1 passed, 1 failed, 1 skipped

Practical Exercise

Exercise: Create a simple Playwright test that navigates to a website and checks for a specific element.

Task:

  1. Write a test that opens https://example.com.
  2. Verify that the page contains the text "Example Domain".

Solution:

import { test, expect } from '@playwright/test';

test('should display the correct page title', async ({ page }) => {
  await page.goto('https://example.com');
  const title = await page.title();
  expect(title).toBe('Example Domain');
});

Explanation:

  • page.goto: Navigates to the specified URL.
  • page.title: Retrieves the page title.
  • expect: Asserts that the title matches the expected value.

Conclusion

In this section, you learned how to run Playwright tests using the command line, configure test options, and interpret test results. These skills are fundamental for effective test automation. In the next module, we will delve deeper into writing tests in TypeScript, enhancing your ability to create robust and maintainable test suites.

© Copyright 2024. All rights reserved