Setting up playwright for browser testing

Key takeaways:

  • Playwright is an open-source tool for automated browser testing with cross-browser compatibility.

  • This supports major browsers (Chromium, Firefox, WebKit) and multiple platforms (Windows, macOS, Linux).

  • This offers robust APIs for complex interactions like filling forms and page navigation.

  • This enables parallel test execution, improving test speed and efficiency.

  • Installation requires Node.js and initializing a Node.js project for the Playwright setup.

  • Tests can be created by writing scripts in .spec.js files using test and expect functions from Playwright.

  • Basic tests include navigating to a URL and verifying page elements, such as page titles.

  • Playwright’s features allow for reliable and efficient browser testing, ensuring quality web applications.

Automated browser testing is a vital component of modern software development workflows, ensuring that web applications function correctly across different browsers and devices. Playwright, a powerful open-source automation tool, simplifies the process of writing and executing browser tests. This guide provides a comprehensive overview of setting up Playwright and creating your first test script.

Why choose a Playwright?

Playwright offers several advantages for automated browser testing:

  1. Cross-browser compatibility: Playwright supports multiple browsers, including Chromium, Firefox, and WebKit, enabling comprehensive testing across different browser engines.

  2. Multi-platform support: It runs on Windows, macOS, and Linux, allowing developers to test applications across various operating systems.

  3. Robust automation: Playwright provides a rich set of APIs for interacting with web pages, enabling complex interactions such as filling forms, clicking elements, and navigating between pages.

  4. Parallel execution: Playwright allows tests to run concurrently, speeding up test execution and improving efficiency.

Setup instructions

Follow these steps to set up Playwright and prepare your environment for automated browser testing:

  1. Install Node.js: Ensure that Node.js is installed on your system. You can download and install Node.js from the official websitehttps://nodejs.org/.

  2. Create a new project: Create a new directory for your Playwright project and navigate into it using the terminal:

mkdir my-playwright-project
cd my-playwright-project
  1. Initialize a Node.js project: Initialize a new Node.js project by running the following command and following the prompts:

npm init -y
  1. Install Playwright: Install the Playwright package as a development dependency in your project:

npm install @playwright/test --save-dev
  1. Write your first test: Create a new test file, such as example.spec.js, and start writing your test script. Here’s a basic example to get started:

const { test, expect } = require('@playwright/test');

test('Basic Test', async ({ page }) => {
  await page.goto('https://example.com');
  const pageTitle = await page.title();
  expect(pageTitle).toContain('Example Domain');
});
Automated testing with Playwright: Verifying page title on Example Domain

For the output, please refer to the terminal tab and ignore the output tab and app link.

This test navigates to the specified website and verifies that the page title contains the expected text.

  • Line 1: Imports the test and expect functions from the @playwright/test library. These functions are essential for creating and running tests in Playwright.

  • Line 3: Defines a test named ‘Basic Test’ using the test function provided by the Playwright. It takes an asynchronous function as its second argument, which receives an object { page }. This object represents the web page that will be interacted with during the test.

  • Line 4: Instructs the page object to navigate to the URL https://example.com. The goto method navigates the browser to the specified URL.

  • Line 5: Retrieves the title of the current web page using the title method of the page object. The title method returns a promise that resolves to the title of the page.

  • Line 6: Uses the expect function to make an assertion about the pageTitle. It checks whether the pageTitle contains the string 'Example Domain'. If the assertion fails, the test will fail, indicating that the page title does not match the expected value.

Conclusion

By following these setup instructions, you can quickly get started with Playwright and automate browser testing for your web applications. With its robust features and ease of use, Playwright empowers developers to build reliable and high-quality web experiences.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to run the Playwright test in the Edge browser?

Install Playwright, then configure the test using the BrowserType chromium with the Edge channel. For example:

const { chromium } = require('playwright');
const browser = await chromium.launch({ channel: 'msedge' });

Is Playwright faster than Selenium?

Yes, Playwright is generally faster due to its modern architecture and support for headless execution, making it lightweight and efficient.


Which browser is not supported by Playwright?

Internet Explorer is not supported by Playwright.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved