forked from jdobosz/testcafe-browser-provider-puppeteer
/
index.js
94 lines (73 loc) · 3.18 KB
/
index.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
const puppeteer = require('puppeteer');
const timeout = (ms) => new Promise(resolve => setTimeout(resolve, ms));
module.exports = {
// Multiple browsers support
isMultiBrowser: true,
browser: null,
openedPages: {},
// Required - must be implemented
// Browser control
async openBrowser(id, pageUrl, browserName) {
const browserArgs = browserName.split(':');
if (!this.browser) {
const launchArgs = {
timeout: 10000,
};
const noSandboxArgs = ['--no-sandbox', '--disable-setuid-sandbox'];
if (browserArgs.indexOf('no_sandbox') !== -1) {
launchArgs.args = noSandboxArgs;
} else if (browserName.indexOf('?') !== -1) {
const userArgs = browserName.split('?');
const params = userArgs[0];
if (params === 'no_sandbox') {
launchArgs.args = noSandboxArgs;
}
const executablePath = userArgs[1];
if (executablePath.length > 0) {
launchArgs.executablePath = executablePath;
}
}
this.browser = await puppeteer.launch(launchArgs);
}
const page = await this.browser.newPage();
const emulationArg = browserArgs.find((v) => /^emulate/.test(v));
if (emulationArg) {
const [, emulationDevice] = emulationArg.split('=');
const device = puppeteer.devices[emulationDevice];
if (!device) {
throw new Error('Emulation device is not supported');
}
await page.emulate(device);
}
await page.goto(pageUrl, {waitUntil: 'networkidle0', timeout: 0});
this.openedPages[id] = page;
},
async closeBrowser(id) {
delete this.openedPages[id];
await this.browser.close();
},
async isValidBrowserName() {
return true;
},
async hasCustomActionForBrowser (/* browserId */) {
return {
hasCloseBrowser: this.hasOwnProperty('closeBrowser'),
hasResizeWindow: this.hasOwnProperty('resizeWindow'),
hasTakeScreenshot: this.hasOwnProperty('takeScreenshot'),
hasGetVideoFrameData: this.hasOwnProperty('getVideoFrameData'),
hasCanResizeWindowToDimensions: this.hasOwnProperty('canResizeWindowToDimensions'),
hasMaximizeWindow: this.hasOwnProperty('maximizeWindow'),
hasChromelessScreenshots: false,
};
},
// Extra methods
async resizeWindow(id, width, height) {
await this.openedPages[id].setViewport({width, height});
},
async takeScreenshot(id, screenshotPath, width, height, fullPage) {
await this.openedPages[id].screenshot({path: screenshotPath, fullPage});
console.log(`please open ${screenshotPath} screenshot. For next 10 seconds created screenshot contains full page (proper behavior)`);
await timeout(10000);
console.log(`...and now open the same screenshot again (${screenshotPath}). Now it contains only visible in viewport part of page`);
},
};