What is @playwright/test?
The @playwright/test npm package is a framework for end-to-end testing that allows developers to automate browser interactions for testing web applications. It supports multiple browsers, provides a rich set of APIs for navigation, interaction, and assertions, and offers features like test parallelization, fixtures, and snapshot testing.
What are @playwright/test's main functionalities?
Browser Automation
Automate browser actions such as navigating to a URL, interacting with page elements, and validating page properties.
const { test, expect } = require('@playwright/test');
test('basic test', async ({ page }) => {
await page.goto('https://example.com');
const title = await page.title();
expect(title).toBe('Example Domain');
});
Cross-Browser Testing
Run tests across multiple browsers like Chromium, Firefox, and WebKit.
const { test } = require('@playwright/test');
test.describe.configure({ browsers: ['chromium', 'firefox', 'webkit'] });
test('cross-browser test', async ({ page }) => {
await page.goto('https://example.com');
// Perform cross-browser checks
});
Mobile Emulation
Emulate mobile devices to test responsive designs and touch interactions.
const { devices, test } = require('@playwright/test');
const iPhone11 = devices['iPhone 11 Pro'];
test('mobile emulation test', async ({ browser }) => {
const context = await browser.newContext({
...iPhone11,
});
const page = await context.newPage();
await page.goto('https://example.com');
// Perform actions in the emulated mobile environment
});
Visual Regression Testing
Capture screenshots and compare them against known good snapshots to detect visual regressions.
const { test, expect } = require('@playwright/test');
test('visual test', async ({ page }) => {
await page.goto('https://example.com');
expect(await page.screenshot()).toMatchSnapshot('homepage.png');
});
Test Fixtures
Create reusable test setup and teardown logic with fixtures.
const { test } = require('@playwright/test');
test('use fixture', async ({ myFixture }) => {
// Use the fixture in the test
});
test.extend({
myFixture: async ({}, use) => {
// Set up the fixture
await use('some value');
// Clean up the fixture
},
});
Other packages similar to @playwright/test
cypress
Cypress is a popular end-to-end testing framework similar to Playwright. It offers a rich interactive test runner and has a focus on ease of use. Unlike Playwright, Cypress only supports testing in a Chromium-based browser, which can be a limitation for cross-browser testing.
selenium-webdriver
Selenium WebDriver is one of the oldest and most widely used browser automation tools. It supports multiple programming languages and browsers. Compared to Playwright, Selenium tests tend to be slower and can be more flaky due to reliance on the WebDriver protocol.
puppeteer
Puppeteer is a Node library developed by the Chrome DevTools team. It provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Playwright is considered a successor to Puppeteer and extends its capabilities by supporting more browsers and additional features.
nightwatch
Nightwatch.js is an automated testing framework for web applications and websites, using the W3C WebDriver API. It is easy to use and set up. Compared to Playwright, Nightwatch may have less advanced features and browser support but is still a solid choice for many testing scenarios.
testcafe
TestCafe is a node.js tool for automating end-to-end web testing. It is known for its ease of setup and use, and it does not require WebDriver. Unlike Playwright, TestCafe runs tests written in JavaScript or TypeScript directly in the browser which can be both an advantage and a limitation depending on the context.
🎭 playwright-test
This package is not ready for use. If you want to run tests with playwright, checkout jest-playwright for Jest or karma-playwright-launcher for Karma.
A test runner for running tests with Playwright.
npm i -D @playwright/test
- Place unit tests in files ending with
.spec.*
.
import '@playwright/test';
import { it, expected } from '@playwright/test-runner';
it('is a basic test with the page', async ({page}) => {
await page.goto('https://playwright.dev/');
const home = await page.waitForSelector('home-navigation');
expect(await home.evaluate(home => home.innerText)).toBe('🎭 Playwright');
});
- Run all of your tests with
npx test-runner .