-
Notifications
You must be signed in to change notification settings - Fork 1
/
chrome_options.go
387 lines (317 loc) · 11.7 KB
/
chrome_options.go
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package webdriver
import (
"encoding/base64"
"errors"
"strings"
"github.com/mediabuyerbot/go-crx3"
"github.com/mediabuyerbot/go-webdriver/pkg/w3cproto"
)
const (
// List of command-line arguments to use when starting Chrome. Arguments with an associated value
// should be separated by a '=' sign (e.g., ['start-maximized', 'user-data-dir=/tmp/temp_profile']).
// See here for a list of Chrome arguments.
ChromeCapabilityArgsName = "args"
// Path to the Chrome executable to use (on Mac OS X, this should be the actual binary,
// not just the app. e.g., '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
ChromeCapabilityBinaryName = "binary"
// A list of Chrome extensions to install on startup. Each item in the list
// should be a base-64 encoded packed Chrome extension (.crx)
ChromeCapabilityExtensionName = "extensions"
// A dictionary with each entry consisting of the name of the preference and its value.
// These preferences are applied to the Local State file in the user data folder.
ChromeCapabilityLocalStateName = "localState"
// A dictionary with each entry consisting of the name of the preference and its value.
// These preferences are only applied to the user profile in use.
// See the 'Preferences' file in Chrome's user data directory for examples.
ChromeCapabilityPreferencesName = "prefs"
// If false, Chrome will be quit when ChromeDriver is killed, regardless of whether the session is quit.
// If true, Chrome will only be quit if the session is quit (or closed). Note, if true, and the session is not quit,
// ChromeDriver cannot clean up the temporary user data directory that the running Chrome instance is using.
ChromeCapabilityDetachName = "detach"
// An address of a Chrome debugger server to connect to, in the form of <hostname/ip:port>, e.g. '127.0.0.1:38947'
ChromeCapabilityDebuggerAddressName = "debuggerAddress"
// List of Chrome command line switches to exclude that ChromeDriver by default passes when starting Chrome.
// Do not prefix switches with --.
ChromeCapabilityExcludeSwitchesName = "excludeSwitches"
// Directory to store Chrome minidumps . (Supported only on Linux.)
ChromeCapabilityMiniDumpPathName = "minidumpPath"
// A dictionary with either a value for “deviceName,” or values for “deviceMetrics” and “userAgent.”
// Refer to Mobile Emulation for more information.
ChromeCapabilityMobileEmulationName = "mobileEmulation"
// An optional dictionary that specifies performance logging preferences. See below for more information.
ChromeCapabilityPerfLoggingPrefsName = "perfLoggingPrefs"
// A list of window types that will appear in the list of window handles.
// For access to <webview> elements, include "webview" in this list.
ChromeCapabilityWindowTypesName = "windowTypes"
ChromeOptionsKey = "goog:chromeOptions"
)
const (
zipExt = ".zip"
crxExt = ".crx"
)
var ErrBase64Format = errors.New("webdriver: string does not match format base64")
type ChromeOptionsBuilder struct {
//W3C Capabilities
capabilities w3cproto.Capabilities
// Chrome options
chromeCapabilities w3cproto.Capabilities
extensions []string
excludeSwitches []string
windowTypes []string
localState w3cproto.Capabilities
args []string
pref w3cproto.Capabilities
mobileEmulation *MobileEmulation
perfLoggingPref *PerfLoggingPreferences
firstMatch []w3cproto.Capabilities
}
func ChromeOptions() *ChromeOptionsBuilder {
return &ChromeOptionsBuilder{
capabilities: w3cproto.MakeCapabilities(),
chromeCapabilities: w3cproto.MakeCapabilities(),
extensions: make([]string, 0),
excludeSwitches: make([]string, 0),
windowTypes: make([]string, 0),
localState: w3cproto.MakeCapabilities(),
args: make([]string, 0),
pref: w3cproto.MakeCapabilities(),
firstMatch: make([]w3cproto.Capabilities, 0),
}
}
func (b *ChromeOptionsBuilder) SetBrowserName(name string) *ChromeOptionsBuilder {
_ = w3cproto.SetBrowserName(b.capabilities, name)
return b
}
func (b *ChromeOptionsBuilder) SetBrowserVersion(version string) *ChromeOptionsBuilder {
_ = w3cproto.SetBrowserVersion(b.capabilities, version)
return b
}
func (b *ChromeOptionsBuilder) SetPlatformName(platform string) *ChromeOptionsBuilder {
_ = w3cproto.SetPlatformName(b.capabilities, w3cproto.Platform(platform))
return b
}
func (b *ChromeOptionsBuilder) SetAcceptInsecureCerts(flag bool) *ChromeOptionsBuilder {
_ = w3cproto.SetAcceptInsecureCerts(b.capabilities, flag)
return b
}
func (b *ChromeOptionsBuilder) SetPageLoadStrategy(strategy string) *ChromeOptionsBuilder {
_ = w3cproto.SetPageLoadStrategy(b.capabilities, strategy)
return b
}
func (b *ChromeOptionsBuilder) SetWindowRect(flag bool) *ChromeOptionsBuilder {
_ = w3cproto.SetWindowRect(b.capabilities, flag)
return b
}
func (b *ChromeOptionsBuilder) SetProxy(proxy *w3cproto.Proxy) *ChromeOptionsBuilder {
_ = w3cproto.SetProxy(b.capabilities, proxy)
return b
}
func (b *ChromeOptionsBuilder) SetUnhandledPromptBehavior(prompt string) *ChromeOptionsBuilder {
_ = w3cproto.SetUnhandledPromptBehavior(b.capabilities, prompt)
return b
}
func (b *ChromeOptionsBuilder) SetTimeout(timeout w3cproto.Timeout) *ChromeOptionsBuilder {
_ = w3cproto.SetTimeout(b.capabilities, timeout)
return b
}
func (b *ChromeOptionsBuilder) SetDebuggerAddr(addr string) *ChromeOptionsBuilder {
_ = b.chromeCapabilities.Set(ChromeCapabilityDebuggerAddressName, addr)
return b
}
func (b *ChromeOptionsBuilder) SetDetach(flag bool) *ChromeOptionsBuilder {
_ = b.chromeCapabilities.Set(ChromeCapabilityDetachName, flag)
return b
}
func (b *ChromeOptionsBuilder) SetBinary(binPath string) *ChromeOptionsBuilder {
b.chromeCapabilities.Set(ChromeCapabilityBinaryName, binPath)
return b
}
func (b *ChromeOptionsBuilder) SetMiniDumpPath(path string) *ChromeOptionsBuilder {
b.chromeCapabilities.Set(ChromeCapabilityMiniDumpPathName, path)
return b
}
func (b *ChromeOptionsBuilder) SetLocalState(key string, value interface{}) *ChromeOptionsBuilder {
b.localState.Set(key, value)
return b
}
func (b *ChromeOptionsBuilder) SetPref(key string, value interface{}) *ChromeOptionsBuilder {
b.pref.Set(key, value)
return b
}
func (b *ChromeOptionsBuilder) AddArgument(arg ...string) *ChromeOptionsBuilder {
b.args = append(b.args, arg...)
return b
}
func (b *ChromeOptionsBuilder) AddExtension(base64 string) error {
if ok := IsBase64(base64); !ok {
return ErrBase64Format
}
b.extensions = append(b.extensions, base64)
return nil
}
func (b *ChromeOptionsBuilder) AddExcludeSwitches(exclude ...string) *ChromeOptionsBuilder {
for _, arg := range exclude {
if len(arg) == 0 {
continue
}
b.excludeSwitches = append(b.excludeSwitches, arg)
}
return b
}
func (b *ChromeOptionsBuilder) AddWindowTypes(types ...string) *ChromeOptionsBuilder {
for _, arg := range types {
if len(arg) == 0 {
continue
}
b.windowTypes = append(b.windowTypes, arg)
}
return b
}
func (b *ChromeOptionsBuilder) AddFirstMatch(key string, value interface{}) *ChromeOptionsBuilder {
if len(key) > 0 {
cap := w3cproto.MakeCapabilities()
cap.Set(key, value)
b.firstMatch = append(b.firstMatch, cap)
}
return b
}
func (b *ChromeOptionsBuilder) MobileEmulation() *MobileEmulation {
if b.mobileEmulation == nil {
b.mobileEmulation = &MobileEmulation{opts: w3cproto.MakeCapabilities()}
}
return b.mobileEmulation
}
func (b *ChromeOptionsBuilder) PerfLoggingPreferences() *PerfLoggingPreferences {
if b.perfLoggingPref == nil {
b.perfLoggingPref = &PerfLoggingPreferences{opts: w3cproto.MakeCapabilities()}
}
return b.perfLoggingPref
}
func (b *ChromeOptionsBuilder) Build() w3cproto.BrowserOptions {
if len(b.extensions) > 0 {
b.chromeCapabilities[ChromeCapabilityExtensionName] = b.extensions
}
if len(b.localState) > 0 {
b.chromeCapabilities[ChromeCapabilityLocalStateName] = b.localState
}
if len(b.excludeSwitches) > 0 {
b.chromeCapabilities[ChromeCapabilityExcludeSwitchesName] = b.excludeSwitches
}
if len(b.windowTypes) > 0 {
b.chromeCapabilities[ChromeCapabilityWindowTypesName] = b.windowTypes
}
if len(b.args) > 0 {
b.chromeCapabilities[ChromeCapabilityArgsName] = b.args
}
if len(b.pref) > 0 {
b.chromeCapabilities[ChromeCapabilityPreferencesName] = b.pref
}
if b.mobileEmulation != nil && len(b.mobileEmulation.opts) > 0 {
b.chromeCapabilities[ChromeCapabilityMobileEmulationName] = b.mobileEmulation.opts
}
if b.perfLoggingPref != nil && len(b.perfLoggingPref.opts) > 0 {
b.chromeCapabilities[ChromeCapabilityPerfLoggingPrefsName] = b.perfLoggingPref.opts
}
b.capabilities.Set(ChromeOptionsKey, b.chromeCapabilities)
return w3cproto.NewBrowserOptions(b.capabilities, b.firstMatch)
}
func LoadChromeExtension(extensionPath string) (base64 string, err error) {
extension := crx3.Extension(extensionPath)
if extension.IsZip() || extension.IsDir() {
err := extension.Pack(nil)
if err != nil {
return base64, err
}
crx := strings.TrimRight(extension.String(), "/")
crx = strings.TrimRight(crx, zipExt)
crx = crx + crxExt
extension = crx3.Extension(crx)
}
if !extension.IsCRX3() {
return base64, crx3.ErrUnsupportedFileFormat
}
b, err := extension.Base64()
if err != nil {
return base64, err
}
return string(b), nil
}
func IsBase64(s string) bool {
if len(s) == 0 {
return false
}
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}
const (
mobileEmulationDeviceName = "deviceName"
mobileEmulationDeviceMetrics = "deviceMetrics"
mobileEmulationUserAgent = "userAgent"
)
type MobileEmulation struct {
opts w3cproto.Capabilities
}
func (e *MobileEmulation) Set(key string, value interface{}) *MobileEmulation {
e.opts.Set(key, value)
return e
}
func (e *MobileEmulation) SetDeviceName(name string) *MobileEmulation {
return e.Set(mobileEmulationDeviceName, name)
}
func (e *MobileEmulation) SetDeviceMetrics(m *DeviceMetrics) *MobileEmulation {
if m == nil {
return e
}
return e.Set(mobileEmulationDeviceMetrics, m.Capabilities())
}
func (e *MobileEmulation) SetUserAgent(agent string) *MobileEmulation {
return e.Set(mobileEmulationUserAgent, agent)
}
const (
perfLoggingEnableNetwork = "enableNetwork"
perfLoggingEnableTimeline = "enableTimeline"
perfLoggingEnablePage = "enablePage"
perfLoggingTracingCategories = "tracingCategories"
perfLoggingBufferUsageReportingInterval = "bufferUsageReportingInterval"
)
type PerfLoggingPreferences struct {
opts w3cproto.Capabilities
}
func (pp *PerfLoggingPreferences) Set(key string, value interface{}) *PerfLoggingPreferences {
pp.opts.Set(key, value)
return pp
}
func (pp *PerfLoggingPreferences) EnableNetwork(flag bool) *PerfLoggingPreferences {
pp.opts.Set(perfLoggingEnableNetwork, flag)
return pp
}
func (pp *PerfLoggingPreferences) EnableTimeline(flag bool) *PerfLoggingPreferences {
pp.opts.Set(perfLoggingEnableTimeline, flag)
return pp
}
func (pp *PerfLoggingPreferences) EnablePage(flag bool) *PerfLoggingPreferences {
pp.opts.Set(perfLoggingEnablePage, flag)
return pp
}
func (pp *PerfLoggingPreferences) TracingCategories(s string) *PerfLoggingPreferences {
pp.opts.Set(perfLoggingTracingCategories, s)
return pp
}
func (pp *PerfLoggingPreferences) BufferUsageReportingIntervalMillis(v uint) *PerfLoggingPreferences {
pp.opts.Set(perfLoggingBufferUsageReportingInterval, v)
return pp
}
type DeviceMetrics struct {
Width uint `json:"width"`
Height uint `json:"height"`
PixelRatio float64 `json:"pixelRatio"`
Touch bool `json:"touch,omitempty"`
}
func (dm *DeviceMetrics) Capabilities() w3cproto.Capabilities {
return w3cproto.Capabilities{
"width": dm.Width,
"height": dm.Height,
"pixelRatio": dm.PixelRatio,
"touch": dm.Touch,
}
}