Playwright Automation Tutorial: Step-by-Step Guide for Testers

March 26, 2025

In the world of automation testing, Playwright has emerged as a powerful tool that enables seamless web application testing. Developed by Microsoft, Playwright is an open-source automation framework that supports multiple browsers, including Chromium, Firefox, and WebKit. It allows testers to perform end-to-end testing with high efficiency, making it a preferred choice over Selenium and other automation tools.

In this Online Playwright automation tutorial, we will provide a step-by-step guide to getting started with Playwright, setting up the environment, writing test scripts, and executing automated tests. This tutorial is ideal for beginners and experienced testers looking to enhance their automation skills.

Online Playwright automation tutorial by Subba Raju Sir

Before we dive into the step-by-step tutorial, let’s explore why Playwright is gaining popularity among testers:

  1. Cross-Browser Testing: Supports Chromium, Firefox, and WebKit, ensuring compatibility across different browsers.
  2. Headless and Headful Execution: Allows running tests in headless mode for faster execution and headful mode for debugging.
  3. Auto-Wait Mechanism: Reduces flakiness by automatically waiting for elements to be ready.
  4. Multiple Programming Languages: Supports JavaScript, TypeScript, Python, C#, and Java.
  5. Parallel Execution: Enables faster test execution by running tests in parallel.
  6. Built-in Report Generation: Provides detailed reports with logs, traces, and screenshots.

Setting Up Playwright for Automation Testing

To start using Playwright, you need to set up your development environment. Follow these steps:

Step 1: Install Node.js

Playwright requires Node.js to run. Download and install the latest version from the official Node.js website.

To verify the installation, run:

node -v

npm -v

Step 2: Create a New Project

Open the terminal and create a new directory for your Playwright project:

mkdir playwright-test

cd playwright-test

Initialize a new Node.js project:

npm init -y

Step 3: Install Playwright

Use the following command to install Playwright along with the necessary browsers:

npm install @playwright/test

To verify the installation, run:

npx playwright –version

Step 4: Install Browsers

If you did not install browsers during setup, run:

npx playwright install

Writing Your First Playwright Test

Now that Playwright is installed, let’s write a simple test script to verify Google’s search functionality.

Step 1: Create a Test File

Inside your project directory, create a new file test.spec.js:

mkdir tests

cd tests

nano test.spec.js

Step 2: Write a Basic Test

Add the following code to the file:

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

test(‘Google search test’, async ({ page }) => {

await page.goto(‘https://www.google.com’);

await page.fill(‘input[name=”q”]’, ‘Playwright Automation’);

await page.keyboard.press(‘Enter’);

await page.waitForSelector(‘#search’);

const results = await page.locator(‘#search’);

expect(results).toBeVisible();

});

Step 3: Run the Test

Execute the test using:

npx playwright test tests/test.spec.js

You should see the test running successfully in the terminal.

Advanced Playwright Features

  1. Handling Alerts and Popups

test(‘Handle alerts’, async ({ page }) => {

page.on(‘dialog’, async dialog => {

console.log(`Dialog message: ${dialog.message()}`);

await dialog.accept();

});

await page.click(‘#alert-button’);

});

  1. Taking Screenshots

await page.screenshot({ path: ‘screenshot.png’ });

3. Generating Reports

Run tests with built-in reporting:

npx playwright test –reporter=html

 

Running Tests in Headless Mode

By default, Playwright runs tests in headless mode, but you can explicitly specify it:

const browser = await playwright.chromium.launch({ headless: true });

For debugging, you can run tests in headful mode:

npx playwright test –headed

Integrating Playwright with CI/CD

Playwright supports integration with CI/CD tools like Jenkins, GitHub Actions, and Azure DevOps. You can add Playwright to a CI pipeline using GitHub Actions:

name: Playwright Tests

on: [push]

jobs:

test:

runs-on: ubuntu-latest

steps:

– uses: actions/checkout@v2

– uses: actions/setup-node@v3

with:

node-version: ’16’

– run: npm install

– run: npx playwright install

– run: npx playwright test

Playwright vs. Selenium

 

Feature Playwright Selenium
Language Support JS, TS, Python, Java, C# JS, Python, Java, C#
Auto-Wait Yes No
Headless Mode Yes Yes
Parallel Execution Yes Limited
Cross-Browser Testing Yes Yes
Speed Faster Slower

 

Conclusion

In this Online Playwright automation tutorial, we covered the basics of Playwright automation testing, including setup, writing test scripts, handling alerts, taking screenshots, and integrating with CI/CD pipelines. Playwright’s advanced features, such as auto-wait, parallel execution, and cross-browser support, make it a powerful alternative to Selenium.

If you’re looking for hands-on training, QA Training Hub offers expert-led courses on Playwright automation testing to help you master end-to-end web testing.

Start your Playwright automation journey today and take your testing skills to the next level!

For more in-depth training, enroll in QA Training Hub’s Online Playwright automation tutorial today!

FAQ’s

  1. What is Playwright in automation testing?

Playwright is an open-source web automation framework by Microsoft that supports multiple browsers and programming languages for end-to-end testing.

  1. Why should I use Playwright over Selenium?

Playwright is faster, supports auto-wait mechanisms, runs tests in parallel, and provides better cross-browser compatibility than Selenium.

  1. Which programming languages does Playwright support?

Playwright supports JavaScript, TypeScript, Python, Java, and C#.

  1. How can I install Playwright?

You can install Playwright using npm:

bash

CopyEdit

npm install @playwright/test

  1. Can Playwright be used for mobile testing?

Yes, Playwright supports mobile emulation for Android and iOS devices.

  1. How does Playwright handle dynamic elements?

Playwright has an auto-wait feature that waits for elements to be available before interacting with them, reducing flakiness.

  1. What browsers does Playwright support?

Playwright supports Chromium, Firefox, and WebKit for cross-browser testing.

  1. Can Playwright be integrated with CI/CD pipelines?

Yes, Playwright can be integrated with Jenkins, GitHub Actions, and Azure DevOps for continuous testing.

  1. How do I run Playwright tests in headless mode?

Use the following command to run Playwright tests in headless mode:

bash

CopyEdit

npx playwright test –headless

  1. What is the difference between headless and headed mode in Playwright?
  • Headless Mode: Runs tests without a GUI for faster execution.
  • Headed Mode: Runs tests with a visible browser window for debugging.
  1. Can I capture screenshots using Playwright?

Yes, you can take screenshots with:

javascript

CopyEdit

await page.screenshot({ path: ‘screenshot.png’ });

  1. Does Playwright support API testing?

Yes, Playwright supports API testing using built-in methods like request.get() and request.post().

  1. How do I handle pop-ups and alerts in Playwright?

Use the page.on(‘dialog’, callback) method to handle alerts.

  1. What is Playwright’s auto-wait feature?

Auto-wait ensures elements are visible, enabled, and ready before interacting with them, reducing test failures.

  1. How can I write a test case in Playwright?

javascript

CopyEdit

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

test(‘Google search test’, async ({ page }) => {

await page.goto(‘https://www.google.com’);

await page.fill(‘input[name=”q”]’, ‘Playwright Automation’);

await page.keyboard.press(‘Enter’);

expect(await page.locator(‘#search’)).toBeVisible();

});

  1. How do I run Playwright tests in parallel?

Playwright runs tests in parallel by default. You can configure concurrency in playwright.config.js.

  1. Can Playwright be used for performance testing?

Yes, Playwright can measure page load times and network requests for performance testing.

  1. Is Playwright free to use?

Yes, Playwright is open-source and free to use.

  1. How do I debug Playwright tests?

Run tests in debug mode with:

bash

CopyEdit

npx playwright test –debug

  1. Where can I find an Online Playwright automation tutorial?

You can find a detailed Online Playwright automation tutorial at QA Training Hub, covering beginner to advanced topics.