Skip to main content

Run your first Playwright test on BrowserStack

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

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.

In this guide, you will learn about:

Prerequisites

Run your first test

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

Step 1: Clone the playwright-browserstack sample repo on GitHub using the following command in your terminal:

git clone https://github.com/browserstack/playwright-browserstack.git
cd playwright-browserstack

Step 2: Install dependencies using the following command in your terminal:

npm install

Step 3: Set your BrowserStack credentials in the script as follows:

Navigate to the single_test.js file in the playwright-browserstack directory and set YOUR_USERNAME and YOUR_ACCESS_KEY.

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 4: Run your first test using the following command in your terminal:

node single_test.js

Step 5: View your tests on BrowserStack on the BrowserStack Automate dashboard. Check out viewing test results to learn more about the dashboard.

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 Google search home page
  • Performs a search for the term “BrowserStack”
  • Marks the test as passed or failed based on the assertions

When you run the test command, tests present in the single_test.js file are executed

// single_test.js

const expect = require('chai').expect
const { chromium } = require('playwright');

const cp = require('child_process');
const clientPlaywrightVersion = cp.execSync('npx playwright --version').toString().trim().split(' ')[1];

(async () => {
  const caps = {
  	'browser': 'chrome',  // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
    'os': 'osx',
    'os_version': 'catalina',
    'name': 'My first playwright test',
    'build': 'playwright-build-1',
    'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
    'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
    'client.playwrightVersion': clientPlaywrightVersion  // Playwright version being used on your local project needs to be passed in this capability for BrowserStack to be able to map request and responses correctly
  };
  const browser = await chromium.connect({
    wsEndpoint: `wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`,
  });
  const page = await browser.newPage();
  await page.goto('https://www.google.com/ncr');
  const element = await page.$('[aria-label="Search"]');
  await element.click();
  await element.type('BrowserStack');
  await element.press('Enter');
  const title = await page.title('');
  console.log(title);
  try {
    expect(title).to.equal("BrowserStack - Google Search", 'Expected page title is incorrect!');
    // following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
    await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {status: 'passed',reason: 'Title matched'}})}`);
  } catch {
    await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {status: 'failed',reason: 'Title did not match'}})}`);
  }
  await browser.close();
})();
Note: Playwright tests run on BrowserStack using a client-server architecture. So, test assertions run on the client side, and hence, BrowserStack won’t know whether your tests have passed or failed. Learn more how to mark tests as passed or failed on BrowserStack.

Next Steps

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
Talk to an Expert