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.20.0 to v1.59.0 are supported for Android real device testing (excluding v1.54.0).
  • Node.js uses the _android.connect() API. Java, C#, and Python use chromium.connectOverCDP(). All use stock Playwright packages, no custom forks required.
  • Playwright v1.53.2 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"

Language-Specific Setup

LanguageSupported Playwright Versions
JavaScript (Node.js)Up to v1.59.0
Java, Python, C#Up to v1.53.2

Install the Playwright package:

npm install playwright

Run Your First Test


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

(async () => {
const capabilities = {
"LT:Options": {
"platformName": "android",
"deviceName": "Pixel 5",
"platformVersion": "11",
"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,
},
};

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

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

Book Demo

Help and Support

Related Articles