forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_driver_session.go
182 lines (146 loc) · 4.53 KB
/
remote_driver_session.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
package goselenium
import (
"bytes"
"encoding/json"
"fmt"
)
// CreateSessionResponse is the response returned from the API when the
// CreateSession() method does not throw an error.
type CreateSessionResponse struct {
Capabilities CreateSessionCapabilities `json:"value"`
SessionID string `json:"sessionId"`
}
// CreateSessionCapabilities is a summarisation of the capabilities returned
// from the CreateSession method.
type CreateSessionCapabilities struct {
AcceptInsecureCerts bool `json:"acceptSslCerts"`
BrowserName string `json:"browserName"`
BrowserVersion string `json:"browserVersion"`
PlatformName string `json:"platformVersion"`
}
// DeleteSessionResponse is the response returned from the API when the
// DeleteSession() method does not thrown an error.
type DeleteSessionResponse struct {
State string `json:"state"`
SessionID string `json:"sessionId"`
}
// SessionStatusResponse is the response returned from the API when the
// SessionStatus() method is called.
type SessionStatusResponse struct {
State string
}
// SetSessionTimeoutResponse is the response returned from the API when the
// SetSessionTimeoutResponse() method is called.
type SetSessionTimeoutResponse struct {
State string
}
type LogEntry struct {
Timestamp float64 `json:"timestamp"`
Level string `json:"level"`
Message string `json:"message"`
}
type LogResponse struct {
Entries []*LogEntry
}
func (s *seleniumWebDriver) CreateSession() (*CreateSessionResponse, error) {
var response CreateSessionResponse
var err error
url := fmt.Sprintf("%s/session", s.seleniumURL)
capabilitiesJSON, err := s.capabilities.toJSON()
if err != nil {
return nil, newMarshallingError(err, "CreateSession", s.capabilities)
}
body := bytes.NewReader([]byte(capabilitiesJSON))
resp, err := s.apiService.performRequest(url, "POST", body)
if err != nil {
return nil, newCommunicationError(err, "CreateSession", url, resp)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "CreateSession", string(resp))
}
s.sessionID = response.SessionID
return &response, nil
}
func (s *seleniumWebDriver) DeleteSession() (*DeleteSessionResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("DeleteSession")
}
var response DeleteSessionResponse
var err error
url := fmt.Sprintf("%s/session/%s", s.seleniumURL, s.sessionID)
resp, err := s.apiService.performRequest(url, "DELETE", nil)
if err != nil {
return nil, newCommunicationError(err, "DeleteSession", url, resp)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "DeleteSession", string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) SessionStatus() (*SessionStatusResponse, error) {
var err error
url := fmt.Sprintf("%s/status", s.seleniumURL)
resp, err := s.stateRequest(&request{
url: url,
method: "GET",
body: nil,
callingMethod: "SessionStatus",
})
if err != nil {
return nil, err
}
return &SessionStatusResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) SetSessionTimeout(to Timeout) (*SetSessionTimeoutResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("SetSessionTimeout")
}
var err error
url := fmt.Sprintf("%s/session/%s/timeouts", s.seleniumURL, s.sessionID)
params := map[string]interface{}{
"type": to.Type(),
"ms": to.Timeout(),
}
marshalledJSON, err := json.Marshal(params)
if err != nil {
return nil, newMarshallingError(err, "SetSessionTimeout", params)
}
bodyReader := bytes.NewReader([]byte(marshalledJSON))
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: bodyReader,
callingMethod: "SetSessionTimeout",
})
if err != nil {
return nil, err
}
return &SetSessionTimeoutResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) Log(logType string) (*LogResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Log")
}
var err error
url := fmt.Sprintf("%s/session/%s/log", s.seleniumURL, s.sessionID)
params := map[string]interface{}{
"type": logType,
}
marshalledJSON, err := json.Marshal(params)
if err != nil {
return nil, newMarshallingError(err, "Log", params)
}
bodyReader := bytes.NewReader([]byte(marshalledJSON))
resp, err := s.logRequest(&request{
url: url,
method: "POST",
body: bodyReader,
callingMethod: "Log",
})
if err != nil {
return nil, err
}
return &LogResponse{Entries: resp.Value}, nil
}