UNPKG

4.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*
4 * This is an implementation of the Direct Driver Provider.
5 * It is responsible for setting up the account object, tearing
6 * it down, and setting up the driver correctly.
7 */
8const fs = require("fs");
9const path = require("path");
10const q = require("q");
11const selenium_webdriver_1 = require("selenium-webdriver");
12const chrome_1 = require("selenium-webdriver/chrome");
13const exitCodes_1 = require("../exitCodes");
14const logger_1 = require("../logger");
15const driverProvider_1 = require("./driverProvider");
16const SeleniumConfig = require('webdriver-manager/built/lib/config').Config;
17let logger = new logger_1.Logger('direct');
18class Direct extends driverProvider_1.DriverProvider {
19 constructor(config) {
20 super(config);
21 }
22 /**
23 * Configure and launch (if applicable) the object's environment.
24 * @return {q.promise} A promise which will resolve when the environment is
25 * ready to test.
26 */
27 setupDriverEnv() {
28 switch (this.config_.capabilities.browserName) {
29 case 'chrome':
30 logger.info('Using ChromeDriver directly...');
31 break;
32 case 'firefox':
33 logger.info('Using FirefoxDriver directly...');
34 break;
35 default:
36 throw new exitCodes_1.BrowserError(logger, 'browserName ' + this.config_.capabilities.browserName +
37 ' is not supported with directConnect.');
38 }
39 return q.fcall(function () { });
40 }
41 /**
42 * Create a new driver.
43 *
44 * @public
45 * @override
46 * @return webdriver instance
47 */
48 getNewDriver() {
49 let driver;
50 switch (this.config_.capabilities.browserName) {
51 case 'chrome':
52 let chromeDriverFile;
53 if (this.config_.chromeDriver) {
54 chromeDriverFile = this.config_.chromeDriver;
55 }
56 else {
57 try {
58 let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
59 let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
60 chromeDriverFile = updateConfig.chrome.last;
61 }
62 catch (e) {
63 throw new exitCodes_1.BrowserError(logger, 'Could not find update-config.json. ' +
64 'Run \'webdriver-manager update\' to download binaries.');
65 }
66 }
67 if (!fs.existsSync(chromeDriverFile)) {
68 throw new exitCodes_1.BrowserError(logger, 'Could not find chromedriver at ' + chromeDriverFile +
69 '. Run \'webdriver-manager update\' to download binaries.');
70 }
71 let chromeService = new chrome_1.ServiceBuilder(chromeDriverFile).build();
72 // driver = ChromeDriver.createSession(new Capabilities(this.config_.capabilities),
73 // chromeService);
74 // TODO(ralphj): fix typings
75 driver =
76 require('selenium-webdriver/chrome')
77 .Driver.createSession(new selenium_webdriver_1.Capabilities(this.config_.capabilities), chromeService);
78 break;
79 case 'firefox':
80 let geckoDriverFile;
81 try {
82 let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
83 let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
84 geckoDriverFile = updateConfig.gecko.last;
85 }
86 catch (e) {
87 throw new exitCodes_1.BrowserError(logger, 'Could not find update-config.json. ' +
88 'Run \'webdriver-manager update\' to download binaries.');
89 }
90 // TODO (mgiambalvo): Turn this into an import when the selenium typings are updated.
91 const FirefoxServiceBuilder = require('selenium-webdriver/firefox').ServiceBuilder;
92 let firefoxService = new FirefoxServiceBuilder(geckoDriverFile).build();
93 // TODO(mgiambalvo): Fix typings.
94 driver =
95 require('selenium-webdriver/firefox')
96 .Driver.createSession(new selenium_webdriver_1.Capabilities(this.config_.capabilities), firefoxService);
97 break;
98 default:
99 throw new exitCodes_1.BrowserError(logger, 'browserName ' + this.config_.capabilities.browserName +
100 ' is not supported with directConnect.');
101 }
102 this.drivers_.push(driver);
103 return driver;
104 }
105}
106exports.Direct = Direct;
107//# sourceMappingURL=direct.js.map
\No newline at end of file