This repository has been 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
/
driverProvider.ts
148 lines (134 loc) · 3.89 KB
/
driverProvider.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
148
/**
* This is a base driver provider class.
* It is responsible for setting up the account object, tearing
* it down, and setting up the driver correctly.
*/
import * as q from 'q';
import {Builder, promise as wdpromise, Session, WebDriver} from 'selenium-webdriver';
import {BlockingProxyRunner} from '../bpRunner';
import {Config} from '../config';
export abstract class DriverProvider {
drivers_: WebDriver[];
config_: Config;
private bpRunner: BlockingProxyRunner;
constructor(config: Config) {
this.config_ = config;
this.drivers_ = [];
this.bpRunner = new BlockingProxyRunner(config);
}
/**
* Get all existing drivers.
*
* @public
* @return array of webdriver instances
*/
getExistingDrivers() {
return this.drivers_.slice(); // Create a shallow copy
}
getBPUrl() {
if (this.config_.blockingProxyUrl) {
return this.config_.blockingProxyUrl;
}
return `http://localhost:${this.bpRunner.port}`;
}
/**
* Create a new driver.
*
* @public
* @return webdriver instance
*/
getNewDriver() {
let builder: Builder;
if (this.config_.useBlockingProxy) {
builder =
new Builder().usingServer(this.getBPUrl()).withCapabilities(this.config_.capabilities);
} else {
builder = new Builder()
.usingServer(this.config_.seleniumAddress)
.usingWebDriverProxy(this.config_.webDriverProxy)
.withCapabilities(this.config_.capabilities);
}
if (this.config_.disableEnvironmentOverrides === true) {
builder.disableEnvironmentOverrides();
}
let newDriver = builder.build();
this.drivers_.push(newDriver);
return newDriver;
}
/**
* Quit a driver.
*
* @public
* @param webdriver instance
*/
quitDriver(driver: WebDriver): wdpromise.Promise<void> {
let driverIndex = this.drivers_.indexOf(driver);
if (driverIndex >= 0) {
this.drivers_.splice(driverIndex, 1);
}
if (driver.getSession() === undefined) {
return wdpromise.when(undefined);
} else {
return driver.getSession()
.then<void>((session_: Session) => {
if (session_) {
return driver.quit();
}
})
.catch<void>(function(err: Error) {});
}
}
/**
* Quits an array of drivers and returns a q promise instead of a webdriver one
*
* @param drivers {webdriver.WebDriver[]} The webdriver instances
*/
static quitDrivers(provider: DriverProvider, drivers: WebDriver[]): q.Promise<void> {
let deferred = q.defer<void>();
wdpromise
.all(drivers.map((driver: WebDriver) => {
return provider.quitDriver(driver);
}))
.then(
() => {
deferred.resolve();
},
() => {
deferred.resolve();
});
return deferred.promise;
}
/**
* Default update job method.
* @return a promise
*/
updateJob(update: any): q.Promise<any> {
return q.fcall(function() {});
};
/**
* Default setup environment method, common to all driver providers.
*/
setupEnv(): q.Promise<any> {
let driverPromise = this.setupDriverEnv();
if (this.config_.useBlockingProxy && !this.config_.blockingProxyUrl) {
// TODO(heathkit): If set, pass the webDriverProxy to BP.
return driverPromise.then(() => this.bpRunner.start());
}
return driverPromise;
};
/**
* Set up environment specific to a particular driver provider. Overridden
* by each driver provider.
*/
protected abstract setupDriverEnv(): q.Promise<any>;
/**
* Teardown and destroy the environment and do any associated cleanup.
* Shuts down the drivers.
*
* @public
* @return {q.Promise<any>} A promise which will resolve when the environment is down.
*/
teardownEnv(): q.Promise<any> {
return DriverProvider.quitDrivers(this, this.drivers_);
}
}