Skip to content

Commit 84c2f46

Browse files
author
heraclmene
committed
Add ExecuteScript() functionality and tests
1 parent 54ce5d1 commit 84c2f46

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

remote_driver.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,29 @@ func (s *seleniumWebDriver) elementRequest(req *elRequest) ([]byte, error) {
176176
return resp, nil
177177
}
178178

179+
func (s *seleniumWebDriver) scriptRequest(script string, url string, method string) (*ExecuteScriptResponse, error) {
180+
r := map[string]interface{}{
181+
"script": script,
182+
"args": []string{""},
183+
}
184+
b, err := json.Marshal(r)
185+
if err != nil {
186+
return nil, newMarshallingError(err, method, r)
187+
}
188+
body := bytes.NewReader(b)
189+
resp, err := s.valueRequest(&request{
190+
url: url,
191+
method: "POST",
192+
body: body,
193+
callingMethod: method,
194+
})
195+
if err != nil {
196+
return nil, err
197+
}
198+
199+
return &ExecuteScriptResponse{State: resp.State, Response: resp.Value}, nil
200+
}
201+
179202
type timeout struct {
180203
timeoutType string
181204
timeout int

remote_driver_document.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ type PageSourceResponse struct {
99
Source string
1010
}
1111

12+
// ExecuteScriptResponse is the response returned from calling the ExecuteScript
13+
// method.
14+
type ExecuteScriptResponse struct {
15+
State string
16+
Response string
17+
}
18+
1219
func (s *seleniumWebDriver) PageSource() (*PageSourceResponse, error) {
1320
if len(s.sessionID) == 0 {
1421
return nil, newSessionIDError("PageSource")
@@ -30,3 +37,13 @@ func (s *seleniumWebDriver) PageSource() (*PageSourceResponse, error) {
3037

3138
return &PageSourceResponse{State: resp.State, Source: resp.Value}, nil
3239
}
40+
41+
func (s *seleniumWebDriver) ExecuteScript(script string) (*ExecuteScriptResponse, error) {
42+
if len(s.sessionID) == 0 {
43+
return nil, newSessionIDError("ExecuteScript")
44+
}
45+
46+
url := fmt.Sprintf("%s/session/%s/execute", s.seleniumURL, s.sessionID)
47+
48+
return s.scriptRequest(script, url, "ExecuteScript")
49+
}

remote_driver_document_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,68 @@ func Test_DocumentPageSource_ResultIsReturnedSuccessfully(t *testing.T) {
6969
t.Errorf(correctResponseErrorText)
7070
}
7171
}
72+
73+
/*
74+
ExecuteScript tests
75+
*/
76+
func Test_CommandExecuteScript_InvalidSessionIDResultsInError(t *testing.T) {
77+
api := &testableAPIService{
78+
jsonToReturn: "",
79+
errorToReturn: nil,
80+
}
81+
82+
d := setUpDriver(setUpDefaultCaps(), api)
83+
84+
_, err := d.ExecuteScript("alert('test');")
85+
if err == nil || !IsSessionIDError(err) {
86+
t.Errorf(sessionIDErrorText)
87+
}
88+
}
89+
90+
func Test_CommandExecuteScript_CommunicationErrorIsReturned(t *testing.T) {
91+
api := &testableAPIService{
92+
jsonToReturn: "",
93+
errorToReturn: errors.New("An error"),
94+
}
95+
96+
d := setUpDriver(setUpDefaultCaps(), api)
97+
d.sessionID = "12345"
98+
99+
_, err := d.ExecuteScript("alert('test');")
100+
if err == nil || !IsCommunicationError(err) {
101+
t.Errorf(apiCommunicationErrorText)
102+
}
103+
}
104+
105+
func Test_CommandExecuteScript_UnmarshallingErrorIsReturned(t *testing.T) {
106+
api := &testableAPIService{
107+
jsonToReturn: "Invalid JSON",
108+
errorToReturn: nil,
109+
}
110+
111+
d := setUpDriver(setUpDefaultCaps(), api)
112+
d.sessionID = "12345"
113+
114+
_, err := d.ExecuteScript("alert('test');")
115+
if err == nil || !IsUnmarshallingError(err) {
116+
t.Errorf(unmarshallingErrorText)
117+
}
118+
}
119+
120+
func Test_CommandExecuteScript_ResultIsReturnedSuccessfully(t *testing.T) {
121+
api := &testableAPIService{
122+
jsonToReturn: `{
123+
"state": "success",
124+
"value": "test"
125+
}`,
126+
errorToReturn: nil,
127+
}
128+
129+
d := setUpDriver(setUpDefaultCaps(), api)
130+
d.sessionID = "12345"
131+
132+
resp, err := d.ExecuteScript("alert('test');")
133+
if err != nil || resp.State != "success" {
134+
t.Errorf(correctResponseErrorText)
135+
}
136+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package integrationtests
2+
3+
import "testing"
4+
5+
func Test_DocumentExecuteScript_CanExecuteScriptsSuccessfully(t *testing.T) {
6+
setUp()
7+
defer tearDown()
8+
9+
driver := createDriver(t)
10+
_, err := driver.CreateSession()
11+
if err != nil {
12+
errorAndWrap(t, "Error thrown whilst creating session.", err)
13+
}
14+
15+
_, err = driver.Go("https://news.ycombinator.com")
16+
if err != nil {
17+
errorAndWrap(t, "Error was thrown whilst navigating or result was not a success.", err)
18+
}
19+
20+
resp, err := driver.ExecuteScript("return \"Test\";")
21+
if err != nil || resp.Response != "Test" {
22+
errorAndWrap(t, "Error was thrown whilst executing script or response was not correct", err)
23+
}
24+
25+
printObjectResult(resp)
26+
}

web_driver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ type WebDriver interface {
160160

161161
// PageSource retrieves the outerHTML value of the current URL.
162162
PageSource() (*PageSourceResponse, error)
163+
164+
// ExecuteScript executes a Javascript script on the currently active
165+
// page.
166+
ExecuteScript(script string) (*ExecuteScriptResponse, error)
163167
}
164168

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

0 commit comments

Comments
 (0)