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

  • BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
  • Java v8+ (if using Gradle, Java v9+ is required), Selenium v2.5+ (JSON Wire / W3C).
  • If you are using CLI for running tests, ensure that Maven or Gradle is installed on your machine, its environment variables are set, and its bin is added to system path, $PATH.

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/playwright-java/

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

mvn clean install

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

Navigate to the PlaywrightTest.java file in the playwright-java/src/test/java/com/browserstack directory and set your BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY

JsonObject capabilitiesObject = new JsonObject();
    capabilitiesObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
    capabilitiesObject.addProperty("browserstack.accessKey", "BROWSERSTACK_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:

mvn -Dexec.mainClass="com.browserstack.PlaywrightSingleTest" -Dexec.classpathScope=test test-compile exec:java

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 PlaywrightTest.java file are executed

// PlaywrightTest.java

package com.browserstack;

import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import java.net.URLEncoder;

public class PlaywrightTest {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            JsonObject capabilitiesObject = new JsonObject();
            capabilitiesObject.addProperty("browser", "chrome");    // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
            capabilitiesObject.addProperty("browser_version", "latest");
            capabilitiesObject.addProperty("os", "osx");
            capabilitiesObject.addProperty("os_version", "catalina");
            capabilitiesObject.addProperty("name", "Playwright first single test");
            capabilitiesObject.addProperty("build", "playwright-java-1");
            capabilitiesObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
            capabilitiesObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
            
            BrowserType chromium = playwright.chromium(); 
            String caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8");
            String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
            Browser browser = chromium.connect(ws_endpoint);
            Page page = browser.newPage();
            try {
                page.navigate("https://www.google.co.in/");
                Locator locator = page.locator("[aria-label='Search']");
                locator.click();
                page.fill("[aria-label='Search']", "BrowserStack");
                page.locator("[aria-label='Google Search'] >> nth=0").click();
                String title = page.title();

                if (title.equals("BrowserStack - Google Search")) {
                    // 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
                    markTestStatus("passed", "Title matched", page);
                } else {
                    markTestStatus("failed", "Title did not match", page);
                }
                
            } catch (Exception err) {
                markTestStatus("failed", err.getMessage(), page);
            }
            browser.close();
        } catch (Exception err) {
            System.out.println(err);
        }
    }

    public static void markTestStatus(String status, String reason, Page page) {
        Object result;
        result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}");
    }
}
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