forked from LambdaTest/playwright-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambdatest-setup.js
140 lines (128 loc) · 4.98 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* 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];
if (process.env.executeOn !== "local"){
// 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',
'name': 'Playwright Test',
'user': process.env.LT_USERNAME,
'accessKey': process.env.LT_ACCESS_KEY,
'network': true,
'video': true,
'console': true,
'tunnel': false, // Add tunnel configuration if testing locally hosted webpage
'tunnelName': '', // Optional
'geoLocation': '', // country code can be fetched from https://www.lambdatest.com/capabilities-generator/
'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)
}
},
beforeEach: [
async ({ page }, use) => {
await page
.context()
.tracing.start({ screenshots: true, snapshots: true, sources: true });
await use();
},
{ auto: true },
],
afterEach: [
async ({ page }, use, testInfo) => {
await use();
if (testInfo.status == "failed") {
await page
.context()
.tracing.stop({ path: `${testInfo.outputDir}/trace.zip` });
await page.screenshot({ path: `${testInfo.outputDir}/screenshot.png` });
await testInfo.attach("screenshot", {
path: `${testInfo.outputDir}/screenshot.png`,
contentType: "image/png",
});
await testInfo.attach("trace", {
path: `${testInfo.outputDir}/trace.zip`,
contentType: "application/zip",
});
}
},
{ auto: true },
],
});
}else {
// Fallback to local if `executeOn` is not set to `lambdatest`
exports.test = base.test.extend({
page: async ({ page }, use) => {
await use(page); // Running locally
}
});
}