This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathlocal.ts
147 lines (130 loc) · 5.12 KB
/
local.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* This is an implementation of the Local Driver Provider.
* It is responsible for setting up the account object, tearing
* it down, and setting up the driver correctly.
*
* TODO - it would be nice to do this in the launcher phase,
* so that we only start the local selenium once per entire launch.
*/
import * as fs from 'fs';
import {SeleniumServer} from 'selenium-webdriver/remote';
import {ChromeDriver, GeckoDriver, SeleniumServer as WdmSeleniumServer} from 'webdriver-manager';
import {Config} from '../config';
import {BrowserError, ConfigError} from '../exitCodes';
import {Logger} from '../logger';
import {DriverProvider} from './driverProvider';
let logger = new Logger('local');
export class Local extends DriverProvider {
server_: SeleniumServer;
constructor(config: Config) {
super(config);
this.server_ = null;
}
/**
* Helper to locate the default jar path if none is provided by the user.
* @private
*/
addDefaultBinaryLocs_(): void {
if (!this.config_.seleniumServerJar) {
logger.debug(
'Attempting to find the SeleniumServerJar in the default ' +
'location used by webdriver-manager');
try {
this.config_.seleniumServerJar = new WdmSeleniumServer().getBinaryPath();
} catch (err) {
throw new BrowserError(logger, 'Run \'webdriver-manager update\' to download binaries.');
}
}
if (!fs.existsSync(this.config_.seleniumServerJar)) {
throw new BrowserError(
logger,
'No selenium server jar found at ' + this.config_.seleniumServerJar +
'. Run \'webdriver-manager update\' to download binaries.');
}
if (this.config_.capabilities.browserName === 'chrome') {
if (!this.config_.chromeDriver) {
logger.debug(
'Attempting to find the chromedriver binary in the default ' +
'location used by webdriver-manager');
try {
this.config_.chromeDriver = new ChromeDriver().getBinaryPath();
} catch (err) {
throw new BrowserError(logger, 'Run \'webdriver-manager update\' to download binaries.');
}
}
// Check if file exists, if not try .exe or fail accordingly
if (!fs.existsSync(this.config_.chromeDriver)) {
if (fs.existsSync(this.config_.chromeDriver + '.exe')) {
this.config_.chromeDriver += '.exe';
} else {
throw new BrowserError(
logger,
'Could not find chromedriver at ' + this.config_.chromeDriver +
'. Run \'webdriver-manager update\' to download binaries.');
}
}
}
if (this.config_.capabilities.browserName === 'firefox') {
if (!this.config_.geckoDriver) {
logger.debug(
'Attempting to find the gecko driver binary in the default ' +
'location used by webdriver-manager');
try {
this.config_.geckoDriver = new GeckoDriver().getBinaryPath();
} catch (err) {
throw new BrowserError(logger, 'Run \'webdriver-manager update\' to download binaries.');
}
}
// Check if file exists, if not try .exe or fail accordingly
if (!fs.existsSync(this.config_.geckoDriver)) {
if (fs.existsSync(this.config_.geckoDriver + '.exe')) {
this.config_.geckoDriver += '.exe';
} else {
throw new BrowserError(
logger,
'Could not find gecko driver at ' + this.config_.geckoDriver +
'. Run \'webdriver-manager update\' to download binaries.');
}
}
}
}
/**
* Configure and launch (if applicable) the object's environment.
* @public
* @return {Promise} A promise which will resolve when the environment is
* ready to test.
*/
async setupDriverEnv(): Promise<any> {
this.addDefaultBinaryLocs_();
logger.info('Starting selenium standalone server...');
let serverConf = this.config_.localSeleniumStandaloneOpts || {};
// If args or port is not set use seleniumArgs and seleniumPort
// for backward compatibility
if (serverConf.args === undefined) {
serverConf.args = this.config_.seleniumArgs || [];
}
if (serverConf.jvmArgs === undefined) {
serverConf.jvmArgs = this.config_.jvmArgs || [];
} else {
if (!Array.isArray(serverConf.jvmArgs)) {
throw new ConfigError(logger, 'jvmArgs should be an array.');
}
}
if (serverConf.port === undefined) {
serverConf.port = this.config_.seleniumPort;
}
// configure server
if (this.config_.chromeDriver) {
serverConf.jvmArgs.push('-Dwebdriver.chrome.driver=' + this.config_.chromeDriver);
}
if (this.config_.geckoDriver) {
serverConf.jvmArgs.push('-Dwebdriver.gecko.driver=' + this.config_.geckoDriver);
}
this.server_ = new SeleniumServer(this.config_.seleniumServerJar, serverConf);
// start local server, grab hosted address, and resolve promise
const url = await this.server_.start(this.config_.seleniumServerStartTimeout);
logger.info('Selenium standalone server started at ' + url);
const address = await this.server_.address();
this.config_.seleniumAddress = address;
}
}