-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathremote_driver_session.go
More file actions
141 lines (113 loc) · 3.68 KB
/
remote_driver_session.go
File metadata and controls
141 lines (113 loc) · 3.68 KB
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
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
}
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
}