UNPKG

3.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * This is a base driver provider class.
5 * It is responsible for setting up the account object, tearing
6 * it down, and setting up the driver correctly.
7 */
8const q = require("q");
9const selenium_webdriver_1 = require("selenium-webdriver");
10const bpRunner_1 = require("../bpRunner");
11class DriverProvider {
12 constructor(config) {
13 this.config_ = config;
14 this.drivers_ = [];
15 this.bpRunner = new bpRunner_1.BlockingProxyRunner(config);
16 }
17 /**
18 * Get all existing drivers.
19 *
20 * @public
21 * @return array of webdriver instances
22 */
23 getExistingDrivers() {
24 return this.drivers_.slice(); // Create a shallow copy
25 }
26 getBPUrl() {
27 if (this.config_.blockingProxyUrl) {
28 return this.config_.blockingProxyUrl;
29 }
30 return `http://localhost:${this.bpRunner.port}`;
31 }
32 /**
33 * Create a new driver.
34 *
35 * @public
36 * @return webdriver instance
37 */
38 getNewDriver() {
39 let builder;
40 if (this.config_.useBlockingProxy) {
41 builder =
42 new selenium_webdriver_1.Builder().usingServer(this.getBPUrl()).withCapabilities(this.config_.capabilities);
43 }
44 else {
45 builder = new selenium_webdriver_1.Builder()
46 .usingServer(this.config_.seleniumAddress)
47 .usingWebDriverProxy(this.config_.webDriverProxy)
48 .withCapabilities(this.config_.capabilities);
49 }
50 if (this.config_.disableEnvironmentOverrides === true) {
51 builder.disableEnvironmentOverrides();
52 }
53 let newDriver = builder.build();
54 this.drivers_.push(newDriver);
55 return newDriver;
56 }
57 /**
58 * Quit a driver.
59 *
60 * @public
61 * @param webdriver instance
62 */
63 quitDriver(driver) {
64 let driverIndex = this.drivers_.indexOf(driver);
65 if (driverIndex >= 0) {
66 this.drivers_.splice(driverIndex, 1);
67 }
68 if (driver.getSession() === undefined) {
69 return selenium_webdriver_1.promise.when(undefined);
70 }
71 else {
72 return driver.getSession()
73 .then((session_) => {
74 if (session_) {
75 return driver.quit();
76 }
77 })
78 .catch(function (err) { });
79 }
80 }
81 /**
82 * Quits an array of drivers and returns a q promise instead of a webdriver one
83 *
84 * @param drivers {webdriver.WebDriver[]} The webdriver instances
85 */
86 static quitDrivers(provider, drivers) {
87 let deferred = q.defer();
88 selenium_webdriver_1.promise
89 .all(drivers.map((driver) => {
90 return provider.quitDriver(driver);
91 }))
92 .then(() => {
93 deferred.resolve();
94 }, () => {
95 deferred.resolve();
96 });
97 return deferred.promise;
98 }
99 /**
100 * Default update job method.
101 * @return a promise
102 */
103 updateJob(update) {
104 return q.fcall(function () { });
105 }
106 ;
107 /**
108 * Default setup environment method, common to all driver providers.
109 */
110 setupEnv() {
111 let driverPromise = this.setupDriverEnv();
112 if (this.config_.useBlockingProxy && !this.config_.blockingProxyUrl) {
113 // TODO(heathkit): If set, pass the webDriverProxy to BP.
114 return driverPromise.then(() => this.bpRunner.start());
115 }
116 return driverPromise;
117 }
118 ;
119 /**
120 * Teardown and destroy the environment and do any associated cleanup.
121 * Shuts down the drivers.
122 *
123 * @public
124 * @return {q.Promise<any>} A promise which will resolve when the environment is down.
125 */
126 teardownEnv() {
127 return DriverProvider.quitDrivers(this, this.drivers_);
128 }
129}
130exports.DriverProvider = DriverProvider;
131//# sourceMappingURL=driverProvider.js.map
\No newline at end of file