What Is the Playwright Framework?
As you already know, in AVS Solutions, we used a Protractor for automated end-to-end testing. However, since the Angular team deprecated the Protractor, we migrated our e2e test to Playwright.
Playwright Framework is an open-source tool developed by Microsoft that allows you to automate web browsers using a simple and powerful API.
Features of Playwright
- Multi-browser support: Playwright Framework supports multiple browsers, including Chrome, Firefox, Safari, and Microsoft Edge. This allows you to test your web application on different browsers and ensure it works correctly across all browsers.
- Cross-platform support: Playwright is designed to work on multiple platforms, including Windows, macOS, and Linux. This allows you to write tests on one platform and execute them on another.
- Reliable automation: Playwright provides reliable automation for end-to-end testing of web applications. Using a single automation API that works consistently across all browsers, Playwright Framework makes it easy to write and maintain tests.
- Easy setup: Playwright is easy to set up and get started with. It provides a simple CLI tool that helps you create a new project, install dependencies, and run tests.
- Built-in debugging: Playwright provides built-in debugging tools that make diagnostics and fixing issues with your tests easier. Use DevTools protocol to interact with your tests’ and debug them as needed.
Getting Started with Playwright Framework
To get started with the Playwright you’ll need to have Node.js installed on your machine. Once you have Node.js installed, you can install the Playwright by typing the following command in the terminal:
npm install playwright
Afterwards, the Playwright is installed, you can use the CLI tool to create a new project and generate a sample test file:
npx playwright init
This will create a new project with a sample test file to use as a starting point. You can then edit the test file to write your tests and run them using the following command:
npx playwright test
In addition, this will run your tests on all supported browsers and generate a report showing the results.
Playwright API
Probably the biggest advantage of Playwright Framework is its unified API which allows you to write code once and run it on multiple browsers consists of several key components:
- Browsers: Playwright provides a Browser class that represents a browser instance. You can use this class to launch a new browser, connect to an existing or launch a headless browser.
- Pages: Once you have a browser instance, you can create a Page object representing a web page. The Page class provides methods for navigating to URLs, interacting with page elements, and capturing screenshots.
- Elements: The playwright provides several classes for working with page elements. Some of these include the ElementHandle, Locator, and Waiter classes. These classes allow you to find and interact with the specific element/s on a page.
- Inputs: The Playwright provides a range of input methods for simulating user interactions, including typing text, clicking buttons, and selecting options from dropdown menus.
- Requests: Playwright provides a Request class that represents an HTTP request. This class can is used to intercept and modify network requests, simulate network errors, and test how your application handles different response codes. But more on that will come in another article.
- Events: Playwright provides a range of events that you can use to monitor and respond to changes in the browser or page state. For example, you can listen for events indicating when a page has finished loading or when a new browser window has been opened.
Example
import { chromium } from ‘playwright’;
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(‘https://www.example.com’);
await page.click(‘button’);
await browser.close();
}));
In the example above, we first launch a new Chromium browser using the ‘chromium.launch()’ method. We then create a new page object using the ‘browser.newPage()’ method and navigate to a web page using the ‘page.goto()’ method. Finally, we simulate a button click using the ‘page.click()’ method and close the browser using the ‘browser.close()’ method.
Playwright Framework Best Practices
Conclusion
Playwright is a new tool compared to Selenium. Therefore it has a smaller community coupled with fewer learning and troubleshooting resources available. Although its documentation is well-written, the level of detail might require a significant amount of time and effort to understand fully. Despite this, it can be an excellent tool for automating end-to-end testing of web applications. Firstly, it has multi-browser support, secondly cross-platform compatibility, thirdly reliable automation, and built-in debugging tools. Considering these advantages, I strongly encourage you to give the Playwright framework a try, when selecting an automation testing tool.