2019-01-30 22:47:03 +07:00
|
|
|
require("chromedriver");
|
|
|
|
import chrome from 'selenium-webdriver/chrome';
|
2019-03-28 05:09:01 +07:00
|
|
|
import SeleniumWebdriver, { WebDriver, ProxyConfig } from "selenium-webdriver";
|
2019-01-30 22:47:03 +07:00
|
|
|
|
2019-03-28 05:09:01 +07:00
|
|
|
export async function StartDriver(proxy?: ProxyConfig) {
|
2019-02-05 05:23:59 +07:00
|
|
|
let options = new chrome.Options();
|
|
|
|
|
|
|
|
if (process.env['HEADLESS'] == 'y') {
|
|
|
|
options = options.headless();
|
|
|
|
}
|
2019-01-30 22:47:03 +07:00
|
|
|
|
2019-03-28 05:09:01 +07:00
|
|
|
let driverBuilder = new SeleniumWebdriver.Builder()
|
|
|
|
.forBrowser("chrome");
|
|
|
|
|
|
|
|
if (proxy) {
|
|
|
|
options = options.addArguments(`--proxy-server=${proxy.httpProxy}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
driverBuilder = driverBuilder.setChromeOptions(options);
|
|
|
|
return await driverBuilder.build();
|
2019-02-13 05:23:43 +07:00
|
|
|
}
|
2019-02-10 05:20:37 +07:00
|
|
|
|
2019-02-13 05:23:43 +07:00
|
|
|
export async function StopDriver(driver: WebDriver) {
|
|
|
|
return await driver.quit();
|
|
|
|
}
|
2019-01-30 22:47:03 +07:00
|
|
|
|
2019-02-13 05:23:43 +07:00
|
|
|
export default function(forEach: boolean = false) {
|
2019-02-10 05:20:37 +07:00
|
|
|
if (forEach) {
|
2019-02-13 05:23:43 +07:00
|
|
|
beforeEach(async function() {
|
|
|
|
this.driver = await StartDriver();
|
|
|
|
});
|
|
|
|
afterEach(async function() {
|
|
|
|
await StopDriver(this.driver);
|
|
|
|
});
|
2019-02-10 05:20:37 +07:00
|
|
|
} else {
|
2019-02-13 05:23:43 +07:00
|
|
|
before(async function() {
|
|
|
|
this.driver = await StartDriver();
|
|
|
|
});
|
|
|
|
after(async function() {
|
|
|
|
await StopDriver(this.driver)
|
|
|
|
});
|
2019-02-10 05:20:37 +07:00
|
|
|
}
|
2019-01-30 22:47:03 +07:00
|
|
|
}
|