forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_driver_command.go
328 lines (264 loc) · 8.35 KB
/
remote_driver_command.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
package goselenium
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)
// WindowHandleResponse is the response returned from the WindowHandle() method.
// The handle is the current active window. Should you switch windows,
// any value returned prior to that call will be invalid.
type WindowHandleResponse struct {
State string
Handle string
}
// CloseWindowResponse is the response returned from the CloseWindow() method.
// As per the W3C specification, it yields all of the available window handles
// minus the active one that closes as a result of the CloseWindow() call.
type CloseWindowResponse struct {
State string `json:"state"`
Handles []string `json:"value"`
}
// SwitchToWindowResponse is the response returned from the SwitchToWindow()
// method. You can verify that this result is correct by calling the
// WindowHandle() method. The two should match.
type SwitchToWindowResponse struct {
State string `json:"state"`
}
// WindowHandlesResponse is the response returned from the WindowHandles()
// method. This is essentially an array of available window handlers that
// aren't necessarily active.
type WindowHandlesResponse struct {
State string `json:"state"`
Handles []string `json:"value"`
}
// SwitchToFrameResponse is the response returned from the SwitchToFrame()
// method. For now, according to the specification, it only returns a state.
type SwitchToFrameResponse struct {
State string
}
// SwitchToParentFrameResponse represents the response from attempting to
// switch the top level browsing context to the parent of the current top level
// browsing context.
type SwitchToParentFrameResponse struct {
State string
}
// WindowSizeResponse is the response returned from calling the WindowSize
// method. The definitions are in CSS pixels.
type WindowSizeResponse struct {
State string `json:"state"`
Dimensions Dimensions `json:"value"`
}
// Dimensions is a type that is both returned and accept by functions. It is
// usually only used for the window size components.
type Dimensions struct {
Width uint `json:"width"`
Height uint `json:"height"`
}
// SetWindowSizeResponse is the response that is returned from setting the
// window size of the current top level browsing context.
type SetWindowSizeResponse struct {
State string
}
// MaximizeWindowResponse is the response that is returned from increasing the
// browser to match the viewport.
type MaximizeWindowResponse struct {
State string
}
func (s *seleniumWebDriver) WindowHandle() (*WindowHandleResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("WindowHandle")
}
var response WindowHandleResponse
var err error
url := fmt.Sprintf("%s/session/%s/window", s.seleniumURL, s.sessionID)
resp, err := s.valueRequest(&request{
url: url,
method: "GET",
body: nil,
callingMethod: "WindowHandle",
})
if err != nil {
return nil, err
}
var value string
err = json.Unmarshal(resp.Value, &value)
if err != nil {
return nil, newUnmarshallingError(err, "WindowHandle", string(resp.Value))
}
response = WindowHandleResponse{
State: resp.State,
Handle: value,
}
return &response, nil
}
func (s *seleniumWebDriver) CloseWindow() (*CloseWindowResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("CloseWindow")
}
var response CloseWindowResponse
var err error
url := fmt.Sprintf("%s/session/%s/window", s.seleniumURL, s.sessionID)
resp, err := s.apiService.performRequest(url, "DELETE", nil)
if err != nil {
return nil, newCommunicationError(err, "CloseWindow", url, resp)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "CloseWindow", string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) SwitchToWindow(handle string) (*SwitchToWindowResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("SwitchToWindow")
}
var err error
url := fmt.Sprintf("%s/session/%s/window", s.seleniumURL, s.sessionID)
params := map[string]interface{}{
// chromedriver versions before late august 2017
// look for "name", not "handle", so let's specify
// both
"name": handle,
"handle": handle,
}
requestJSON, err := json.Marshal(params)
if err != nil {
return nil, newMarshallingError(err, "SwitchToWindow", params)
}
body := bytes.NewReader(requestJSON)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: body,
callingMethod: "SwitchToWindow",
})
if err != nil {
return nil, err
}
return &SwitchToWindowResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) WindowHandles() (*WindowHandlesResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("WindowHandles")
}
var response WindowHandlesResponse
var err error
url := fmt.Sprintf("%s/session/%s/window/handles", s.seleniumURL, s.sessionID)
resp, err := s.apiService.performRequest(url, "GET", nil)
if err != nil {
return nil, newCommunicationError(err, "WindowHandles", url, resp)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "WindowHandles", string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) SwitchToFrame(by By) (*SwitchToFrameResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("SwitchToFrame")
}
if by == nil || (by.Type() != "index") {
return nil, errors.New("switchtoframe: invalid by argument")
}
var err error
url := fmt.Sprintf("%s/session/%s/frame", s.seleniumURL, s.sessionID)
params := map[string]interface{}{
"id": by.Value(),
}
requestJSON, err := json.Marshal(params)
if err != nil {
return nil, newMarshallingError(err, "SwitchToFrame", params)
}
body := bytes.NewReader(requestJSON)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: body,
callingMethod: "SwitchToFrame",
})
if err != nil {
return nil, err
}
return &SwitchToFrameResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) SwitchToParentFrame() (*SwitchToParentFrameResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("SwitchToParentFrame")
}
var err error
url := fmt.Sprintf("%s/session/%s/frame/parent", s.seleniumURL, s.sessionID)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: nil,
callingMethod: "SwitchToParentFrame",
})
if err != nil {
return nil, err
}
return &SwitchToParentFrameResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) WindowSize() (*WindowSizeResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("WindowSize")
}
var response WindowSizeResponse
var err error
url := fmt.Sprintf("%s/session/%s/window/size", s.seleniumURL, s.sessionID)
resp, err := s.apiService.performRequest(url, "GET", nil)
if err != nil {
return nil, newCommunicationError(err, "WindowSize", url, nil)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "WindowSize", string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) SetWindowSize(dimension *Dimensions) (*SetWindowSizeResponse, error) {
if dimension == nil {
return nil, errors.New("setwindowsize: invalid dimension argument")
} else if len(s.sessionID) == 0 {
return nil, newSessionIDError("SetWindowSize")
}
var err error
url := fmt.Sprintf("%s/session/%s/window/size", s.seleniumURL, s.sessionID)
body := map[string]uint{
"width": dimension.Width,
"height": dimension.Height,
}
json, err := json.Marshal(body)
if err != nil {
return nil, newMarshallingError(err, "SetWindowSize", body)
}
jsonBytes := bytes.NewReader(json)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: jsonBytes,
callingMethod: "SetWindowSize",
})
if err != nil {
return nil, err
}
return &SetWindowSizeResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) MaximizeWindow() (*MaximizeWindowResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("MaximizeWindow")
}
var err error
url := fmt.Sprintf("%s/session/%s/window/maximize", s.seleniumURL, s.sessionID)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: nil,
callingMethod: "MaximizeWindow",
})
if err != nil {
return nil, err
}
return &MaximizeWindowResponse{State: resp.State}, nil
}