Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
@playwright/test
Advanced tools
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.
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
},
});
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 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 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.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 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.
š§ This project is under active development and is not ready for serious use.
Zero config cross-browser end-to-end testing for web apps. Browser automation with Playwright, Jest-like assertions and support for TypeScript.
npm i -D @playwright/test
Create foo.spec.ts
(or foo.spec.js
) to define your test. Playwright provides a page
argument to the test function.
// tests/foo.spec.ts
const { it, expect } = require('@playwright/test');
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.innerText()).toBe('š Playwright');
});
The test runner provides browser primitives as arguments to your test functions. Tests can use one or more of these primitives.
page
: Instance of Page. Each test gets a new isolated page to run the test.context
: Instance of [BrowserContext][browser-context]. Each test gets a new isolated context to run the test. The page
object belongs to this context.browser
: Instance of Browser. Browsers are shared across tests to optimize resources. Each worker process gets a browser instance.Use test fixtures to customize or create new function arguments.
Use it
and describe
to write test functions.
const { it, describe } = require('@playwright/test');
describe('feature foo', () => {
it('is working correctly', async ({ page }) => {
// Test function
// ...
})
});
To run a single test use fit
or it.only
. To skip a test use xit
or it.skip
. Use test annotations to mark tests as slow, flaky or fixme.
The test runner provides the expect package for assertions. See expect API reference.
Tests can be run on single or multiple browsers and with flags to generate screenshot on test failures.
# Run all tests across Chromium, Firefox and WebKit
npx folio
# Run tests on a single browser
npx folio --param browserName=chromium
# Run all tests in headful mode
npx folio --param headful
# Take screenshots on failure
npx folio --param screenshotOnFailure
# See all options
npx folio --help
Test runner CLI can be customized with test parameters.
The context
argument is an instance of [BrowserContext][browser-context]. Browser contexts are isolated execution environments that can host multiple pages.
const { it } = require('@playwright/test');
it('tests on multiple web pages', async ({ context }) => {
const pageFoo = await context.newPage();
const pageBar = await context.newPage();
// Test function code
});
See multi-page scenarios for more examples.
Define a custom argument that mocks networks call for a browser context.
const { it, fixtures } = require('@playwright/test');
fixtures.mockedContext.init(async ({ context }, runTest) => {
context.route(/.css/, route => route.abort());
runTest(context);
});
it('loads pages without css requests', async ({ mockedContext }) => {
const page = await mockedContext.newPage();
await page.goto('https://stackoverflow.com');
// Test function code
});
Override default options for creating a BrowserContext to use mobile emulation.
const { it, fixtures } = require('@playwright/test');
const { devices } = require('playwright');
fixtures.defaultContextOptions.override(async ({}, runTest) => {
await runTest({ ...devices['iPhone 11'] });
});
it('uses mobile emulation', async ({ context }) => {
// Test function code
});
FAQs
A high-level API to automate web browsers
We found that @playwright/test demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Ā It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.