Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit a1a84fb

Browse files
committed
Merge pull request #40 from w0rse/master
Implement execute_async, async_script, timeouts methods
2 parents 00c1e42 + 111cb0e commit a1a84fb

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ Method | Description
6060
`driver.refresh()` | Refreshes the browser.
6161
`driver.getElement(cssSelector)` | Finds an element on the page using the `cssSelector` and returns an Element.
6262
`driver.getElements(cssSelector)` | Finds all elements on the page using the `cssSelector` and returns an array of Elements.
63+
`driver.setTimeouts(type, milliseconds)` | Sets a timeout for a certain type of operation. Valid types are: "script" for script timeouts, "implicit" for modifying the implicit wait timeout and "page load" for setting a page load timeout.
6364
`driver.setElementTimeout(milliseconds)` | Sets a timeout for WebDriver to find elements with `getElement` and `getElements`.
65+
`driver.setScriptTimeout(milliseconds)` | Sets a timeout for WebDriver to execute async scripts with `evaluateAsync`.
6466
`driver.getUrl()` | Returns the current url of the page.
6567
`driver.getPageTitle()` | Returns the current page title.
6668
`driver.getPageSource()` | Returns the current page's html source.
6769
`driver.getScreenshot()` | Returns screenshot as a base64 encoded PNG.
6870
`driver.evaluate(javascriptString)` | Executes the given javascript. It must contain a return statement in order to get a value back.
71+
`driver.evaluateAsync(javascriptString)` | Executes the given asynchronous javascript. The executed script must signal that is done by invoking the provided callback, which is always provided as the final argument to the function.
6972
`driver.setCookie(Cookie)` | Sets a cookie on the current page's domain. `Cookie = { name, value, path='/' }`
7073
`driver.getCookies()` | Returns all cookies visible to the current page.
7174
`driver.clearCookies()` | Deletes all cookies visible to the current page.
@@ -110,8 +113,8 @@ Status | HTTP Method | Path | Summary
110113
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | GET | `/sessions` | Returns a list of the currently active sessions.
111114
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | GET | `/session/:sessionId` | Retrieve the capabilities of the specified session.
112115
![Implemented](./docs/implemented.png "Implemented") | DELETE | `/session/:sessionId` | Delete the session.
113-
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | POST | `/session/:sessionId/timeouts` | Configure the amount of time that a particular type of operation can execute for before they are aborted and a `Timeout` error is returned to the client.
114-
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | POST | `/session/:sessionId/timeouts/async_script` | Set the amount of time, in milliseconds, that asynchronous scripts executed by `/session/:sessionId/execute_async` are permitted to run before they are aborted and a `Timeout` error is returned to the client.
116+
![Implemented](./docs/implemented.png "Implemented") | POST | `/session/:sessionId/timeouts` | Configure the amount of time that a particular type of operation can execute for before they are aborted and a `Timeout` error is returned to the client.
117+
![Implemented](./docs/implemented.png "Implemented") | POST | `/session/:sessionId/timeouts/async_script` | Set the amount of time, in milliseconds, that asynchronous scripts executed by `/session/:sessionId/execute_async` are permitted to run before they are aborted and a `Timeout` error is returned to the client.
115118
![Implemented](./docs/implemented.png "Implemented") | POST | `/session/:sessionId/timeouts/implicit_wait` | Set the amount of time the driver should wait when searching for elements.
116119
![Implemented](./docs/implemented.png "Implemented") | GET | `/session/:sessionId/window_handle` | Retrieve the current window handle.
117120
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | GET | `/session/:sessionId/window_handles` | Retrieve the list of all window handles available to the session.
@@ -120,8 +123,8 @@ Status | HTTP Method | Path | Summary
120123
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | POST | `/session/:sessionId/forward` | Navigate forwards in the browser history, if possible.
121124
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | POST | `/session/:sessionId/back` | Navigate backwards in the browser history, if possible.
122125
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | POST | `/session/:sessionId/refresh` | Refresh the current page.
123-
![Implemented](./docs/not_implemented.png "Not Yet Implemented") | POST | `/session/:sessionId/execute` | Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
124-
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | POST | `/session/:sessionId/execute_async` | Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
126+
![Implemented](./docs/implemented.png "Implemented") | POST | `/session/:sessionId/execute` | Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
127+
![Implemented](./docs/implemented.png "Implemented") | POST | `/session/:sessionId/execute_async` | Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
125128
![Implemented](./docs/implemented.png "Implemented") | GET | `/session/:sessionId/screenshot` | Take a screenshot of the current page.
126129
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | GET | `/session/:sessionId/ime/available_engines` | List all available engines on the machine.
127130
![Not Yet Implemented](./docs/not_implemented.png "Not Yet Implemented") | GET | `/session/:sessionId/ime/active_engine` | Get the name of the active IME engine.

lib/index.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3030
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3131
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
*/
33-
var WebDriver, assert, buildRequester, createAlertApi, createCookieApi, createDebugApi, createElementApi, createLocalStorageApi, createLocationApi, createNavigationApi, createPageApi, createSession, createWindowApi, extend, http, parseResponseData;
33+
var WebDriver, assert, buildRequester, createAlertApi, createCookieApi, createDebugApi, createElementApi, createLocalStorageApi, createLocationApi, createNavigationApi, createPageApi, createSession, createWindowApi, extend, http, parseResponseData,
34+
slice = [].slice;
3435

3536
assert = require('assertive');
3637

@@ -95,11 +96,52 @@ module.exports = WebDriver = (function() {
9596
this.http["delete"]('');
9697
};
9798

99+
WebDriver.prototype.setTimeouts = function(type, ms) {
100+
this.http.post("/timeouts", {
101+
type: type,
102+
ms: ms
103+
});
104+
};
105+
106+
WebDriver.prototype.setScriptTimeout = function(ms) {
107+
this.http.post("/timeouts/async_script", {
108+
ms: ms
109+
});
110+
};
111+
98112
WebDriver.prototype.evaluate = function(clientFunctionString) {
99-
var error, error1, friendlyError, response;
113+
var args, error, error1, friendlyError, response;
114+
if (arguments.length > 1) {
115+
clientFunctionString = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
116+
}
117+
if (typeof clientFunctionString === 'function') {
118+
clientFunctionString = 'return (' + clientFunctionString + ').apply(this, arguments)';
119+
}
100120
response = this.http.post("/execute", {
101121
script: clientFunctionString,
102-
args: []
122+
args: args != null ? args : []
123+
});
124+
try {
125+
return parseResponseData(response);
126+
} catch (error1) {
127+
error = error1;
128+
friendlyError = new Error("Error evaluating JavaScript: " + clientFunctionString + "\n" + error.message);
129+
friendlyError.inner = error;
130+
throw friendlyError;
131+
}
132+
};
133+
134+
WebDriver.prototype.evaluateAsync = function(clientFunctionString) {
135+
var args, error, error1, friendlyError, response;
136+
if (arguments.length > 1) {
137+
clientFunctionString = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
138+
}
139+
if (typeof clientFunctionString === 'function') {
140+
clientFunctionString = 'return (' + clientFunctionString + ').apply(this, arguments)';
141+
}
142+
response = this.http.post("/execute_async", {
143+
script: clientFunctionString,
144+
args: args != null ? args : []
103145
});
104146
try {
105147
return parseResponseData(response);

src/index.coffee

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,42 @@ module.exports = class WebDriver
7777
@http.delete ''
7878
return
7979

80+
setTimeouts: (type, ms) ->
81+
@http.post "/timeouts", { type, ms }
82+
return
83+
84+
setScriptTimeout: (ms) ->
85+
@http.post "/timeouts/async_script", { ms }
86+
return
87+
8088
evaluate: (clientFunctionString) ->
89+
if arguments.length > 1
90+
[clientFunctionString, args...] = arguments
91+
92+
if typeof clientFunctionString == 'function'
93+
clientFunctionString = 'return (' + clientFunctionString + ').apply(this, arguments)'
94+
8195
response = @http.post "/execute",
8296
script: clientFunctionString
83-
args: []
97+
args: args ? []
98+
99+
try
100+
return parseResponseData(response)
101+
catch error
102+
friendlyError = new Error "Error evaluating JavaScript: #{clientFunctionString}\n#{error.message}"
103+
friendlyError.inner = error
104+
throw friendlyError
105+
106+
evaluateAsync: (clientFunctionString) ->
107+
if arguments.length > 1
108+
[clientFunctionString, args...] = arguments
109+
110+
if typeof clientFunctionString == 'function'
111+
clientFunctionString = 'return (' + clientFunctionString + ').apply(this, arguments)'
112+
113+
response = @http.post "/execute_async",
114+
script: clientFunctionString
115+
args: args ? []
84116

85117
try
86118
return parseResponseData(response)

0 commit comments

Comments
 (0)