-
Notifications
You must be signed in to change notification settings - Fork 0
/
safari.js
264 lines (227 loc) · 7.61 KB
/
safari.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
/**
* @fileoverview Defines a WebDriver client for Safari.
*/
'use strict';
const http = require('./http');
const io = require('./io');
const {Capabilities, Capability} = require('./lib/capabilities');
const command = require('./lib/command');
const error = require('./lib/error');
const logging = require('./lib/logging');
const promise = require('./lib/promise');
const Symbols = require('./lib/symbols');
const webdriver = require('./lib/webdriver');
const portprober = require('./net/portprober');
const remote = require('./remote');
/**
* @return {string} .
* @throws {Error}
*/
function findSafariDriver() {
let exe = io.findInPath('safaridriver', true);
if (!exe) {
throw Error(
`The safaridriver executable could not be found on the current PATH.
Please ensure you are using Safari 10.0 or above.`);
}
return exe;
}
/**
* Creates {@link selenium-webdriver/remote.DriverService} instances that manage
* a [safaridriver] server in a child process.
*
* [safaridriver]: https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewInSafari/Articles/Safari_10_0.html#//apple_ref/doc/uid/TP40014305-CH11-DontLinkElementID_28
*/
class ServiceBuilder extends remote.DriverService.Builder {
/**
* @param {string=} opt_exe Path to the server executable to use. If omitted,
* the builder will attempt to locate the safaridriver on the system PATH.
*/
constructor(opt_exe) {
super(opt_exe || findSafariDriver());
this.setLoopback(true); // Required.
}
}
const OPTIONS_CAPABILITY_KEY = 'safari.options';
const TECHNOLOGY_PREVIEW_OPTIONS_KEY = 'technologyPreview';
/**
* Configuration options specific to the {@link Driver SafariDriver}.
*/
class Options {
constructor() {
/** @private {Object<string, *>} */
this.options_ = null;
/** @private {./lib/logging.Preferences} */
this.logPrefs_ = null;
/** @private {?./lib/capabilities.ProxyConfig} */
this.proxy_ = null;
}
/**
* Extracts the SafariDriver specific options from the given capabilities
* object.
* @param {!Capabilities} capabilities The capabilities object.
* @return {!Options} The SafariDriver options.
*/
static fromCapabilities(capabilities) {
var options = new Options();
var o = capabilities.get(OPTIONS_CAPABILITY_KEY);
if (o instanceof Options) {
options = o;
} else if (o) {
options.setCleanSession(o.cleanSession);
options.setTechnologyPreview(o[TECHNOLOGY_PREVIEW_OPTIONS_KEY]);
}
if (capabilities.has(Capability.PROXY)) {
options.setProxy(capabilities.get(Capability.PROXY));
}
if (capabilities.has(Capability.LOGGING_PREFS)) {
options.setLoggingPrefs(capabilities.get(Capability.LOGGING_PREFS));
}
return options;
}
/**
* Sets whether to force Safari to start with a clean session. Enabling this
* option will cause all global browser data to be deleted.
* @param {boolean} clean Whether to make sure the session has no cookies,
* cache entries, local storage, or databases.
* @return {!Options} A self reference.
*/
setCleanSession(clean) {
if (!this.options_) {
this.options_ = {};
}
this.options_['cleanSession'] = clean;
return this;
}
/**
* Sets the logging preferences for the new session.
* @param {!./lib/logging.Preferences} prefs The logging preferences.
* @return {!Options} A self reference.
*/
setLoggingPrefs(prefs) {
this.logPrefs_ = prefs;
return this;
}
/**
* Sets the proxy to use.
*
* @param {./lib/capabilities.ProxyConfig} proxy The proxy configuration to use.
* @return {!Options} A self reference.
*/
setProxy(proxy) {
this.proxy_ = proxy;
return this;
}
/**
* Instruct the SafariDriver to use the Safari Technology Preview if true.
* Otherwise, use the release version of Safari. Defaults to using the release version of Safari.
*
* @param {boolean} useTechnologyPreview
* @return {!Options} A self reference.
*/
setTechnologyPreview(useTechnologyPreview) {
if (!this.options_) {
this.options_ = {};
}
this.options_[TECHNOLOGY_PREVIEW_OPTIONS_KEY] = !!useTechnologyPreview;
return this;
}
/**
* Converts this options instance to a {@link Capabilities} object.
* @param {Capabilities=} opt_capabilities The capabilities to
* merge these options into, if any.
* @return {!Capabilities} The capabilities.
*/
toCapabilities(opt_capabilities) {
var caps = opt_capabilities || Capabilities.safari();
if (this.logPrefs_) {
caps.set(Capability.LOGGING_PREFS, this.logPrefs_);
}
if (this.proxy_) {
caps.set(Capability.PROXY, this.proxy_);
}
if (this.options_) {
caps.set(OPTIONS_CAPABILITY_KEY, this);
}
return caps;
}
/**
* Converts this instance to its JSON wire protocol representation. Note this
* function is an implementation detail not intended for general use.
* @return {!Object<string, *>} The JSON wire protocol representation of this
* instance.
*/
[Symbols.serialize]() {
return this.options_ || {};
}
}
/**
* @param {(Options|Object<string, *>)=} o The options object
* @return {boolean}
*/
function useTechnologyPreview(o) {
if (o instanceof Options) {
return !!(o.options_ && o.options_[TECHNOLOGY_PREVIEW_OPTIONS_KEY]);
}
if (o && typeof o === 'object') {
return !!o[TECHNOLOGY_PREVIEW_OPTIONS_KEY];
}
return false;
}
const SAFARIDRIVER_TECHNOLOGY_PREVIEW_EXE = '/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver';
/**
* A WebDriver client for Safari. This class should never be instantiated
* directly; instead, use the {@linkplain ./builder.Builder Builder}:
*
* var driver = new Builder()
* .forBrowser('safari')
* .build();
*
*/
class Driver extends webdriver.WebDriver {
/**
* Creates a new Safari session.
*
* @param {(Options|Capabilities)=} opt_config The configuration
* options for the new session.
* @param {promise.ControlFlow=} opt_flow The control flow to create
* the driver under.
* @return {!Driver} A new driver instance.
*/
static createSession(opt_config, opt_flow) {
let caps, exe;
if (opt_config instanceof Options) {
caps = opt_config.toCapabilities();
} else {
caps = opt_config || Capabilities.safari();
}
if (useTechnologyPreview(caps.get(OPTIONS_CAPABILITY_KEY))) {
exe = SAFARIDRIVER_TECHNOLOGY_PREVIEW_EXE;
}
let service = new ServiceBuilder(exe).build();
let executor = new http.Executor(
service.start().then(url => new http.HttpClient(url)));
return /** @type {!Driver} */(webdriver.WebDriver.createSession(
executor, caps, opt_flow, this, () => service.kill()));
}
}
// Public API
exports.Driver = Driver;
exports.Options = Options;
exports.ServiceBuilder = ServiceBuilder;