UNPKG

7.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*
4 * This is an implementation of the Local Driver Provider.
5 * It is responsible for setting up the account object, tearing
6 * it down, and setting up the driver correctly.
7 *
8 * TODO - it would be nice to do this in the launcher phase,
9 * so that we only start the local selenium once per entire launch.
10 */
11const fs = require("fs");
12const path = require("path");
13const q = require("q");
14const exitCodes_1 = require("../exitCodes");
15const logger_1 = require("../logger");
16const driverProvider_1 = require("./driverProvider");
17const SeleniumConfig = require('webdriver-manager/built/lib/config').Config;
18const SeleniumChrome = require('webdriver-manager/built/lib/binaries/chrome_driver').ChromeDriver;
19const SeleniumStandAlone = require('webdriver-manager/built/lib/binaries/standalone').StandAlone;
20const remote = require('selenium-webdriver/remote');
21let logger = new logger_1.Logger('local');
22class Local extends driverProvider_1.DriverProvider {
23 constructor(config) {
24 super(config);
25 this.server_ = null;
26 }
27 /**
28 * Helper to locate the default jar path if none is provided by the user.
29 * @private
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 // Check if file exists, if not try .exe or fail accordingly
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 // Check if file exists, if not try .exe or fail accordingly
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 * Configure and launch (if applicable) the object's environment.
102 * @public
103 * @return {q.promise} A promise which will resolve when the environment is
104 * ready to test.
105 */
106 setupDriverEnv() {
107 this.addDefaultBinaryLocs_();
108 logger.info('Starting selenium standalone server...');
109 let serverConf = this.config_.localSeleniumStandaloneOpts || {};
110 // If args or port is not set use seleniumArgs and seleniumPort
111 // for backward compatibility
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 // configure server
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 // start local server, grab hosted address, and resolve promise
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 * Teardown and destroy the environment and do any associated cleanup.
149 * Shuts down the drivers and server.
150 *
151 * @public
152 * @override
153 * @return {q.promise} A promise which will resolve when the environment
154 * is down.
155 */
156 teardownEnv() {
157 return super.teardownEnv().then(() => {
158 logger.info('Shutting down selenium standalone server.');
159 return this.server_.stop();
160 });
161 }
162}
163exports.Local = Local;
164//# sourceMappingURL=local.js.map
\No newline at end of file