Skip to main content

Playwright Test vs. Playwright Library

· 4 min read
Avi Stramer

Playwright enables reliable end-to-end testing for modern web apps. Playwright provides two main tools: the Playwright Library for browser automation tasks and Playwright Test for end-to-end testing. While both tools leverage the same underlying technology, they serve distinct purposes and come with their unique set of features. In this blog post, we delve into a detailed comparison between Playwright Test and the Playwright Library, complete with practical code examples to showcase their capabilities.

What is Playwright Library?

Playwright Library is a comprehensive Node.js library for automating web browser interactions. It enables developers to programmatically control browsers to perform tasks such as navigation, form submission, and screenshot capture. One of the key strengths of the Playwright Library is its ability to support automation across Chrome, Firefox, Webkit (the engine within Safari), and Edge with a consistent API.

Example Code for Playwright Library:

const { chromium } = require('playwright');

(async () => {
// Launch the browser
const browser = await chromium.launch();
const page = await browser.newPage();

// Navigate to a webpage
await page.goto('https://example.com');

// Take a screenshot
await page.screenshot({ path: 'example.png' });

// Close the browser
await browser.close();
})();

This simple example demonstrates how the Playwright Library can be used to navigate to a webpage and take a screenshot, highlighting its utility for a wide range of automation tasks beyond testing.

What is Playwright Test?

Playwright Test extends the functionality of the Playwright Library into a full-featured test runner designed specifically for end-to-end testing. It is designed for testing web applications across various browsers and environments, offering features like parallel test execution, snapshot testing, and test retries.

Example Code for Playwright Test:

playwright.config.ts Configuration:

import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
// Define the test directory
testDir: './tests',

// Specify browsers for testing
projects: [
{ name: 'Chromium', use: { browserName: 'chromium' } },
{ name: 'Firefox', use: { browserName: 'firefox' } },
{ name: 'Webkit', use: { browserName: 'webkit' } },
],

// Global timeout and retries
timeout: 30000, // 30 seconds
retries: 2,

// Output directory and video recording
outputDir: './test-results/',
use: { video: 'on-first-retry' },
};

export default config;

Test Spec:

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

test('example test - verify webpage title', async ({ page }) => {
// Navigate and verify title
await page.goto('https://example.com');
await expect(page).toHaveTitle('Example Domain');
});

This example showcases a test that navigates to a webpage and verifies its title. The test framework configuration file indicates that you want to run each test across Chromium, Webkit, and Firefox with 2 retries if there are any failures. The Playwright runner then executes the tests via npx playwright test and writes a report into the ./test-results directory.

Comparison and Conclusion

While Playwright Library is a versatile tool for various browser automation tasks, Playwright Test is specifically designed for end-to-end testing, offering a more structured and feature-rich environment for testing web applications across multiple browsers and platforms.

The choice between Playwright Test and the Playwright Library ultimately depends on your specific needs. If your focus is on automating browser interactions for tasks beyond testing, such as web scraping or automating repetitive tasks, Playwright Library offers the flexibility and control you need. Conversely, if you're looking to streamline your end-to-end testing process with a tool that offers parallel test execution, snapshot testing, and comprehensive reporting, Playwright Test is the optimal choice.

Whether you're a developer or a QA engineer, understanding the capabilities and use cases of both Playwright Test and the Playwright Library can help you leverage the right tool for your web automation and testing needs, ensuring your applications perform flawlessly across all platforms and browsers.

Testable's Cloud Platform can help you automate, execute, report, analyze and collaborate on both Playwright Library and Playwright Test suites with a range of value added features on top of the open source libraries. Check out our site for more details.