UNPKG

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