forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafari.js
More file actions
168 lines (147 loc) · 4.87 KB
/
Copy pathsafari.js
File metadata and controls
168 lines (147 loc) · 4.87 KB
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
// 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 remote = require('./remote')
const webdriver = require('./lib/webdriver')
const { Browser, Capabilities } = require('./lib/capabilities')
/**
* _Synchronously_ attempts to locate the IE driver executable on the current
* system.
*
* @return {?string} the located executable, or `null`.
*/
function locateSynchronously() {
return process.platform === 'darwin'
? io.findInPath('safaridriver', true)
: null
}
/**
* @return {string} .
* @throws {Error}
*/
function findSafariDriver() {
let exe = locateSynchronously()
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 extends Capabilities {
/**
* @param {(Capabilities|Map<string, ?>|Object)=} other Another set of
* capabilities to initialize this instance from.
*/
constructor(other = undefined) {
super(other)
/** @private {!Object} */
this.options_ = this.get(OPTIONS_CAPABILITY_KEY) || {}
this.set(OPTIONS_CAPABILITY_KEY, this.options_)
this.setBrowserName(Browser.SAFARI)
}
/**
* 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) {
this.options_[TECHNOLOGY_PREVIEW_OPTIONS_KEY] = !!useTechnologyPreview
return this
}
}
/**
* @param {(Capabilities|Object<string, *>)=} o The options object
* @return {boolean}
*/
function useTechnologyPreview(o) {
if (o instanceof Capabilities) {
let options = o.get(OPTIONS_CAPABILITY_KEY)
return !!(options && 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)=} options The configuration options.
* @return {!Driver} A new driver instance.
*/
static createSession(options) {
let caps = options || new Options()
let exe
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} */ (super.createSession(executor, caps, () =>
service.kill()
))
}
}
// Public API
exports.Driver = Driver
exports.Options = Options
exports.ServiceBuilder = ServiceBuilder
exports.locateSynchronously = locateSynchronously