-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathplaywright-android-real-device.js
More file actions
99 lines (79 loc) · 3.25 KB
/
playwright-android-real-device.js
File metadata and controls
99 lines (79 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require('dotenv').config();
const {_android} = require("playwright");
const {expect} = require("expect");
(async () => {
console.log('Starting Playwright Android Real Device test...');
console.log('Device: Galaxy S21 5G (Android 12)');
const capabilities = {
"LT:Options": {
"platformName": "android",
"deviceName": "Galaxy S21 5G",
"platformVersion": "12",
"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,
"projectName": "New UI",
},
};
console.log('Connecting to LambdaTest Android cloud...');
console.log('Username:', process.env.LT_USERNAME);
console.log('Platform: Android 12, Device: Galaxy S21 5G');
let device = await _android.connect(
`wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(
JSON.stringify(capabilities))}`,
);
console.log('Connected to Android device successfully!');
console.log(`Device Model: ${device.model()}, Serial: ${device.serial()}`);
console.log('Force stopping Chrome browser...');
await device.shell("am force-stop com.android.chrome");
console.log('Launching Chrome browser...');
let context = await device.launchBrowser();
let page = await context.newPage();
console.log('Navigating to DuckDuckGo...');
await page.goto("https://duckduckgo.com");
console.log('Finding search box...');
let element = await page.locator("[name=\"q\"]");
console.log('Clicking search box...');
await element.click();
console.log('Typing "Playwright"...');
await element.type("Playwright");
console.log('Pressing Enter...');
await element.press("Enter");
console.log('Waiting for search results...');
let title = await page.title();
console.log('Page title:', title);
try {
console.log('Verifying title contains "Playwright"...');
expect(title).toEqual("Playwright at DuckDuckGo");
console.log('Android Test PASSED! Title matched successfully');
// Mark the test as completed or failed
await page.evaluate(_ => {}, `lambdatest_action: ${JSON.stringify({ action: "setTestStatus", arguments: {status: "passed", remark: "Assertions passed" },})}`);
console.log('Marked test as PASSED in LambdaTest dashboard');
await teardown(page, context, device)
} catch (e) {
console.log('Android Test FAILED!');
console.log('Error:', e.message);
console.log('Expected title: "Playwright at DuckDuckGo"');
console.log('Actual title:', title);
await page.evaluate(_ => {}, `lambdatest_action: ${JSON.stringify({action: "setTestStatus", arguments: { status: "failed", remark: e.stack }})}`);
console.log('Marked test as FAILED in LambdaTest dashboard');
await teardown(page, context, device)
throw e.stack
}
})().catch(err => {
console.error('Unexpected error occurred in Android test:');
console.error(err);
process.exit(1);
});
async function teardown(page, context, device) {
console.log('Cleaning up Android test resources...');
await page.close();
await context.close();
await device.close();
console.log('Android test completed and resources cleaned up!');
}