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

Getting Started With Playwright Testing on Android Real Devices


Playwright Android automation is supported on TestMu AI across Node.js, Java, C#, and Python. Run Playwright tests on Chrome for Android across 100+ real Android devices. This guide covers getting started with Playwright testing on Android devices on the TestMu AI platform.

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


Set your TestMu AI username and access key in the environment variables. You can get your TestMu AI username and access key from your TestMu AI Profile > Account Settings > Password & Security.

Access Key on TestMu AI Automation Dashboard

Windows

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

macOS/Linux

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

Install the Playwright package:

npm install playwright

Run Your First Test


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 device = await _android.connect(
`wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(
JSON.stringify(capabilities)
)}`
);

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 context.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:

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 is where you can see the results of your Playwright tests after running them on the TestMu AI platform.

The below screenshot of TestMu AI Automation Dashboard 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 to control your Playwright Android tests.

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 REGEX for device name

When you run a test on a specific device, the exact device 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 REGEX for App/Browser Automation for details.

WebView Testing


To test embedded WebViews on a real Android device, add the mandatory isPwMobileWebviewTest capability to your options (sample script):

playwrightwebview.js
const capabilities = {
"LT:Options": {
"platformName": "android",
"isRealMobile": true, //if true, test will run on real devices
"isPwMobileWebviewTest": true, //mandatory capability to enable WebView testing
},

Run the test as usual (for example, node playwrightwebview.js) and check the Automation dashboard for results.

Test across 3000+ combinations of browsers, real devices & OS.

×
Schedule Your Personal Demo
Book Demo

Help and Support

Related Articles