forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_driver_navigation.go
213 lines (172 loc) · 4.58 KB
/
remote_driver_navigation.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
package goselenium
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
// GoResponse is the response returned from the selenium web driver when calling
// the Go() call. Unfortunately, the W3C specification defines that the response
// should only be whether the call succeeded or not. Should there be any redirects
// they will not be catered for in this response. Should you expect any redirects
// to happen, call the CurrentURL() method.
type GoResponse struct {
State string
}
// CurrentURLResponse is the response returned from the GET Url call.
type CurrentURLResponse struct {
State string
URL string
}
// BackResponse is the response returned from the Back call.
type BackResponse struct {
State string
}
// ForwardResponse is the response returned from the Forward call.
type ForwardResponse struct {
State string
}
// RefreshResponse is the response returned from the Refresh call.
type RefreshResponse struct {
State string
}
// TitleResponse is the response returned from the Title call.
type TitleResponse struct {
State string
Title string
}
func (s *seleniumWebDriver) Go(goURL string) (*GoResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Go")
}
var err error
url := fmt.Sprintf("%s/session/%s/url", s.seleniumURL, s.sessionID)
invalidURL := goURL == ""
validProtocol := strings.HasPrefix(goURL, "https://") || strings.HasPrefix(goURL, "http://")
if invalidURL || !validProtocol {
return nil, newInvalidURLError(goURL)
}
params := map[string]string{
"url": goURL,
}
marshalledJSON, err := json.Marshal(params)
if err != nil {
return nil, newMarshallingError(err, "Go", params)
}
bodyReader := bytes.NewReader([]byte(marshalledJSON))
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: bodyReader,
callingMethod: "Go",
})
if err != nil {
return nil, err
}
return &GoResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) CurrentURL() (*CurrentURLResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("CurrentURL")
}
var response CurrentURLResponse
var err error
url := fmt.Sprintf("%s/session/%s/url", s.seleniumURL, s.sessionID)
resp, err := s.valueRequest(&request{
url: url,
method: "GET",
body: nil,
callingMethod: "CurrentURL",
})
if err != nil {
return nil, err
}
var value string
err = json.Unmarshal(resp.Value, &value)
if err != nil {
return nil, newUnmarshallingError(err, "CurrentURL", string(resp.Value))
}
response = CurrentURLResponse{
State: resp.State,
URL: value,
}
return &response, nil
}
func (s *seleniumWebDriver) Back() (*BackResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Back")
}
var err error
url := fmt.Sprintf("%s/session/%s/back", s.seleniumURL, s.sessionID)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: nil,
callingMethod: "Back",
})
if err != nil {
return nil, err
}
return &BackResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) Forward() (*ForwardResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Forward")
}
var err error
url := fmt.Sprintf("%s/session/%s/forward", s.seleniumURL, s.sessionID)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: nil,
callingMethod: "Forward",
})
if err != nil {
return nil, err
}
return &ForwardResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) Refresh() (*RefreshResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Refresh")
}
var err error
url := fmt.Sprintf("%s/session/%s/refresh", s.seleniumURL, s.sessionID)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: nil,
callingMethod: "Refresh",
})
if err != nil {
return nil, err
}
return &RefreshResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) Title() (*TitleResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Title")
}
var response TitleResponse
var err error
url := fmt.Sprintf("%s/session/%s/title", s.seleniumURL, s.sessionID)
resp, err := s.valueRequest(&request{
url: url,
method: "GET",
body: nil,
callingMethod: "Title",
})
if err != nil {
return nil, err
}
var value string
err = json.Unmarshal(resp.Value, &value)
if err != nil {
return nil, newUnmarshallingError(err, "Title", string(resp.Value))
}
response = TitleResponse{
State: resp.State,
Title: value,
}
return &response, nil
}