As shifting testing left in the software development lifecycle (SDLC) gains popularity, the number of testing frameworks is increasing. One of these is Playwright, which developers use for its fast and reliable ability to quickly write and execute end-to-end tests across multiple languages, mobile web applications, and platforms.
Playwright is an open-source, JavaScript-based test automation framework developed and maintained by Microsoft. The Playwright library, which is compatible with Windows, macOS, and Linux, provides cross-browser automation for testing Chromium (Google Chrome, Microsoft Edge), Mozilla Firefox, and WebKit (Apple Safari) through a single, high-level API. Playwright is useful for developers who need to work with multiple programming languages because of its testing compatibility with Python, C# .NET, Node.js, and Java, in addition to JavaScript. Playwright can handle both desktop and mobile testing and supports all major browsers.
Although Playwright just debuted in 2020 and doesn’t yet offer as many integrations as some other automation platforms, it’s generally considered to be as developer-friendly as more mature frameworks like Selenium and Appium. Here are some reasons why:
Speed and coverage: Playwright can be used to test complex applications across multiple languages, mobile web apps, and platforms.
Scalability: Playwright can run tests at scale on a preconfigured infrastructure or locally on a container.
Integrations: Playwright integrates with a range of continuous integration/continuous delivery (CI/CD) tools, including Jenkins, CircleCI, and GitLab. It also works well with JavaScript testing frameworks other than JavaScript, including Jest, Jasmine, and Mocha.
Enhanced test accuracy: Playwright includes an auto-wait function that repeats the relevant checks on elements and requested actions until they all become actionable.
Parallel testing: Playwright can run simultaneous, or parallel, tests for multiple web pages. This allows Playwright to create a browser context for each test—like creating new, individual browser profiles. Testing in parallel provides full test isolation of the individual web pages that can help scale up testing.
As you prepare to use Playwright, it’s important to know that your test automation framework needs to be sufficiently robust to run each time code is committed and to provide tools for debugging.
You can speed up testing and unify results with a containerized solution such as saucectl, which has preconfigured container (Docker) images and virtual machines (VMs) for each framework – including Playwright. These can be leveraged to run tests locally or in the cloud at scale. No matter how you test, test assets are sent to the Sauce Cloud for easy debugging with screenshots, videos, logs, and historical insights.
Python is by far the most utilized non-JavaScript programming language. According to the 2021 State of JavaScript survey of more than 16 thousand developers, the largest group of respondents among 20 programming languages, 24.8%, use Python. If you are a Python programmer, you can use the Playwright Pytest plugin to write end-to-end tests or use the library to manually write the testing infrastructure with a test runner like saucectl. You can also use the library to manually write the testing infrastructure with saucectl.
You can use Playwright to test your web apps either locally in Docker or remotely on Sauce Labs using the saucectl CLI. This gives you the flexibility to run your tests in the environment that best suits your organization, while still benefiting from thousands of device/browser/OS combinations and Sauce Labs analytics. Additionally, saucectl relies on a YAML specification file to determine exactly which tests to run and how to run them. You can customize saucectl to run your Playwright tests by modifying the properties of the YAML file.
saucectl helps mitigate some of the issues that can occur when software teams use multiple test frameworks because it orchestrates the relationship between your Playwright tests and the rich parallelization, test history filtering, and analytics of the Sauce Labs Continuous Testing Cloud. saucectl performs the underlying business logic to access the tests in Playwright, runs them (either in the Sauce Labs Cloud or locally in a Docker image), then securely transmits the test assets to the Sauce Labs platform, where you can review, share, and evaluate your test outcomes at scale.
saucectl helps software teams incorporate their Playwright tests earlier in the SDLC to speed up delivery and increase product quality. By covering Playwright in this way, Sauce Labs makes automated testing more accessible and easier for different teams to deliver releases with more confidence in less time.
To get started, use our Playwright Quickstart Guide to learn how to use saucectl to run Playwright tests directly from your existing Playwright project. If you don't have Playwright tests but want to try, the Playwright Demo Repo includes a sample project structure, working configuration file, and sample Playwright test so you can get up and running in less than 10 minutes.
Below are two examples of tests built using Playwright. These instances are from the playwright.dev documentation site.
Example of a Playwright test in JavaScript with built-in auto-wait functionality where conditions must be met before performing the requested action:
1// @ts-check2const { test, expect } = require("@playwright/test");3test("homepage has Playwright in title and get started link linking to the intro page", async ({4page,5}) => {6await page.goto("https://playwright.dev/"); // Expect a title "to contain" a substring.7await expect(page).toHaveTitle(/Playwright/); // create a locator8const getStarted = page.locator("text=Get Started"); // Expect an attribute "to be strictly equal" to the value.9await expect(getStarted).toHaveAttribute("href", "/docs/intro"); // Click the get started link.10await getStarted.click(); // Expects the URL to contain intro.11await expect(page).toHaveURL(/.*intro/);12});
Example of Playwright application test with Python: The file is created inside the current working directory or in a sub-directory with the code:
1import re2from playwright.sync_api import Page, expect34def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(5page: Page, foo6):7page.goto("https://playwright.dev/")89# Expect a title "to contain" a substring.10expect(page).to_have_title(re.compile("Playwright"))1112# create a locator13get_started = page.locator("text=Get Started")1415# Expect an attribute "to be strictly equal" to the value.16expect(get_started).to_have_attribute("href", "/docs/intro")1718# Click the get started link.19get_started.click()2021# Expects the URL to contain intro.22expect(page).to_have_url(re.compile(".*intro"))