1 | 'use strict';
|
2 |
|
3 | const CDP = require('chrome-remote-interface');
|
4 | const fs = require('fs');
|
5 | const path = require('path');
|
6 |
|
7 | module.exports = async function connectClient (instance, log, options) {
|
8 |
|
9 | function fetch (fileName) {
|
10 | const filePath = path.join(__dirname, '../client', fileName);
|
11 | return fs.readFileSync(filePath, 'utf-8');
|
12 | }
|
13 |
|
14 | const client = await CDP({port: instance.port});
|
15 | const { DOM, DOMStorage, Console, Network, Page, Runtime } = client;
|
16 | const mochaOptions = `window.mochaOptions = ${JSON.stringify(options.mocha)}`;
|
17 |
|
18 | await Promise.all([
|
19 | DOM.enable(),
|
20 | DOMStorage.enable(),
|
21 | Network.enable(),
|
22 | Page.enable(),
|
23 | Runtime.enable(),
|
24 | Console.enable()
|
25 | ]);
|
26 |
|
27 | log.info('CDP Client Connected');
|
28 |
|
29 | await Page.addScriptToEvaluateOnLoad({ scriptSource: mochaOptions });
|
30 | await Page.addScriptToEvaluateOnLoad({ scriptSource: fetch('event-bus.js') });
|
31 | await Page.addScriptToEvaluateOnLoad({ scriptSource: fetch('shim.js') });
|
32 |
|
33 | return client;
|
34 | };
|