forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capabilities.go
executable file
·165 lines (134 loc) · 3.88 KB
/
capabilities.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
package goselenium
import (
"encoding/json"
)
// Browser defines a supported selenium enabled browser.
type Browser interface {
BrowserName() string
}
// Browser represents a browser to run within Selenium.
type browser struct {
browserName string
}
// ChromeOptions defines chromedriver-specific options
type ChromeOptions interface {
Binary() string
SetBinary(binary string)
Args() []string
SetArgs(args []string)
AsMap() map[string]interface{}
}
type chromeOptions struct {
binary string
args []string
}
// Binary returns the executable chromedriver will launch when a new session is created.
func (co *chromeOptions) Binary() string {
return co.binary
}
// SetBinary defines the executable chromedriver will launch when a new session is created.
func (co *chromeOptions) SetBinary(binary string) {
co.binary = binary
}
// Args returns the command-line argument chromedriver will pass to the binary.
func (co *chromeOptions) Args() []string {
return co.args
}
// SetArgs defines the command-line argument chromedriver will pass to the binary.
func (co *chromeOptions) SetArgs(args []string) {
co.args = args
}
// AsMap returns a version of chrome options suitable for sending
// over to chromedriver over the wire.
func (co *chromeOptions) AsMap() map[string]interface{} {
return map[string]interface{}{
"binary": co.Binary(),
"args": co.Args(),
"windowTypes": []string{"webview"},
}
}
// BrowserName returns the browser name assigned to the current browser object.
func (b browser) BrowserName() string {
return b.browserName
}
// FirefoxBrowser returns a Firefox browser object.
func FirefoxBrowser() Browser {
return browser{"firefox"}
}
// ChromeBrowser returns a Chrome browser object.
func ChromeBrowser() Browser {
return browser{"chrome"}
}
// AndroidBrowser returns an Android browser object.
func AndroidBrowser() Browser {
return browser{"android"}
}
// HTMLUnitBrowser returns a HTMLUnit browser object.
func HTMLUnitBrowser() Browser {
return browser{"htmlunit"}
}
// InternetExplorerBrowser returns an IE browser object.
func InternetExplorerBrowser() Browser {
return browser{"internetexplorer"}
}
// IPhoneBrowser returns an IPhone browser object.
func IPhoneBrowser() Browser {
return browser{"iphone"}
}
// IPadBrowser returns an IPad browser object.
func IPadBrowser() Browser {
return browser{"ipad"}
}
// OperaBrowser returns an Opera browser object.
func OperaBrowser() Browser {
return browser{"opera"}
}
// SafariBrowser returns a Safari browser object.
func SafariBrowser() Browser {
return browser{"safari"}
}
// Capabilities represents the capabilities defined in the W3C specification.
// The main capability is the browser, which can be set by calling one of the
// \wBrowser\(\) methods.
type Capabilities struct {
browser Browser
chromeOptions ChromeOptions
}
// Browser yields the browser capability assigned to the current Capabilities
// object..
func (c *Capabilities) Browser() Browser {
if c.browser != nil {
return c.browser
}
return browser{}
}
// SetBrowser sets the browser capability to be one of the allowed browsers.
func (c *Capabilities) SetBrowser(b Browser) {
c.browser = b
}
// NewChromeOptions returns an empty set of ChromeOptions
func NewChromeOptions() ChromeOptions {
return &chromeOptions{
binary: "",
args: []string{},
}
}
// SetChromeOptions sets the chrome options for the current Capabilities object.
func (c *Capabilities) SetChromeOptions(co ChromeOptions) {
c.chromeOptions = co
}
func (c *Capabilities) toJSON() (string, error) {
capabilities := map[string]map[string]interface{}{
"desiredCapabilities": {
"browserName": c.browser.BrowserName(),
},
}
if c.chromeOptions != nil {
capabilities["desiredCapabilities"]["chromeOptions"] = c.chromeOptions.AsMap()
}
capabilitiesJSON, err := json.Marshal(capabilities)
if err != nil {
return "", err
}
return string(capabilitiesJSON), nil
}