This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathwebdriver.ts
63 lines (52 loc) · 1.61 KB
/
webdriver.ts
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
import * as path from 'path';
import * as child_process from 'child_process';
import { Builder, ThenableWebDriver } from 'selenium-webdriver';
import * as chrome from 'selenium-webdriver/chrome';
async function getGlobalChromedriverPath() {
const yarnGlobalPath = await new Promise<string>((resolve, reject) => {
child_process.exec('yarn global dir', { timeout: 8000 }, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result.trim());
}
});
});
return path.resolve(
yarnGlobalPath,
'./node_modules/chromedriver/lib/chromedriver',
process.platform === 'win32' ? './chromedriver.exe' : './chromedriver'
);
}
export function runDriver(): () => ThenableWebDriver {
let webdriver: ThenableWebDriver | null = null;
// same webdriver instance serves all the tests in the suite
before(async function () {
const chromedriverPath = await getGlobalChromedriverPath();
const service = new chrome.ServiceBuilder(chromedriverPath).build();
chrome.setDefaultService(service);
webdriver = new Builder()
.forBrowser('chrome')
.setChromeOptions(
process.env.CI ? new chrome.Options().headless() : new chrome.Options()
)
.build();
});
after(async function () {
if (!webdriver) {
throw new Error(
'cannot clean up webdriver because it was not initialized'
);
}
await webdriver.quit();
// complete cleanup
webdriver = null;
});
// expose singleton getter
return () => {
if (!webdriver) {
throw new Error('webdriver not initialized');
}
return webdriver;
};
}