1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const fs = require("fs");
|
12 | const path = require("path");
|
13 | const q = require("q");
|
14 | const exitCodes_1 = require("../exitCodes");
|
15 | const logger_1 = require("../logger");
|
16 | const driverProvider_1 = require("./driverProvider");
|
17 | const SeleniumConfig = require('webdriver-manager/built/lib/config').Config;
|
18 | const SeleniumChrome = require('webdriver-manager/built/lib/binaries/chrome_driver').ChromeDriver;
|
19 | const SeleniumStandAlone = require('webdriver-manager/built/lib/binaries/standalone').StandAlone;
|
20 | const remote = require('selenium-webdriver/remote');
|
21 | let logger = new logger_1.Logger('local');
|
22 | class Local extends driverProvider_1.DriverProvider {
|
23 | constructor(config) {
|
24 | super(config);
|
25 | this.server_ = null;
|
26 | }
|
27 | |
28 |
|
29 |
|
30 |
|
31 | addDefaultBinaryLocs_() {
|
32 | if (!this.config_.seleniumServerJar) {
|
33 | logger.debug('Attempting to find the SeleniumServerJar in the default ' +
|
34 | 'location used by webdriver-manager');
|
35 | try {
|
36 | let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
|
37 | let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
|
38 | this.config_.seleniumServerJar = updateConfig.standalone.last;
|
39 | }
|
40 | catch (err) {
|
41 | throw new exitCodes_1.BrowserError(logger, 'No update-config.json found.' +
|
42 | ' Run \'webdriver-manager update\' to download binaries.');
|
43 | }
|
44 | }
|
45 | if (!fs.existsSync(this.config_.seleniumServerJar)) {
|
46 | throw new exitCodes_1.BrowserError(logger, 'No selenium server jar found at ' + this.config_.seleniumServerJar +
|
47 | '. Run \'webdriver-manager update\' to download binaries.');
|
48 | }
|
49 | if (this.config_.capabilities.browserName === 'chrome') {
|
50 | if (!this.config_.chromeDriver) {
|
51 | logger.debug('Attempting to find the chromedriver binary in the default ' +
|
52 | 'location used by webdriver-manager');
|
53 | try {
|
54 | let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
|
55 | let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
|
56 | this.config_.chromeDriver = updateConfig.chrome.last;
|
57 | }
|
58 | catch (err) {
|
59 | throw new exitCodes_1.BrowserError(logger, 'No update-config.json found. ' +
|
60 | 'Run \'webdriver-manager update\' to download binaries.');
|
61 | }
|
62 | }
|
63 |
|
64 | if (!fs.existsSync(this.config_.chromeDriver)) {
|
65 | if (fs.existsSync(this.config_.chromeDriver + '.exe')) {
|
66 | this.config_.chromeDriver += '.exe';
|
67 | }
|
68 | else {
|
69 | throw new exitCodes_1.BrowserError(logger, 'Could not find chromedriver at ' + this.config_.chromeDriver +
|
70 | '. Run \'webdriver-manager update\' to download binaries.');
|
71 | }
|
72 | }
|
73 | }
|
74 | if (this.config_.capabilities.browserName === 'firefox') {
|
75 | if (!this.config_.geckoDriver) {
|
76 | logger.debug('Attempting to find the gecko driver binary in the default ' +
|
77 | 'location used by webdriver-manager');
|
78 | try {
|
79 | let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
|
80 | let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
|
81 | this.config_.geckoDriver = updateConfig.gecko.last;
|
82 | }
|
83 | catch (err) {
|
84 | throw new exitCodes_1.BrowserError(logger, 'No update-config.json found. ' +
|
85 | 'Run \'webdriver-manager update\' to download binaries.');
|
86 | }
|
87 | }
|
88 |
|
89 | if (!fs.existsSync(this.config_.geckoDriver)) {
|
90 | if (fs.existsSync(this.config_.geckoDriver + '.exe')) {
|
91 | this.config_.geckoDriver += '.exe';
|
92 | }
|
93 | else {
|
94 | throw new exitCodes_1.BrowserError(logger, 'Could not find gecko driver at ' + this.config_.geckoDriver +
|
95 | '. Run \'webdriver-manager update\' to download binaries.');
|
96 | }
|
97 | }
|
98 | }
|
99 | }
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | setupDriverEnv() {
|
107 | this.addDefaultBinaryLocs_();
|
108 | logger.info('Starting selenium standalone server...');
|
109 | let serverConf = this.config_.localSeleniumStandaloneOpts || {};
|
110 |
|
111 |
|
112 | if (serverConf.args === undefined) {
|
113 | serverConf.args = this.config_.seleniumArgs || [];
|
114 | }
|
115 | if (serverConf.jvmArgs === undefined) {
|
116 | serverConf.jvmArgs = this.config_.jvmArgs || [];
|
117 | }
|
118 | else {
|
119 | if (!Array.isArray(serverConf.jvmArgs)) {
|
120 | throw new exitCodes_1.ConfigError(logger, 'jvmArgs should be an array.');
|
121 | }
|
122 | }
|
123 | if (serverConf.port === undefined) {
|
124 | serverConf.port = this.config_.seleniumPort;
|
125 | }
|
126 |
|
127 | if (this.config_.chromeDriver) {
|
128 | serverConf.jvmArgs.push('-Dwebdriver.chrome.driver=' + this.config_.chromeDriver);
|
129 | }
|
130 | this.server_ = new remote.SeleniumServer(this.config_.seleniumServerJar, serverConf);
|
131 | let deferred = q.defer();
|
132 |
|
133 | this.server_.start(this.config_.seleniumServerStartTimeout)
|
134 | .then((url) => {
|
135 | logger.info('Selenium standalone server started at ' + url);
|
136 | return this.server_.address();
|
137 | })
|
138 | .then((address) => {
|
139 | this.config_.seleniumAddress = address;
|
140 | deferred.resolve();
|
141 | })
|
142 | .catch((err) => {
|
143 | deferred.reject(err);
|
144 | });
|
145 | return deferred.promise;
|
146 | }
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | teardownEnv() {
|
157 | return super.teardownEnv().then(() => {
|
158 | logger.info('Shutting down selenium standalone server.');
|
159 | return this.server_.stop();
|
160 | });
|
161 | }
|
162 | }
|
163 | exports.Local = Local;
|
164 |
|
\ | No newline at end of file |