Skip to content

Commit 1c7d3cb

Browse files
author
heraclmene
committed
Add AlertText functionality and tests.
1 parent af429cb commit 1c7d3cb

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

remote_driver_alert.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ type AcceptAlertResponse struct {
1414
State string
1515
}
1616

17+
// AlertTextResponse is the response returned from calling the AlertText
18+
// method.
19+
type AlertTextResponse struct {
20+
State string
21+
Text string
22+
}
23+
1724
func (s *seleniumWebDriver) DismissAlert() (*DismissAlertResponse, error) {
1825
if len(s.sessionID) == 0 {
1926
return nil, newSessionIDError("DismissAlert")
@@ -57,3 +64,25 @@ func (s *seleniumWebDriver) AcceptAlert() (*AcceptAlertResponse, error) {
5764

5865
return &AcceptAlertResponse{State: resp.State}, nil
5966
}
67+
68+
func (s *seleniumWebDriver) AlertText() (*AlertTextResponse, error) {
69+
if len(s.sessionID) == 0 {
70+
return nil, newSessionIDError("AlertTextResponse")
71+
}
72+
73+
var err error
74+
75+
url := fmt.Sprintf("%s/session/%s/alert/text", s.seleniumURL, s.sessionID)
76+
77+
resp, err := s.valueRequest(&request{
78+
url: url,
79+
method: "GET",
80+
body: nil,
81+
callingMethod: "AlertText",
82+
})
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
return &AlertTextResponse{State: resp.State, Text: resp.Value}, nil
88+
}

remote_driver_alert_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,69 @@ func Test_AlertAcceptAlert_CorrectResponseIsReturned(t *testing.T) {
136136
t.Errorf(correctResponseErrorText)
137137
}
138138
}
139+
140+
/*
141+
AlertText() Tests
142+
*/
143+
144+
func Test_AlertAlertText_InvalidSessionIdResultsInError(t *testing.T) {
145+
api := &testableAPIService{
146+
jsonToReturn: "",
147+
errorToReturn: nil,
148+
}
149+
150+
d := setUpDriver(setUpDefaultCaps(), api)
151+
152+
_, err := d.AlertText()
153+
if err == nil || !IsSessionIDError(err) {
154+
t.Errorf(sessionIDErrorText)
155+
}
156+
}
157+
158+
func Test_AlertAlertText_CommunicationErrorIsReturnedCorrectly(t *testing.T) {
159+
api := &testableAPIService{
160+
jsonToReturn: "",
161+
errorToReturn: errors.New("An error :<"),
162+
}
163+
164+
d := setUpDriver(setUpDefaultCaps(), api)
165+
d.sessionID = "12345"
166+
167+
_, err := d.AlertText()
168+
if err == nil || !IsCommunicationError(err) {
169+
t.Errorf(apiCommunicationErrorText)
170+
}
171+
}
172+
173+
func Test_AlertAlertText_UnmarshallingErrorIsReturnedCorrectly(t *testing.T) {
174+
api := &testableAPIService{
175+
jsonToReturn: "Invalid JSON!",
176+
errorToReturn: nil,
177+
}
178+
179+
d := setUpDriver(setUpDefaultCaps(), api)
180+
d.sessionID = "12345"
181+
182+
_, err := d.AlertText()
183+
if err == nil || !IsUnmarshallingError(err) {
184+
t.Errorf(unmarshallingErrorText)
185+
}
186+
}
187+
188+
func Test_AlertAlertText_CorrectResponseIsReturned(t *testing.T) {
189+
api := &testableAPIService{
190+
jsonToReturn: `{
191+
"state": "success",
192+
"value": "this is text"
193+
}`,
194+
errorToReturn: nil,
195+
}
196+
197+
d := setUpDriver(setUpDefaultCaps(), api)
198+
d.sessionID = "12345"
199+
200+
resp, err := d.AlertText()
201+
if err != nil || resp.State != "success" || resp.Text != "this is text" {
202+
t.Errorf(correctResponseErrorText)
203+
}
204+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package integrationtests
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func Test_AlertAlertText_CanGetTheAlertText(t *testing.T) {
9+
setUp()
10+
defer tearDown()
11+
12+
driver := createDriver(t)
13+
_, err := driver.CreateSession()
14+
if err != nil {
15+
t.Errorf("Create session error")
16+
}
17+
18+
_, err = driver.Go("https://heraclmene.github.io/helpers/goselenium/alert.html")
19+
if err != nil {
20+
t.Errorf("Error visiting URL")
21+
}
22+
23+
time.Sleep(time.Second)
24+
25+
resp, err := driver.AlertText()
26+
if err != nil || resp.State != "success" || resp.Text != "this is an alert" {
27+
t.Errorf("Error returned or alert text was not correct")
28+
}
29+
30+
printObjectResult(resp)
31+
}

web_driver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ type WebDriver interface {
252252
// AcceptAlertResponse accepts an alert if there is one present. If not,
253253
// it will throw an error.
254254
AcceptAlert() (*AcceptAlertResponse, error)
255+
256+
// AlertText gets the text associated with the current alert. If there is
257+
// not an alert, it will throw an error.
258+
AlertText() (*AlertTextResponse, error)
255259
}
256260

257261
// Element is an interface which specifies what all WebDriver elements

0 commit comments

Comments
 (0)