Playwright Automation Framework: Tutorial | BrowserStack

August 2024 · 8 minute read

Test automation has become significant in SDLC with the rising adoption of test automation in Agile teams. As the scope increases, new tools for test automation are emerging in the market. Selenium and other proprietary tools were ruling the market; however, new open-source tools are now leading the market. 

Playwright is an addition to the many Test Automation Frameworks available in the market and shares a similar syntax to Puppeteer, but it has overcome many limitations present in Puppeteer. Let’s explore more in the following sections with this detailed Playwright tutorial.

Table of Contents

What is Playwright?

Playwright is an open-source test automation library initially developed by Microsoft contributors. It supports programming languages like Java, Python, C#, and NodeJS. Playwright comes with Apache 2.0 License and is most popular with NodeJS with Javascript/Typescript. This Playwright tutorial will help set up NodeJS using Visual Studio Code.

Why choose Playwright Automation?

Though Playwright is new to the market, we can hardly list the limitations as it supports multiple languages. People who want to migrate from Selenium to Playwright can do it quickly since Playwright support C#,  Java, and Python. Programming languages are not a barrier. Playwright’s first release was in January 2020, and it has gained much popularity ever since.

Growing Popularity of Playwright Framework

As of 2023, the current snapshot of Microsoft Playwright on Github:

NPM Trends of Playwright Framework:

Advantages of Playwright Automation

Playwright contributors are highly active in releasing new features every month, which are listed below:

  • Easy Setup and Configuration: Being a Test Automation Framework, it just needs a configuration as the installation doesn’t take much time. Depending on the language we use with Playwright, the installation steps might change
  • Multi-Browser Support: Chromium family browsers (Chrome, Edge), Webkit (Safari), and Firefox are all supported.
  • Multi-Language Support: Playwright supports Java, C#, Python, Javascript/ Typescript making it a popular choice. Most of the modern open-source test automation frameworks miss this feature.
  • Types of Testing: Playwright supports Functional, End to End, and API Testing. With a third-party plugin, Playwright can be integrated with Accessibility Testing.
  • Parallel Browser Testing: Playwright also supports the execution of simultaneous tests (AKA parallel testing) through Browser Context and can run parallel tests with multiple browsers. This scales up testing and comes in handy when multiple web pages must be tested simultaneously.
  • Support for Multiple Tab/Browser Window: Playwright supports multi-tab and multi-window. Some test cases must verify the scenario by launching a new window and returning to the parent window. Playwright supports all different types of test cases.
  • Built-in Reporters: Playwright framework, by default, comes with many valuable reporters like List, Dot, Line, JSON, JUnit, and HTML Reporters. The exciting part is that with Playwright, one can create custom reporters. Playwright also supports third-party reporter Allure Report.
  • Typescript Support out of the box: Zero configuration is required for typescript language support as it understands your typescript and javascript code.
  • CI/CD Integration Support: Playwright supports CI/CD integration. It even provides docker images for some language bindings.
  • Debugging Tools Support: Playwright testing supports different debugging options making it developer-friendly. Some debugging options are Playwright Inspector, VSCode Debugger, Browser Developer Tools, and Trace Viewers Console Logs. 
  • Other notable features of Playwright include:

    Limitations of Playwright Automation

  • Playwright is new, and it’s still evolving—scope for improvement.
  • No Support for IE11
  • Playwright doesn’t support Native Mobile Apps
  • Though Playwright documentation is excellent, we cannot expect much regarding community support. Since Playwright is new to the market user base is still growing.
  • Getting Started with Playwright Setup

    This Playwright tutorial explains how to set up with NodeJS easily.

    Pre-Requisites: 

  • Install Visual Studio Code: Download and Install Visual Studio Code(VSCode).
  • Install NodeJS: Download and Install Node JS
  • How to Install and run Playwright Test Script

    Step 1: Create a fresh new directory (ex: PlaywrightDemo) in VSCode

    Step 2: Open Directory in Visual Studio Code. From VS code,

    Click on File > Open Folder > Choose newly Created Folder (PlaywrightDemo)

    Step 3: From the VS Code, Click on Terminal Menu > Click on New Terminal 

    Step 4: Enter the below command to start the Playwright installation

    npm init playwright@latest

    Note: The above command asks a set of questions. Please provide appropriate inputs. In this tutorial, we are using typescript language.

    The above command does the following operation:

    Folder structure for Playwright Tutorial

    Step 5: Install Browsers

    However, Playwright is configured to run on existing browsers, which might create issues while running tests, so it is recommended to use the Playwright browsers. Using the below command, you can install all different browsers in Playwright.

    npx playwright install

    The above command installs the Playwright version of  Chromium, Firefox, and Webkit browser.

    Install Browsers to setup Playwright

    Step 6: Create the first Playwright test

    Navigate inside the tests folder and create a test spec file ex: demo.spec.ts

    Let’s start a test case with the below scenario

    Scenario: 

    The demo.spec.ts is our Playwright test script as follows 

    //demo.spec.ts import { test, expect, Page } from '@playwright/test'; test.beforeEach(async ({ page }) => {   await page.goto('https://www.browserstack.com/'); }); test.describe('Demo Test', () => {     test('Verify Login Error Message', async ({ page }) => {         await page.waitForSelector('text=Sign in',{state:'visible'});         await page.locator('text=Sign in').first().click();         await page.waitForSelector('#user_email_login')         await page.locator('#user_email_login').type('example1@example.com');         await page.locator('#user_password').type('examplepassword');         await page.locator('#user_submit').click();         const errorMessage = await (await page.locator("//input[@id='user_password']/../div[@class='error-msg']").textContent()).trim();         console.log("Browserstack Login Error Message: "+errorMessage);         expect(errorMessage).toBe('Invalid password');     }); });

    Step 7: Execute Playwright Test Script

    As discussed above, during installation, Playwright creates playwright.config.ts (playwright.config.ts is the global configuration file) which will have some settings. By default, the global configuration contains the value to run on all the browsers. It runs on all three different browsers when you execute the Playwright test. We don’t need it, so we need to remove that option.

    If you are finding to do the above, copy and paste the below line of code to playwright.config.ts

    //playwright.config.ts import type { PlaywrightTestConfig } from '@playwright/test'; const config: PlaywrightTestConfig = {   testDir: './tests',   timeout: 30 * 1000,   expect: {     timeout: 5000   },   reporter: 'html',   use: {     actionTimeout: 0,     trace: 'on-first-retry',   }, }; export default config;

    So, you have configured the playwright.config.ts, we are all set to execute the first Playwright test script.

    Execute Playwright Spec using the below command

    npx playwright test  demo.spec.ts –headed

    Let’s understand the above command:

    Once you execute the above command, the Playwright tests start executing in the Chromium browser.

    Playwright Framework Tutorial

    Step 8: View the report

    In our playwright.config.ts, we specify the HTML report, so after executing the Playwright test, the HTML report gets generated automatically in the playwright-report folder.

    To view generated HTML report simply type:

    npx playwright show-report

    View Report in Playwright Framework Example

    Playwright Testing on BrowserStack

    One can run all your playwright-test tests on BrowserStack as well. Follow the steps below to run the sample tests in this repository:

  • Clone this repository using git clone https://github.com/browserstack/playwright-browserstack.git  
  • Go inside the directory playwright-test using cd playwright-test
  • Install the dependencies using npm install
  • Put in your credentials in the file fixtures.js in the caps part.
  • To run your tests on BrowserStack, you must ensure you correctly configured the projects in the playwright.config.js file.
  • Run the sample test script using the npm test, which runs all the tests inside the tests directory across five browsers in BrowserStack.
  • If you want to test against localhost or other private websites, run the sample test script using npm run test:local, which runs all the tests inside the tests directory across five browsers in BrowserStack.
  • To run your tests locally, you must configure the projects without the name @browserstack in playwright.config.js file.
  • For complete details, visit the Github page.

    To conclude,

    Playwright is a solid test automation framework that allows you to perform end-to-end testing across major browsers. It is equipped with multiple features, such as resiliency, auto-wait, capturing test trace, and so on, supported with BrowserStack.

    Run hundreds of Playwright tests in parallel in minutes. Focus on testing – not test infrastructure – with zero setups and maintenance.

    Run Playwright Tests

    ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6po5qxp6e2qLTTZquurJ%2BntqK4