-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun-server.js
More file actions
43 lines (37 loc) · 1.03 KB
/
run-server.js
File metadata and controls
43 lines (37 loc) · 1.03 KB
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
const net = require('net');
const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const getWebdriver = () => {
const webdriver = new Builder()
.setChromeOptions(new chrome.Options().headless())
.forBrowser('chrome')
.build();
return webdriver;
};
const connectToChromeDriver = port => {
let socket;
return new Promise((resolve, reject) => {
// Give up after 1s
const timer = setTimeout(() => {
socket.destroy();
reject(new Error('Unable to connect to ChromeDriver'));
}, 1000);
const connectionListener = () => {
clearTimeout(timer);
socket.destroy();
return resolve();
};
socket = net.createConnection(
{ host: 'localhost', port },
connectionListener
);
// Fail on error
socket.once('error', err => {
clearTimeout(timer);
socket.destroy();
return reject(err);
});
});
};
module.exports.getWebdriver = getWebdriver;
module.exports.connectToChromeDriver = connectToChromeDriver;