Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & Test Observability
No Result Found

Playwright with NodeJS

Select your testing method

Learn how to run your first Playwright test across 100+ browser-OS combinations.

For Playwright Android support, refer to Run Playwright tests on Android devices using BrowserStack.

Introduction

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

Prerequisites

Run a sample build

To run your first Playwright test on BrowserStack, complete the following steps:

Step 1: Clone sample git repository

Clone the node-js-playwright-browserstack sample repo on GitHub using the following command:

git clone -b playwright-legacy https://github.com/browserstack/node-js-playwright-browserstack.git
cd node-js-playwright-browserstack

To run your Playwright test with TypeScript, check out our Playwright TypeScript repository.

Step 2: Set BrowserStack credentials

Set your BrowserStack credentials in the browserstack.config.js file:

const caps = {
    'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
    'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
  };

Alternatively, you can set the environment variables in your system as follows:

# Set these values in your ~/.zprofile (zsh) or ~/.profile (bash)
  export BROWSERSTACK_USERNAME="YOUR_USERNAME"
  export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
# setx.exe does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts
  setx BROWSERSTACK_USERNAME="YOUR_USERNAME"
  setx BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"

# Verify whether the variables have been set
echo BROWSERSTACK_USERNAME
echo BROWSERSTACK_ACCESS_KEY

Step 3: Install the dependencies

Install dependencies using the following command:

npm install

Step 4: Run your test

Run your first test using the following command in your terminal:

npm run sample-test

Step 5: View test results

View your tests on the BrowserStack Automate dashboard. To learn more about the dashboard, check the view test results document.

Automate Dashboard

Understand the details of your test

When you run your test command, the test script:

  • Starts the latest version of the Chrome browser
  • Opens the bstackdemo.com website
  • Adds a product to the cart
  • Marks the test as passed or failed based on the assertions

When you run the test command, tests present in the sample_test.js file in tests folder executes

sample_test.js
// @ts-check
const { test, expect } = require('@playwright/test');

test('BstackDemo Add to cart', async ({ page },testInfo) => {

try{

  await page.evaluate(_ => {},`browserstack_executor: ${JSON.stringify({action: "setSessionName", arguments: {name:testInfo.project.name}})}`);
  await page.waitForTimeout(5000);

  await page.goto('https://www.bstackdemo.com/',{ waitUntil: 'networkidle' });
  await page.locator('[id="\\32 "]').getByText('Add to cart').click();
  await page.getByText('Checkout').click();
  await page.locator('#username svg').click();
  await page.locator('#react-select-2-option-0-0').click();
  await page.locator('#password svg').click();
  await page.locator('#react-select-3-option-0-0').click();
  await page.getByRole('button', { name: 'Log In' }).click();
  await page.getByLabel('First Name').click();
  await page.getByLabel('First Name').fill('SampleFirst');
  await page.getByLabel('Last Name').click();
  await page.getByLabel('Last Name').fill('sampleLast');
  await page.getByLabel('Address').click();
  await page.getByLabel('Address').fill('sampleAddress');
  await page.getByLabel('State/Province').click();
  await page.getByLabel('State/Province').fill('SampleState');
  await page.getByLabel('Postal Code').click();
  await page.getByLabel('Postal Code').fill('123456');
  await page.getByRole('button', { name: 'Submit' }).click();
  await page.getByRole('button', { name: 'Continue Shopping »' }).click();

  await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {status: 'passed',reason: 'Product added to cart'}})}`);

} catch (e) {
  console.log(e);
  await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {status: 'failed',reason: 'Test failed'}})}`);

}  

});

Next Steps

After you have successfully run your first test on BrowserStack, try:

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle