-
Notifications
You must be signed in to change notification settings - Fork 14
/
local_test_using_bindings.js
84 lines (73 loc) · 2.87 KB
/
local_test_using_bindings.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
'use strict';
const puppeteer = require('puppeteer');
const BrowserStackLocal = require('browserstack-local');
const bsLocal = new BrowserStackLocal.Local();
// replace YOUR_ACCESS_KEY with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
const BS_LOCAL_ARGS = {
'key': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY'
};
/**
* Mark test status on BrowserStack.
*
* @param {Page} page - Page object created by Puppeteer context.
* @param {String} status - Status string can be either passed|failed.
* @param {String} reason - Explanatory reason for the status marked.
* @return {Promise<String>} Stringified response from BrowserStack regarding the
* execution of the jsExecutor.
*/
function markTest(page, status, reason) {
return page.evaluate(
_ => {},
`browserstack_executor: ${JSON.stringify({
action: 'setSessionStatus',
arguments: { status, reason }
})}`);
}
/**
* Driver Test Function.
*
* @async
* @return {Promise<void>}
*/
async function testFn() {
console.log('Started BrowserStackLocal');
// Check if BrowserStack local instance is running
console.log(`BrowserStackLocal running: ${bsLocal.isRunning()}`);
const caps = {
'browser': 'chrome',
'browser_version': 'latest',
'os': 'os x',
'os_version': 'catalina',
'build': 'puppeteer-build-1',
'name': 'My first Puppeteer test',
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
'browserstack.local': 'true'
};
// Use `.connect()` to initiate an Automate session on BrowserStack
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://cdp.browserstack.com/puppeteer?caps=${encodeURIComponent(JSON.stringify(caps))}`,
});
// BrowserStack specific code ends here
const page = await browser.newPage();
await page.goto('http://localhost:45691');
try {
await page.waitForFunction(
`document
.querySelector("body")
.innerText
.includes("This is an internal server for BrowserStack Local")`,
);
// Following line of code is responsible for marking the status of the
// test on BrowserStack as 'passed'. You can use this code in your
// after hook after each test
await markTest(page, 'passed', 'Local is up and running');
} catch {
await markTest(page, 'failed', 'BrowserStack Local binary is not running');
}
await browser.close();
// Stop the Local instance after your test run is completed, i.e after driver.quit
bsLocal.stop(() => console.log('Stopped BrowserStackLocal'));
}
// Starts the Local instance with the required arguments
bsLocal.start(BS_LOCAL_ARGS, testFn);