For AI agents and LLMs: a machine-readable index is available at llms.txt. A plain-Markdown version of any documentation page is available by appending .md to its URL.
Skip to main content

How to Run Playwright Tests With JUnit on TestMu AI


Run your Java Playwright tests on the TestMu AI cloud grid to reach 50+ real desktop browsers and operating systems without maintaining local infrastructure. You connect Playwright over a CDP WebSocket, pass your capabilities, then run the sample suite with JUnit.

Prerequisites​


Complete these before running the test below.

  1. A TestMu AI Username and Access Key. Get them from your TestMu AI Profile. Don't have an account? Sign up for free.

  2. JDK 8 or later and Apache Maven installed. The sample projects are Maven projects, so Maven resolves the dependencies when you build.

  3. Clone the sample repository. Each framework lives in its own subdirectory.

Sample repo
Verified
git clone https://github.com/LambdaTest/playwright-sample.git
cd playwright-sample

Set Your Credentials​


Your Username and Access Key are read from environment variables. Set them once. Pick your operating system:

Verified
export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"

How the Sample Test Works​


The test builds a capabilities object that carries the browser, version, platform, and your LT:Options, then connects Playwright to the grid at wss://cdp.lambdatest.com/playwright. The credentials come from the LT_USERNAME and LT_ACCESS_KEY environment variables you set above:

Verified
JsonObject capabilities = new JsonObject();
JsonObject ltOptions = new JsonObject();

String user = System.getenv("LT_USERNAME");
String accessKey = System.getenv("LT_ACCESS_KEY");

capabilities.addProperty("browserName", "Chrome"); // Browsers allowed: `Chrome`, `MicrosoftEdge`, `pw-chromium`, `pw-firefox` and `pw-webkit`
capabilities.addProperty("browserVersion", "latest");
ltOptions.addProperty("platform", "Windows 10");
ltOptions.addProperty("user", user);
ltOptions.addProperty("accessKey", accessKey);
capabilities.add("LT:Options", ltOptions);

JUnit wraps this same connection in a @DataProvider so one test runs across multiple browser configurations, as the steps below show.

Run the Test With JUnit​


JUnit runs the connection through a @DataProvider, so a single test runs across multiple browser configurations (here Chrome and Microsoft Edge). You can use your own project, or the sample below.

  1. Clone the sample project and move into the JUnit subdirectory:
Sample repo
Verified
git clone https://github.com/LambdaTest/playwright-sample.git
cd playwright-sample
cd playwright-java-junit
  1. Install the npm dependencies:
Verified
npm install
  1. Open the LTCapability.java file and provide your TestMu AI Username and Access Key. It supplies the parameterized capabilities through a @DataProvider:
Verified
LTCapability.java
import com.google.gson.JsonObject;
import com.tngtech.java.junit.dataprovider.DataProvider;

public class LTCapability {
@DataProvider
public static Object[] getDefaultTestCapability() {
JsonObject capabilities1 = new JsonObject();
JsonObject ltOptions1 = new JsonObject();

String user = System.getenv("LT_USERNAME");
String accessKey = System.getenv("LT_ACCESS_KEY");

capabilities1.addProperty("browserName", "Chrome"); // Browsers allowed: `Chrome`, `MicrosoftEdge`, `pw-chromium`, `pw-firefox` and `pw-webkit`
capabilities1.addProperty("browserVersion", "latest");
ltOptions1.addProperty("platform", "Windows 10");
ltOptions1.addProperty("name", "Playwright Test");
ltOptions1.addProperty("build", "Playwright Testing using Junit");
ltOptions1.addProperty("user", user);
ltOptions1.addProperty("accessKey", accessKey);
capabilities1.add("LT:Options", ltOptions1);


JsonObject capabilities2 = new JsonObject();
JsonObject ltOptions2 = new JsonObject();
capabilities2.addProperty("browserName", "MicrosoftEdge"); // Browsers allowed: `Chrome`, `MicrosoftEdge`, `pw-chromium`, `pw-firefox` and `pw-webkit`
capabilities2.addProperty("browserVersion", "latest");
ltOptions2.addProperty("platform", "Windows 10");
ltOptions2.addProperty("name", "Playwright Test");
ltOptions2.addProperty("build", "Playwright Testing using Junit");
ltOptions2.addProperty("user", user);
ltOptions2.addProperty("accessKey", accessKey);
capabilities2.add("LT:Options", ltOptions2);
return new Object[]{
capabilities1,capabilities2
};
}
}
  1. Run the test from the terminal:
Verified
mvn clean test

View Your Results​


Your test results, including video, network logs, and command-by-command execution, appear on the TestMu AI Web Automation Dashboard. A green status confirms the test passed.


Continue with these related guides:

Terminal First Testing With Kane CLI

Natural language browser & mobile app tests right from terminal.

×
Schedule Your Personal Demo
Kane CLI terminal

Help and Support

Related Articles