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 ContentsWhat 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.
- It leverages the DevTools protocol to write powerful, stable automated tests.
- It can see into and control the browser rather than relying on a middle translation layer; it allows for the simulation of more insightful and relevant user scenarios.
Growing Popularity of Playwright Framework
As of 2023, the current snapshot of Microsoft Playwright on Github:
- Active Users: 23.2k
- Forks (copy of a repository that you manage): 2.4k
- Releases: 94 (This shows how active they are in fixing and releasing new features)
NPM Trends of Playwright Framework:
- Weekly Downloads: 1,244,085
Advantages of Playwright Automation
Playwright contributors are highly active in releasing new features every month, which are listed below:
Other notable features of Playwright include:
- iframe Support
- Support for Page Object Model
- Built-in Reporters
- Page object pattern
- Cross-Origin Navigation Support
- Selectors Support
- Shadow DOM
- Automatic Waiting
- Third-Party Test Runner Supports
- Videos and Screenshot
- Browser Emulation
- Test Retry,
- Parameterized Project, etc.
Limitations of Playwright Automation
Getting Started with Playwright Setup
This Playwright tutorial explains how to set up with NodeJS easily.
Pre-Requisites:
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:
- Creates Package.json
- Installs npm library
- Sets up basic Files and Folders
- tests folder: This folder contains actual test scripts. By default, an example.spec.ts file will be created inside this folder.
- .gitignore: This file helps if you are using git repository
- package.json and package-lock.json: This file helps to track dependencies, create a shortcut for running tests, etc.
- playwright.config.ts: This is the global configuration file for the Playwright, which you can configure with available options.
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.
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.
- Navigate to playwright.config.ts. Comment the option which starts with projects: [
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:
- We are specifying which test file we need to run i.e. demo.spec.ts. Do not mention any spec file name if you want to run all the spec files inside the tests folder, do not mention any spec file name.
- Playwright runs in headless mode by default, so we specify the –headed to run on headed mode.
- We are not mentioning any browser name; by default, the Playwright test runs in the Chromium browser
Once you execute the above command, the Playwright tests start executing in the Chromium browser.
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
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:
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.
- BrowserStack’s real device cloud provides 3000+ real browsers and devices for instant, on-demand testing.
- The cloud also provides integrations with popular CI/CD tools such as Jira, Jenkins, TeamCity, Travis CI, and more.
- Additionally, in-built debugging tools let testers identify and resolve bugs immediately.
Run hundreds of Playwright tests in parallel in minutes. Focus on testing – not test infrastructure – with zero setups and maintenance.
ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6po5qxp6e2qLTTZquurJ%2BntqK4