Skip to content

Commit

Permalink
Add SendAlertText functionality and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heraclmene committed Dec 27, 2016
1 parent 1c7d3cb commit df88659
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 16 deletions.
43 changes: 42 additions & 1 deletion remote_driver_alert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package goselenium

import "fmt"
import (
"bytes"
"encoding/json"
"fmt"
)

// DismissAlertResponse is the response returned from calling the DismissAlert
// method.
Expand All @@ -21,6 +25,12 @@ type AlertTextResponse struct {
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")
Expand Down Expand Up @@ -86,3 +96,34 @@ func (s *seleniumWebDriver) AlertText() (*AlertTextResponse, error) {

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
}
64 changes: 64 additions & 0 deletions remote_driver_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,67 @@ func Test_AlertAlertText_CorrectResponseIsReturned(t *testing.T) {
t.Errorf(correctResponseErrorText)
}
}

/*
SendAlertText() Tests
*/
func Test_AlertSendAlertText_InvalidSessionIdResultsInError(t *testing.T) {
api := &testableAPIService{
jsonToReturn: "",
errorToReturn: nil,
}

d := setUpDriver(setUpDefaultCaps(), api)

_, err := d.SendAlertText("test")
if err == nil || !IsSessionIDError(err) {
t.Errorf(sessionIDErrorText)
}
}

func Test_AlertSendAlertText_CommunicationErrorIsReturnedCorrectly(t *testing.T) {
api := &testableAPIService{
jsonToReturn: "",
errorToReturn: errors.New("An error :<"),
}

d := setUpDriver(setUpDefaultCaps(), api)
d.sessionID = "12345"

_, err := d.SendAlertText("test")
if err == nil || !IsCommunicationError(err) {
t.Errorf(apiCommunicationErrorText)
}
}

func Test_AlertSendAlertText_UnmarshallingErrorIsReturnedCorrectly(t *testing.T) {
api := &testableAPIService{
jsonToReturn: "Invalid JSON!",
errorToReturn: nil,
}

d := setUpDriver(setUpDefaultCaps(), api)
d.sessionID = "12345"

_, err := d.SendAlertText("test")
if err == nil || !IsUnmarshallingError(err) {
t.Errorf(unmarshallingErrorText)
}
}

func Test_AlertSendAlertText_CorrectResponseIsReturned(t *testing.T) {
api := &testableAPIService{
jsonToReturn: `{
"state": "success"
}`,
errorToReturn: nil,
}

d := setUpDriver(setUpDefaultCaps(), api)
d.sessionID = "12345"

resp, err := d.SendAlertText("test")
if err != nil || resp.State != "success" {
t.Errorf(correctResponseErrorText)
}
}
7 changes: 1 addition & 6 deletions test/integration_tests/alert_acceptalert_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package integrationtests

import (
"testing"
"time"
)
import "testing"

func Test_AlertAcceptAlert_CanAcceptAnAlertCorrectly(t *testing.T) {
setUp()
Expand All @@ -20,8 +17,6 @@ func Test_AlertAcceptAlert_CanAcceptAnAlertCorrectly(t *testing.T) {
t.Errorf("Error visiting URL")
}

time.Sleep(time.Second)

resp, err := driver.AcceptAlert()
if err != nil || resp.State != "success" {
t.Errorf("Error returned or accepting an alert was not a success")
Expand Down
7 changes: 1 addition & 6 deletions test/integration_tests/alert_alerttext_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package integrationtests

import (
"testing"
"time"
)
import "testing"

func Test_AlertAlertText_CanGetTheAlertText(t *testing.T) {
setUp()
Expand All @@ -20,8 +17,6 @@ func Test_AlertAlertText_CanGetTheAlertText(t *testing.T) {
t.Errorf("Error visiting URL")
}

time.Sleep(time.Second)

resp, err := driver.AlertText()
if err != nil || resp.State != "success" || resp.Text != "this is an alert" {
t.Errorf("Error returned or alert text was not correct")
Expand Down
3 changes: 0 additions & 3 deletions test/integration_tests/alert_dismissalert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package integrationtests

import (
"testing"
"time"

goselenium "github.com/bunsenapp/go-selenium"
)
Expand All @@ -22,8 +21,6 @@ func Test_AlertDismissAlert_CanDismissAnAlertCorrectly(t *testing.T) {
t.Errorf("Error visiting URL")
}

time.Sleep(time.Second)

resp, err := driver.DismissAlert()
if err != nil || resp.State != "success" {
t.Errorf("Error returned or dismissing an alert was not a success")
Expand Down
45 changes: 45 additions & 0 deletions test/integration_tests/alert_sendalerttext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package integrationtests

import (
"testing"

goselenium "github.com/bunsenapp/go-selenium"
)

func Test_AlertSendAlertText_CanSendAlertTextCorrectly(t *testing.T) {
setUp()
defer tearDown()

driver := createDriver(t)
_, err := driver.CreateSession()
if err != nil {
t.Errorf("Create session error")
}

_, err = driver.Go("https://heraclmene.github.io/helpers/goselenium/prompt.html")
if err != nil {
t.Errorf("Error visiting URL")
}

resp, err := driver.SendAlertText("test")
if err != nil || resp.State != "success" {
t.Errorf("Error returned or sending alert text was not a success")
}

_, err = driver.AcceptAlert()
if err != nil {
t.Errorf("Error was returned when accepting alert.")
}

_, err = driver.AlertText()
if err != nil {
comErr := err.(goselenium.CommunicationError)
if comErr.Response.State != goselenium.NoSuchAlert {
t.Errorf("Error returned was not correct.")
}
} else {
t.Errorf("Error returned was not correct.")
}

printObjectResult(resp)
}
9 changes: 9 additions & 0 deletions web_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ type WebDriver interface {
// AlertText gets the text associated with the current alert. If there is
// not an alert, it will throw an error.
AlertText() (*AlertTextResponse, error)

// SendAlertText enters the text specified into the value box.
//
// If the prompt is of type 'alert' or 'confirm', a communication error
// with code 'element not interactable' will be returned.
//
// If the prompt is anything other than 'prompt', a communication error with
// code 'unsupported operation' will be returned.
SendAlertText(text string) (*SendAlertTextResponse, error)
}

// Element is an interface which specifies what all WebDriver elements
Expand Down

0 comments on commit df88659

Please sign in to comment.