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 Android Tests on TestMu AI


When you need to validate your web app on Chrome for Android, you can run Playwright tests on real Android devices with TestMu AI. You get access to 100+ real Android devices across Node.js, Java, C#, and Python, so you catch device-specific issues instead of emulator approximations. You connect Playwright to the TestMu AI cloud over a CDP endpoint, pass your capabilities, and run your existing test scripts against a live device.

Supported Versions
  • Playwright versions v1.53.0 to v1.61.0 are supported for Android Real Device testing (excluding v1.54.0).
  • Java, C#, and Python use the chromium.connect() API. Node.js supports both chromium.connect() and the Android-native _android.connect() API. All use stock Playwright packages, no custom forks required.
  • Playwright v1.53.0 is currently supported for Playwright C# (for Android & iOS).

Prerequisites​


Before you run a test, set your TestMu AI username and access key as environment variables. You can find both under your TestMu AI Profile > Account Settings > Password & Security.

Windows

Verified
set LT_USERNAME="YOUR_LAMBDATEST_USERNAME"
set LT_ACCESS_KEY="YOUR_LAMBDATEST_ACCESS_KEY"

macOS/Linux

Verified
export LT_USERNAME="YOUR_LAMBDATEST_USERNAME"
export LT_ACCESS_KEY="YOUR_LAMBDATEST_ACCESS_KEY"
Verified

Install the Playwright package:

npm install playwright

Run Your First Test​


Pick your language below, then use the sample script to connect to TestMu AI and run a search test on a real Android device.

Verified

Node.js supports both the Chromium API (chromium.connect()) and the Android-native API (_android.connect()).

Using chromium.connect()

playwright-android-test.js
const { chromium } = require("playwright");

(async () => {
const capabilities = {
"LT:Options": {
platformName: "android",
deviceName: ".*",
platformVersion: ".*",
isRealMobile: true,
build: "Playwright Android Build",
name: "Playwright Android Test",
user: process.env.LT_USERNAME,
accessKey: process.env.LT_ACCESS_KEY,
network: true,
video: true,
console: true,
playwrightClientVersion: "1.61.0",
},
};

const cdpUrl = `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(
JSON.stringify(capabilities)
)}`;

const browser = await chromium.connect(cdpUrl);
const context = browser.contexts()[0] || (await browser.newContext());
const page = context.pages()[0] || (await context.newPage());

await page.goto("https://duckduckgo.com", { timeout: 30000 });
await page.locator('[name="q"]').fill("LambdaTest");
await page.locator('[name="q"]').press("Enter");
await page.waitForTimeout(3000);

const title = await page.title();
console.log("Page title:", title);

try {
if (title.includes("LambdaTest")) {
await page.evaluate(
(_) => {},
`lambdatest_action: ${JSON.stringify({
action: "setTestStatus",
arguments: { status: "passed", remark: "Title verified" },
})}`
);
}
} catch (e) {
await page.evaluate(
(_) => {},
`lambdatest_action: ${JSON.stringify({
action: "setTestStatus",
arguments: { status: "failed", remark: e.message },
})}`
);
}

await page.close();
await browser.close();
})();

Using _android.connect()

playwright-android-test.js
const { _android } = require("playwright");

(async () => {
const capabilities = {
"LT:Options": {
platformName: "android",
deviceName: ".*",
platformVersion: ".*",
isRealMobile: true,
build: "Playwright Android Build",
name: "Playwright Android Test",
user: process.env.LT_USERNAME,
accessKey: process.env.LT_ACCESS_KEY,
network: true,
video: true,
console: true,
playwrightClientVersion: "1.61.0",
},
};

const cdpUrl = `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(
JSON.stringify(capabilities)
)}`;

const device = await _android.connect(cdpUrl);
console.log(`Model: ${device.model()}, Serial: ${device.serial()}`);
await device.shell("am force-stop com.android.chrome");

const context = await device.launchBrowser();
context.setDefaultTimeout(120000);
const page = await context.newPage();

await page.goto("https://duckduckgo.com");
await page.locator('[name="q"]').fill("LambdaTest");
await page.locator('[name="q"]').press("Enter");
await page.waitForTimeout(3000);

const title = await page.title();
console.log("Page title:", title);

try {
if (title.includes("LambdaTest")) {
await page.evaluate(
(_) => {},
`lambdatest_action: ${JSON.stringify({
action: "setTestStatus",
arguments: { status: "passed", remark: "Title verified" },
})}`
);
}
} catch (e) {
await page.evaluate(
(_) => {},
`lambdatest_action: ${JSON.stringify({
action: "setTestStatus",
arguments: { status: "failed", remark: e.message },
})}`
);
}

await page.close();
await device.close();
})();
tip

The timeout value specified in the Playwright configuration may default to 30 seconds on real devices. To set a custom timeout, add:

Verified
context.setDefaultTimeout(120000);  // Set your desired timeout value.

Run the test:

node playwright-android-test.js
tip

For Java, C#, and Python on Android, the CDP connection returns an existing browser context and page. Always check for existing contexts/pages before creating new ones, as shown in the examples above.

View Your Playwright Test Results​


The TestMu AI Automation Dashboard shows the results of your Playwright tests after each run on the TestMu AI platform. Open it to review builds, sessions, logs, and video.

The screenshot below shows the Playwright build on the left and the build sessions associated with the selected build on the right.

Playwright Android build and session details on TestMu AI Automation Dashboard

Capabilities Reference​


Configure these capabilities in your LT:Options object to control how your Playwright Android tests run.

tip

Use the Playwright Android Capability Generator to generate capabilities for your tests.

KeyExpected ValuesDescriptionCapability
platformandroidSpecify the platform nameconst capability = { "LT:Options": {"platform": "android",}}
platformVersion12Specify the platform versionconst capability = { "LT:Options": {"platformVersion": "12",}}
deviceNamePixel 5Specify the device name.const capability = { "LT:Options": {"deviceName": "Pixel 5",}}
buildPlaywright Android BuildRepresent the build number for your testconst capability = {"LT:Options": {"build": "<build_name>",}}
namePlaywright Android TestRepresents the name of a testconst capability = {"LT:Options": {"name": "<test_name>",}}
projectNamePlaywright Sample ProjectRepresents the name of a projectconst capability = {"LT:Options": {"projectName": "<project_name>",}}
tags["tag1", "tag2", "tag3"]Group your Playwright testsconst capability = {"LT:Options": { "tags": ["tag1", "tag2", "tag3"], }}
buildTags["build1", "build2", "build3"]Group your Playwright buildsconst capability = {"LT:Options": { "buildTags": ["build1", "build2", "build3"] }}
networktrue/falseEnable network logsconst capability = { "LT:Options": {"network": true,}}
consoletrue/falseEnable browser console logsconst capabilities = { "LT:Options": {"console": true,}}
videotrue/falseEnable video recording of the entire screenconst capability = { "LT:Options": {"video": true,}}
tunneltrue/falseEnable tunnel for local testingconst capability = { "LT:Options": {"tunnel": true,}}
tunnelNametrue/falseSpecify tunnel nameconst capability = { "LT:Options": {"tunnelName": "<tunnel_name>",}}
geoLocationAR (Argentina)Specify country codeconst capability = { "LT:Options": {"geoLocation": "AR",}}

Using a REGEX for the Device Name​


When you run a test on a specific device, the exact model you selected may not be available. A regular expression (REGEX) widens the device search so you get any matching device. For example, to run on any Pixel device rather than one specific model, use a REGEX for deviceName. See how to match devices with a REGEX for details.


Continue with these related guides to extend your Playwright coverage on TestMu AI.

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