forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_driver_alert.go
129 lines (103 loc) · 2.74 KB
/
remote_driver_alert.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
package goselenium
import (
"bytes"
"encoding/json"
"fmt"
)
// DismissAlertResponse is the response returned from calling the DismissAlert
// method.
type DismissAlertResponse struct {
State string
}
// AcceptAlertResponse is the response returned from calling the AcceptAlert
// method.
type AcceptAlertResponse struct {
State string
}
// AlertTextResponse is the response returned from calling the AlertText
// method.
type AlertTextResponse struct {
State string
Text string
}
// SendAlertTextResponse is the response returned from calling the
// SendAlertText method.
type SendAlertTextResponse struct {
State string
}
func (s *seleniumWebDriver) DismissAlert() (*DismissAlertResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("DismissAlert")
}
var err error
url := fmt.Sprintf("%s/session/%s/alert/dismiss", s.seleniumURL, s.sessionID)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: nil,
callingMethod: "DismissAlert",
})
if err != nil {
return nil, err
}
return &DismissAlertResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) AcceptAlert() (*AcceptAlertResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("AcceptAlert")
}
var err error
url := fmt.Sprintf("%s/session/%s/alert/accept", s.seleniumURL, s.sessionID)
resp, err := s.stateRequest(&request{
url: url,
method: "POST",
body: nil,
callingMethod: "AcceptAlert",
})
if err != nil {
return nil, err
}
return &AcceptAlertResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) AlertText() (*AlertTextResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("AlertTextResponse")
}
var err error
url := fmt.Sprintf("%s/session/%s/alert/text", s.seleniumURL, s.sessionID)
resp, err := s.valueRequest(&request{
url: url,
method: "GET",
body: nil,
callingMethod: "AlertText",
})
if err != nil {
return nil, err
}
return &AlertTextResponse{State: resp.State, Text: resp.Value}, nil
}
func (s *seleniumWebDriver) SendAlertText(text string) (*SendAlertTextResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("SendAlertText")
}
var err error
url := fmt.Sprintf("%s/session/%s/alert/text", s.seleniumURL, s.sessionID)
b := map[string]string{
"text": text,
}
json, err := json.Marshal(b)
if err != nil {
return nil, newMarshallingError(err, "SendAlertText", b)
}
body := bytes.NewReader(json)
resp, err := s.valueRequest(&request{
url: url,
method: "POST",
body: body,
callingMethod: "SendAlertText",
})
if err != nil {
return nil, err
}
return &SendAlertTextResponse{State: resp.State}, nil
}