Connect & Get help from fellow developers on our Discord community.
Ask the Community
Run Java Playwright-Android tests on BrowserStack Automate
Learn how to run your Java Playwright-Android tests on BrowserStack Automate
This guide shows you how to run your Java Playwright tests on BrowserStack real Android devices using Automate.
Prerequisites
BrowserStack Username and Access key. You can find this in your account profile . If you have not created an account yet, you can sign up for a Free Trial .
JUnit, Java v8+ is installed on your machine.
If you are using CLI for running tests, ensure that Maven is installed on your machine, Maven environment variables are set, and Maven bin is added to system path, $PATH.
Perform the steps given below to run a Playwright-Android test:
Update Project Dependencies
Use the BrowserStack-forked Playwright dependency to enable integration
Open your project’s pom.xml file.
Locate your Playwright dependency.
Change the groupId from com.microsoft.playwright to com.browserstack.
Set the version to 1.52.1.
```xml
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>playwright</artifactId>
<version>1.52.1</version>
</dependency>
```
Copy icon
Copy
Modify/Create tests
Below is a complete JUnit 5 example that connects to a BrowserStack Android device, opens a browser, and performs a simple test.
```java
package com.example.test;
import com.google.gson.JsonObject;
import com.microsoft.browserstack.*;
import org.junit.jupiter.api.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class BasicPlaywrightTest {
static Playwright pw;
static AndroidDevice device;
static BrowserContext context;
static Page page;
@BeforeAll
static void setup() {
System.out.println("=== Setting up test on BrowserStack Android ===");
String user = System.getenv("BROWSERSTACK_USERNAME");
String key = System.getenv("BROWSERSTACK_ACCESS_KEY");
pw = Playwright.create();
Android android = pw.android();
// Define BrowserStack capabilities
JsonObject caps = new JsonObject();
caps.addProperty("browserstack.user", user);
caps.addProperty("browserstack.key", key);
caps.addProperty("platformName", "android");
caps.addProperty("device", "Samsung Galaxy S23"); // Specify the device
caps.addProperty("browser", "chrome");
caps.addProperty("buildName", "PW Java Android Build");
caps.addProperty("sessionName", "My First Playwright Java Android Test");
String encodedCaps;
try {
encodedCaps = URLEncoder.encode(caps.toString(), StandardCharsets.UTF_8.name());
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// Form the WebSocket URL
String wsUrl = "wss://cdp.browserstack.com/playwright?caps=" + encodedCaps;
// Connect to the BrowserStack device
device = android.connect(wsUrl);
context = device.launchBrowser();
page = context.newPage();
}
@AfterAll
static void teardown() {
System.out.println("=== Tearing down test session ===");
// Gracefully close all resources
try { if (page != null) page.close(); } catch (Throwable t) { System.err.println("Error closing page: " + t); }
try { if (context != null) context.close(); } catch (Throwable t) { System.err.println("Error closing context: " + t); }
try { if (device != null) device.close(); } catch (Throwable t) { System.err.println("Error closing device: " + t); }
try { if (pw != null) pw.close(); } catch (Throwable t) { System.err.println("Error closing Playwright: " + t); }
System.out.println("=== Teardown complete ===");
}
@Test
void sampleBrowserStackTest() {
page.navigate("https://www.browserstack.com", new Page.NavigateOptions().setTimeout(45000.0));
String title = page.title();
System.out.println("Page title is: " + title);
assertNotNull(title);
}
}
```
Copy icon
Copy
Run Your Test
Once the setup is complete, you can run your test using your standard Maven command from the terminal
After execution, you can view the test results, including a video recording and logs, on the BrowserStack Automate Dashboard .
Is this page helping you?
Thank you for your valuable feedback!