-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixture.js
155 lines (144 loc) · 5.04 KB
/
fixture.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const base = require("@playwright/test");
const cp = require("child_process");
const { _android } = require("playwright");
const clientPlaywrightVersion = cp
.execSync("npx playwright --version")
.toString()
.trim()
.split(" ")[1];
const BrowserStackLocal = require("browserstack-local");
const util = require("util");
// BrowserStack Specific Capabilities.
// Set 'browserstack.local:true For Local testing
const caps = {
osVersion: "13.0",
deviceName: "Samsung Galaxy S23", // "Samsung Galaxy S22 Ultra", "Google Pixel 7 Pro", "OnePlus 9", etc.
browserName: "chrome",
realMobile: "true",
name: "My android playwright test",
build: "playwright-build-1",
"browserstack.username": process.env.BROWSERSTACK_USERNAME || "<USERNAME>",
"browserstack.accessKey":
process.env.BROWSERSTACK_ACCESS_KEY || "<ACCESS_KEY>",
"browserstack.local": process.env.BROWSERSTACK_LOCAL || false,
};
exports.bsLocal = new BrowserStackLocal.Local();
// replace YOUR_ACCESS_KEY with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
exports.BS_LOCAL_ARGS = {
key: process.env.BROWSERSTACK_ACCESS_KEY || "ACCESSKEY",
};
// Patching the capabilities dynamically according to the project name.
const patchMobileCaps = (name, title) => {
let combination = name.split(/@browserstack/)[0];
let [browerCaps, osCaps] = combination.split(/:/);
let [browser, deviceName] = browerCaps.split(/@/);
let osCapsSplit = osCaps.split(/ /);
let os = osCapsSplit.shift();
let osVersion = osCapsSplit.join(" ");
caps.browser = browser ? browser : "chrome";
caps.deviceName = deviceName ? deviceName : "Samsung Galaxy S22 Ultra";
caps.osVersion = osVersion ? osVersion : "12.0";
caps.name = title;
caps.realMobile = "true";
};
const patchCaps = (name, title) => {
let combination = name.split(/@browserstack/)[0];
let [browerCaps, osCaps] = combination.split(/:/);
let [browser, browser_version] = browerCaps.split(/@/);
let osCapsSplit = osCaps.split(/ /);
let os = osCapsSplit.shift();
let os_version = osCapsSplit.join(" ");
caps.browser = browser ? browser : "chrome";
caps.browser_version = browser_version ? browser_version : "latest";
caps.os = os ? os : "osx";
caps.os_version = os_version ? os_version : "catalina";
caps.name = title;
};
const isHash = (entity) =>
Boolean(entity && typeof entity === "object" && !Array.isArray(entity));
const nestedKeyValue = (hash, keys) =>
keys.reduce((hash, key) => (isHash(hash) ? hash[key] : undefined), hash);
const isUndefined = (val) => val === undefined || val === null || val === "";
const evaluateSessionStatus = (status) => {
if (!isUndefined(status)) {
status = status.toLowerCase();
}
if (status === "passed") {
return "passed";
} else if (status === "failed" || status === "timedout") {
return "failed";
} else {
return "";
}
};
exports.test = base.test.extend({
page: async ({ page, playwright }, use, testInfo) => {
if (testInfo.project.name.match(/browserstack/)) {
let vBrowser, vContext, vDevice;
const isMobile = testInfo.project.name.match(/browserstack-mobile/);
if (isMobile) {
patchMobileCaps(
testInfo.project.name,
`${testInfo.file} - ${testInfo.title}`
);
vDevice = await playwright._android.connect(
`wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(
JSON.stringify(caps)
)}`
);
await vDevice.shell("am force-stop com.android.chrome");
vContext = await vDevice.launchBrowser();
} else {
patchCaps(testInfo.project.name, `${testInfo.title}`);
delete caps.osVersion;
delete caps.deviceName;
delete caps.realMobile;
vBrowser = await playwright.chromium.connect({
wsEndpoint:
`wss://cdp.browserstack.com/playwright?caps=` +
`${encodeURIComponent(JSON.stringify(caps))}`,
});
vContext = await vBrowser.newContext(testInfo.project.use);
}
const vPage = await vContext.newPage();
await use(vPage);
await vPage.close();
if (isMobile) {
await vDevice.close();
} else {
await vBrowser.close();
}
} else {
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 },
],
});