forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_driver.go
executable file
·249 lines (208 loc) · 5.61 KB
/
remote_driver.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
package goselenium
import (
"bytes"
"encoding/json"
"io"
"strings"
"errors"
)
// NewSeleniumWebDriver creates a new instance of a Selenium web driver with a
// service URL (usually http://domain:port/wd/hub) and a Capabilities object.
// This method will return validation errors if the Selenium URL is invalid or
// the required capabilities (BrowserName) are not set.
func NewSeleniumWebDriver(serviceURL string, capabilities Capabilities) (WebDriver, error) {
if serviceURL == "" {
return nil, errors.New("Provided Selenium URL is invalid")
}
urlValid := strings.HasPrefix(serviceURL, "http://") || strings.HasPrefix(serviceURL, "https://")
if !urlValid {
return nil, errors.New("Provided Selenium URL is invalid.")
}
browser := capabilities.Browser()
hasBrowserCapability := browser.BrowserName() != ""
if !hasBrowserCapability {
return nil, errors.New("An invalid capabilities object was provided.")
}
if strings.HasSuffix(serviceURL, "/") {
serviceURL = strings.TrimSuffix(serviceURL, "/")
}
driver := &seleniumWebDriver{
seleniumURL: serviceURL,
capabilities: &capabilities,
apiService: &seleniumAPIService{},
}
return driver, nil
}
// SessionScriptTimeout creates an appropriate Timeout implementation for the
// script timeout.
func SessionScriptTimeout(to int) Timeout {
return timeout{
timeoutType: "script",
timeout: to,
}
}
// SessionPageLoadTimeout creates an appropriate Timeout implementation for the
// page load timeout.
func SessionPageLoadTimeout(to int) Timeout {
return timeout{
timeoutType: "page load",
timeout: to,
}
}
// SessionImplicitWaitTimeout creates an appropriate timeout implementation for the
// session implicit wait timeout.
func SessionImplicitWaitTimeout(to int) Timeout {
return timeout{
timeoutType: "implicit",
timeout: to,
}
}
// ByIndex accepts an integer that represents what the index of an element is
// and returns the appropriate By implementation.
func ByIndex(index uint) By {
return by{
t: "index",
value: index,
}
}
// ByCSSSelector accepts a CSS selector (i.e. ul#id > a) for use in the
// FindElement(s) functions.
func ByCSSSelector(selector string) By {
return by{
t: "css selector",
value: selector,
}
}
// ByLinkText is used to find an anchor element by its innerText.
func ByLinkText(text string) By {
return by{
t: "link text",
value: text,
}
}
// ByPartialLinkText works the same way as ByLinkText but performs a search
// where the link text contains the string passed in instead of a full match.
func ByPartialLinkText(text string) By {
return by{
t: "partial link text",
value: text,
}
}
// ByXPath utilises the xpath to find elements (see http://www.guru99.com/xpath-selenium.html).
func ByXPath(path string) By {
return by{
t: "xpath",
value: path,
}
}
type seleniumWebDriver struct {
seleniumURL string
sessionID string
capabilities *Capabilities
apiService apiServicer
}
func (s *seleniumWebDriver) DriverURL() string {
return s.seleniumURL
}
func (s *seleniumWebDriver) stateRequest(req *request) (*stateResponse, error) {
var response stateResponse
var err error
resp, err := s.apiService.performRequest(req.url, req.method, req.body)
if err != nil {
return nil, newCommunicationError(err, req.callingMethod, req.url, resp)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, req.callingMethod, string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) valueRequest(req *request) (*valueResponse, error) {
var response valueResponse
var err error
resp, err := s.apiService.performRequest(req.url, req.method, req.body)
if err != nil {
return nil, newCommunicationError(err, req.callingMethod, req.url, resp)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, req.callingMethod, string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) elementRequest(req *elRequest) ([]byte, error) {
b := map[string]interface{}{
"using": req.by.Type(),
"value": req.by.Value(),
}
bJSON, err := json.Marshal(b)
if err != nil {
return nil, newMarshallingError(err, req.callingMethod, bJSON)
}
body := bytes.NewReader(bJSON)
resp, err := s.apiService.performRequest(req.url, req.method, body)
if err != nil {
return nil, newCommunicationError(err, req.callingMethod, req.url, resp)
}
return resp, nil
}
func (s *seleniumWebDriver) scriptRequest(script string, url string, method string) (*ExecuteScriptResponse, error) {
r := map[string]interface{}{
"script": script,
"args": []string{""},
}
b, err := json.Marshal(r)
if err != nil {
return nil, newMarshallingError(err, method, r)
}
body := bytes.NewReader(b)
resp, err := s.valueRequest(&request{
url: url,
method: "POST",
body: body,
callingMethod: method,
})
if err != nil {
return nil, err
}
return &ExecuteScriptResponse{State: resp.State, Response: resp.Value}, nil
}
type timeout struct {
timeoutType string
timeout int
}
func (t timeout) Type() string {
return t.timeoutType
}
func (t timeout) Timeout() int {
return t.timeout
}
type request struct {
url string
method string
body io.Reader
callingMethod string
}
type elRequest struct {
url string
by By
method string
callingMethod string
}
type stateResponse struct {
State string `json:"state"`
}
type valueResponse struct {
State string `json:"state"`
Value string `json:"value"`
}
type by struct {
t string
value interface{}
}
func (b by) Type() string {
return b.t
}
func (b by) Value() interface{} {
return b.value
}