Skip to content

Commit 3ac422d

Browse files
author
heraclmene
committed
Add ExecuteScriptAsync functionality and tests.
1 parent 84c2f46 commit 3ac422d

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

remote_driver_document.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,13 @@ func (s *seleniumWebDriver) ExecuteScript(script string) (*ExecuteScriptResponse
4747

4848
return s.scriptRequest(script, url, "ExecuteScript")
4949
}
50+
51+
func (s *seleniumWebDriver) ExecuteScriptAsync(script string) (*ExecuteScriptResponse, error) {
52+
if len(s.sessionID) == 0 {
53+
return nil, newSessionIDError("ExecuteScriptAsync")
54+
}
55+
56+
url := fmt.Sprintf("%s/session/%s/execute_async", s.seleniumURL, s.sessionID)
57+
58+
return s.scriptRequest(script, url, "ExecuteScriptAsync")
59+
}

remote_driver_document_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,68 @@ func Test_CommandExecuteScript_ResultIsReturnedSuccessfully(t *testing.T) {
134134
t.Errorf(correctResponseErrorText)
135135
}
136136
}
137+
138+
/*
139+
ExecuteScriptAsync tests
140+
*/
141+
func Test_CommandExecuteScriptAsync_InvalidSessionIDResultsInError(t *testing.T) {
142+
api := &testableAPIService{
143+
jsonToReturn: "",
144+
errorToReturn: nil,
145+
}
146+
147+
d := setUpDriver(setUpDefaultCaps(), api)
148+
149+
_, err := d.ExecuteScriptAsync("alert('test');")
150+
if err == nil || !IsSessionIDError(err) {
151+
t.Errorf(sessionIDErrorText)
152+
}
153+
}
154+
155+
func Test_CommandExecuteScriptAsync_CommunicationErrorIsReturned(t *testing.T) {
156+
api := &testableAPIService{
157+
jsonToReturn: "",
158+
errorToReturn: errors.New("An error"),
159+
}
160+
161+
d := setUpDriver(setUpDefaultCaps(), api)
162+
d.sessionID = "12345"
163+
164+
_, err := d.ExecuteScriptAsync("alert('test');")
165+
if err == nil || !IsCommunicationError(err) {
166+
t.Errorf(apiCommunicationErrorText)
167+
}
168+
}
169+
170+
func Test_CommandExecuteScriptAsync_UnmarshallingErrorIsReturned(t *testing.T) {
171+
api := &testableAPIService{
172+
jsonToReturn: "Invalid JSON",
173+
errorToReturn: nil,
174+
}
175+
176+
d := setUpDriver(setUpDefaultCaps(), api)
177+
d.sessionID = "12345"
178+
179+
_, err := d.ExecuteScriptAsync("alert('test');")
180+
if err == nil || !IsUnmarshallingError(err) {
181+
t.Errorf(unmarshallingErrorText)
182+
}
183+
}
184+
185+
func Test_CommandExecuteScriptAsync_ResultIsReturnedSuccessfully(t *testing.T) {
186+
api := &testableAPIService{
187+
jsonToReturn: `{
188+
"state": "success",
189+
"value": "test"
190+
}`,
191+
errorToReturn: nil,
192+
}
193+
194+
d := setUpDriver(setUpDefaultCaps(), api)
195+
d.sessionID = "12345"
196+
197+
resp, err := d.ExecuteScriptAsync("alert('test');")
198+
if err != nil || resp.State != "success" {
199+
t.Errorf(correctResponseErrorText)
200+
}
201+
}

test/integration_tests/document_executescript_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,26 @@ func Test_DocumentExecuteScript_CanExecuteScriptsSuccessfully(t *testing.T) {
2424

2525
printObjectResult(resp)
2626
}
27+
28+
func Test_DocumentExecuteScriptAsync_CanExecuteScriptsSuccessfully(t *testing.T) {
29+
setUp()
30+
defer tearDown()
31+
32+
driver := createDriver(t)
33+
_, err := driver.CreateSession()
34+
if err != nil {
35+
errorAndWrap(t, "Error thrown whilst creating session.", err)
36+
}
37+
38+
_, err = driver.Go("https://news.ycombinator.com")
39+
if err != nil {
40+
errorAndWrap(t, "Error was thrown whilst navigating or result was not a success.", err)
41+
}
42+
43+
resp, err := driver.ExecuteScriptAsync("var callback = arguments[arguments.length - 1]; callback('Test');")
44+
if err != nil || resp.Response != "Test" {
45+
errorAndWrap(t, "Error was thrown whilst executing script or response was not correct", err)
46+
}
47+
48+
printObjectResult(resp)
49+
}

web_driver.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ type WebDriver interface {
164164
// ExecuteScript executes a Javascript script on the currently active
165165
// page.
166166
ExecuteScript(script string) (*ExecuteScriptResponse, error)
167+
168+
// ExecuteScriptAsync executes a Javascript script asynchronously on the
169+
// currently active page. If you do not have experience with this call,
170+
// there is an example below.
171+
//
172+
// The async handler runs on the concept of a callback; meaning it will run
173+
// your code asynchronously and if it completes, will call the callback.
174+
//
175+
// Selenium helpfully provides a callback function which is passed in
176+
// to the 'arguments' array that you can access within your script. The
177+
// callback function is always the LAST element of the array. You can
178+
// access it like the below:
179+
// var callback = arguments[arguments.length - 1];
180+
// The callback function also accepts one argument as a parameter, this
181+
// can be anything and will be assigned to the Response property of
182+
// ExecuteScriptResponse.
183+
//
184+
// An example:
185+
// var callback = arguments[arguments.length - 1];
186+
// doLongWindedTask();
187+
// callback();
188+
ExecuteScriptAsync(script string) (*ExecuteScriptResponse, error)
167189
}
168190

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

0 commit comments

Comments
 (0)