-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambdatest-setup.js
97 lines (87 loc) · 3.73 KB
/
lambdatest-setup.js
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
/**
* Add the file in your test suite to run tests on LambdaTest.
* Import `test` object from this file in the tests.
*/
const base = require('@playwright/test')
const path = require('path')
const { chromium, _android } = require('playwright')
const cp = require('child_process');
const playwrightClientVersion = cp.execSync('npx playwright --version').toString().trim().split(' ')[1];
// LambdaTest capabilities
const capabilities = {
'browserName': 'Chrome', // Browsers allowed: `Chrome`, `MicrosoftEdge`, `pw-chromium`, `pw-firefox` and `pw-webkit`
'browserVersion': 'latest',
'LT:Options': {
'platform': 'Windows 10',
'build': 'Playwright JS Build - Playwright 101 Assignment',
'name': 'Playwright Test',
'user': 'harshamanoj912',
'accessKey': 'WumGH3qVRCoL7vTMQ3YT93dSeXCyBLENQAPm0N0nGAQ1zrZStB',
'network': true,
'video': true,
'console': true,
'playwrightClientVersion': playwrightClientVersion
}
}
// Patching the capabilities dynamically according to the project name.
const modifyCapabilities = (configName, testName) => {
let config = configName.split('@lambdatest')[0]
// Check if its an android test or a desktop test
if (configName.match(/android/)) {
let [deviceName, platformVersion, platform] = config.split(':')
capabilities['LT:Options']['deviceName'] = deviceName
capabilities['LT:Options']['platformVersion'] = platformVersion
capabilities['LT:Options']['platformName'] = platform
capabilities['LT:Options']['name'] = testName
capabilities['LT:Options']['build'] = 'Playwright JS Android Build'
capabilities['LT:Options']['isRealMobile'] = true
delete capabilities.browserName;
delete capabilities.browserVersion;
} else {
// Desktop test
let [browserName, browserVersion, platform] = config.split(':')
capabilities.browserName = browserName ? browserName : capabilities.browserName
capabilities.browserVersion = browserVersion ? browserVersion : capabilities.browserVersion
capabilities['LT:Options']['platform'] = platform ? platform : capabilities['LT:Options']['platform']
capabilities['LT:Options']['name'] = testName
}
}
exports.test = base.test.extend({
page: async ({ page, playwright }, use, testInfo) => {
// Configure LambdaTest platform for cross-browser testing
let fileName = testInfo.file.split(path.sep).pop()
if (testInfo.project.name.match(/lambdatest/)) {
modifyCapabilities(testInfo.project.name, `${testInfo.title} - ${fileName}`)
let device, context, browser, ltPage;
// Check if its a desktop or an android test
if (testInfo.project.name.match(/android/)) {
// Android test
device = await _android.connect(`wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`);
await device.shell("am force-stop com.android.chrome");
context = await device.launchBrowser();
ltPage = await context.newPage(testInfo.project.use);
} else {
// Desktop test
browser = await chromium.connect(`wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`)
ltPage = await browser.newPage(testInfo.project.use)
}
await use(ltPage)
const testStatus = {
action: 'setTestStatus',
arguments: {
status: testInfo.status,
remark: testInfo.error?.stack || testInfo.error?.message,
}
}
await ltPage.evaluate(() => {},
`lambdatest_action: ${JSON.stringify(testStatus)}`)
await ltPage.close()
await context?.close();
await browser?.close()
await device?.close();
} else {
// Run tests in local in case of local config provided
await use(page)
}
}
})