Playwright JavaScript Examples: Complete Beginner-to-Advanced Guide with Real Automation Scenarios

Introduction

If you’re learning browser automation, one of the best ways to master Playwright is through practical coding examples. This guide on Playwright JavaScript Examples covers everything from your first automation script to advanced enterprise testing techniques.

Playwright is Microsoft’s modern automation framework for testing web applications across Chromium, Firefox, and WebKit. Combined with JavaScript, it enables developers and QA engineers to write reliable, maintainable, and fast automation tests.

In this article, you’ll find Playwright JavaScript examples for beginners, complete runnable code, real-world automation scenarios, interview-focused explanations, and best practices for building production-ready automation frameworks.


Why Use Playwright with JavaScript?

JavaScript is the native language of Playwright, making it an excellent choice for browser automation.

Benefits

  • Easy to learn
  • Excellent documentation
  • Fast execution
  • Cross-browser testing
  • Built-in auto-waiting
  • API testing support
  • Parallel execution
  • HTML reporting
  • Large JavaScript ecosystem

Playwright vs Selenium

FeaturePlaywrightSelenium
Auto-waitingBuilt-inManual waits often required
Browser supportChromium, Firefox, WebKitMultiple browsers
Parallel executionBuilt-inRequires additional configuration
API testingBuilt-inExternal libraries needed
Trace ViewerYesNo built-in support

Prerequisites (Node.js, npm, VS Code)

Before running these Playwright JavaScript code examples, install:

  • Node.js (LTS recommended)
  • npm
  • Visual Studio Code
  • Git (optional)

Verify your installation:

node -v

npm -v


Installing Playwright

Create a new project:

mkdir playwright-js-demo

cd playwright-js-demo

npm init -y

Install Playwright:

npm init playwright@latest

This command:

  • Installs Playwright
  • Downloads browser binaries
  • Creates sample tests
  • Generates a Playwright configuration file

Project Structure for JavaScript

A clean project structure improves maintainability.

playwright-js-project

├── pages

├── tests

├── fixtures

├── utils

├── test-data

├── screenshots

├── reports

├── traces

├── playwright.config.js

├── package.json

└── .env

Folder Purpose

FolderDescription
pagesPage Object Model classes
testsTest files
utilsReusable helper functions
fixturesShared setup and teardown
reportsHTML reports
screenshotsFailure screenshots
tracesTrace Viewer files

Your First Playwright JavaScript Test

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

test(‘Verify homepage title’, async ({ page }) => {

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

    await expect(page).toHaveTitle(‘Example Domain’);

});

Explanation

This test:

  1. Opens a browser.
  2. Navigates to the website.
  3. Waits for the page to load.
  4. Verifies the page title.
  5. Closes automatically after execution.

Run the test:

npx playwright test


Playwright Locators with Examples

Choosing stable locators is essential for reliable automation.

getByRole()

await page.getByRole(‘button’, {

    name: ‘Login’

}).click();

Best for buttons, links, and accessible elements.


getByLabel()

await page.getByLabel(‘Email’)

    .fill(‘user@example.com’);

Perfect for forms with associated labels.


getByText()

await page.getByText(‘Sign Out’)

    .click();

Useful for visible text elements.


locator()

await page.locator(‘.product-card’)

    .first()

    .click();

Flexible for CSS-based selection.


getByTestId()

await page.getByTestId(‘submit-button’)

    .click();

Ideal when developers provide stable test IDs.

Recommended Locator Order

  1. getByRole()
  2. getByLabel()
  3. getByPlaceholder()
  4. getByText()
  5. getByTestId()
  6. locator()
  7. XPath (only when necessary)

Assertions and Auto-Waiting Examples

Playwright automatically waits until elements are ready.

await expect(page)

    .toHaveURL(/dashboard/);

await expect(

    page.getByRole(‘heading’, {

        name: ‘Dashboard’

    })

).toBeVisible();

Why Auto-Waiting Matters

Playwright waits until elements are:

  • Visible
  • Stable
  • Enabled
  • Ready for interaction

This significantly reduces flaky tests.


Handling Forms, Alerts, Frames, Windows, and File Uploads

Login Form

await page.getByLabel(‘Username’)

    .fill(‘admin’);

await page.getByLabel(‘Password’)

    .fill(‘password’);

await page.getByRole(‘button’, {

    name: ‘Login’

}).click();


JavaScript Alert

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

    console.log(dialog.message());

    await dialog.accept();

});


Working with Frames

const frame = page.frameLocator(‘#payment-frame’);

await frame.getByLabel(‘Card Number’)

    .fill(‘4111111111111111’);


Multiple Tabs

const pagePromise = context.waitForEvent(‘page’);

await page.getByText(‘Open New Tab’).click();

const newPage = await pagePromise;

await newPage.waitForLoadState();


File Upload

await page.locator(‘input[type=file]’)

    .setInputFiles(‘documents/resume.pdf’);


Screenshots, Videos, and Trace Viewer Examples

Screenshot

await page.screenshot({

    path: ‘screenshots/homepage.png’

});


Configure Tracing

use: {

    screenshot: ‘only-on-failure’,

    video: ‘retain-on-failure’,

    trace: ‘retain-on-failure’

}

Why Use Trace Viewer?

Trace Viewer records:

  • Screenshots
  • DOM snapshots
  • Console logs
  • Network requests
  • Timeline of user actions

It is one of the most effective tools for debugging failed tests.


API Testing with Playwright

Playwright supports API testing alongside UI automation.

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

test(‘Verify API’, async ({ request }) => {

    const response = await request.get(

        ‘https://jsonplaceholder.typicode.com/posts/1’

    );

    expect(response.ok()).toBeTruthy();

});

Combined Workflow

API Login

     │

     ▼

Receive Token

     │

     ▼

Open Browser

     │

     ▼

Verify Dashboard

Using API calls for setup can reduce UI execution time and simplify end-to-end tests.


Page Object Model (POM) Example

LoginPage.js

class LoginPage {

    constructor(page) {

        this.page = page;

    }

    async login(username, password) {

        await this.page.getByLabel(‘Username’)

            .fill(username);

        await this.page.getByLabel(‘Password’)

            .fill(password);

        await this.page.getByRole(‘button’, {

            name: ‘Login’

        }).click();

    }

}

module.exports = { LoginPage };

Benefits

  • Cleaner tests
  • Reusable methods
  • Easier maintenance
  • Better scalability

Parallel Execution and Cross-Browser Testing

Configure parallel execution:

module.exports = {

    workers: 4

};

Enable multiple browsers:

projects: [

    { name: ‘Chromium’ },

    { name: ‘Firefox’ },

    { name: ‘WebKit’ }

]

Workflow

Tests

 │

 ├── Chromium

 ├── Firefox

 └── WebKit

      │

      ▼

 Consolidated Report


Real-Time Automation Project Examples

1. Login Automation

  • Open application
  • Enter username
  • Enter password
  • Verify dashboard

2. Registration Form

  • Fill all fields
  • Upload profile image
  • Submit form
  • Validate confirmation message

3. E-commerce Search

await page.getByPlaceholder(‘Search’)

    .fill(‘Laptop’);

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

await expect(

    page.locator(‘.product-card’)

).toHaveCount(10);


4. Table Validation

const rows = page.locator(‘table tbody tr’);

await expect(rows)

    .toHaveCount(5);


5. File Download

const downloadPromise = page.waitForEvent(‘download’);

await page.getByText(‘Download’).click();

const download = await downloadPromise;


6. Responsive Testing

await page.setViewportSize({

    width: 390,

    height: 844

});

This simulates a mobile-sized viewport for responsive UI validation.


Enterprise Best Practices

To build a scalable Playwright JavaScript automation framework:

  • Use the Page Object Model.
  • Prefer semantic locators.
  • Keep tests independent.
  • Externalize environment variables using .env.
  • Store reusable utilities separately.
  • Enable screenshots, videos, and traces for failures.
  • Integrate with GitHub Actions or Jenkins.
  • Use HTML reporting.
  • Review flaky tests regularly.
  • Write descriptive test names.

Common Errors and Troubleshooting

ProblemSolution
Browser not installedRun npx playwright install
Element not foundVerify locator strategy and page state
Flaky testsRely on auto-waiting instead of fixed delays
File upload failsConfirm file path exists
Frame interaction failsUse frameLocator() correctly
API authentication errorVerify request headers and tokens

Troubleshooting Workflow

Test Failure

      │

      ▼

Check Locator

      │

      ▼

Review Trace

      │

      ▼

Inspect Logs

      │

      ▼

Fix & Re-run


Playwright JavaScript Interview Questions

  1. What is Playwright?
  2. Why use Playwright with JavaScript?
  3. How do you install Playwright?
  4. What is the Playwright Test runner?
  5. What are semantic locators?
  6. Why use getByRole()?
  7. What is auto-waiting?
  8. How do assertions work?
  9. What is the Page Object Model?
  10. How do you handle frames?
  11. How do you upload files?
  12. How do you download files?
  13. How do you handle multiple tabs?
  14. How do you capture screenshots?
  15. What is Trace Viewer?
  16. How do you perform API testing?
  17. How do you run tests in parallel?
  18. How do you enable cross-browser testing?
  19. How do you integrate Playwright with GitHub Actions?
  20. How do you integrate with Jenkins?
  21. How do you debug failed tests?
  22. How do you organize an enterprise framework?
  23. How do you manage environment variables?
  24. What are common Playwright best practices?
  25. How would you explain your automation framework in an interview?

FAQs

Is Playwright JavaScript suitable for beginners?

Yes. Playwright has a clean API, built-in auto-waiting, and excellent documentation, making it approachable for beginners.

Can I use JavaScript instead of TypeScript?

Absolutely. Playwright fully supports JavaScript. TypeScript adds static type checking, which can be helpful for larger projects.

Does Playwright support API testing?

Yes. Playwright includes a built-in API testing client, allowing you to combine API and UI testing in the same project.

What is the best locator strategy?

Prefer semantic locators such as getByRole() and getByLabel() whenever possible. They tend to be more stable and reflect how users interact with the application.

Can Playwright run tests in parallel?

Yes. Playwright supports parallel execution and cross-browser testing out of the box.

Is Playwright suitable for enterprise automation?

Yes. Features such as reporting, tracing, parallel execution, CI/CD integration, and Page Object Model support make it a strong choice for enterprise web automation.

Leave a Comment

Your email address will not be published. Required fields are marked *