Session Class
Create a new Web Driver session
Constructor
Session
-
[options]
-
callback
Parameters:
-
[options]
Object optionalOptions for WebDriver session
-
[hostname=localhost]
String optional -
[port=4444]
Number optionalSelenium server port
-
[path="/wd/hub"]
String optionalPath to Selenium server REST hub
-
[username]
String optionalUsername for Basic authentication
-
[password]
String optionalPassword for Basic authentication
-
-
callback
FunctionFunction context where to run WebDriver commands
Example:
new WebDriver.Session(function() {
this.go("http://google.com");
if (this.title.indexOf("Google") !== -1) {
console.log("Success!");
}
});
new WebDriver.Session({
hostname: "ondemand.saucelabs.com",
port: 80,
username: "<username>",
password: "<password>"
}, function() {
this.url = "http://facebook.com";
});
Item Index
Methods
Attributes
Methods
acceptAlert
()
chainable
Accepts the currently displayed alert dialog. Usually, this is equivalent to clicking on the 'OK' button in the dialog.
back
()
chainable
Navigate backwards in the browser history, if possible.
click
-
[button=MouseButtons.LEFT]
Click any mouse button (at the coordinates set by the last moveto command). Note that calling this command after calling buttondown and before calling button up (or any out-of-order interactions sequence) will yield undefined behaviour).
Parameters:
-
[button=MouseButtons.LEFT]
MouseButtons optionalWhich button, enum: {LEFT = 0, MIDDLE = 1 , RIGHT = 2}. Defaults to the left mouse button if not specified.
dismissAlert
()
chainable
Dismisses the currently displayed alert dialog. For confirm() and prompt() dialogs, this is equivalent to clicking the 'Cancel' button. For alert() dialogs, this is equivalent to clicking the 'OK' button.
doubleClick
()
chainable
Double-clicks at the current mouse coordinates (set by moveto).
element
-
value
-
[using=By.cssSelector]
Search for an element on the page, starting from the document root. The located element will be returned as a WebElement object. Each locator must return the first matching element located in the DOM.
Parameters:
-
value
StringThe search target
-
[using=By.cssSelector]
By optionalThe locator strategy to use.
Returns:
elements
-
value
-
[using=By.cssSelector]
Search for multiple elements on the page, starting from the document root. The located elements will be returned as an array of WebElement objects. Elements should be returned in the order located in the DOM.
Parameters:
-
value
StringThe search target
-
[using=By.cssSelector]
By optionalThe locator strategy to use.
Returns:
execute
-
func
-
[args]
Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be synchronous and the result of evaluating the script is returned to the client.
Arguments may be any JSON-primitive, array, or JSON object. JSON objects that define a WebElement reference will be converted to the corresponding DOM element. Likewise, any WebElements in the script result will be returned to the client as WebElement JSON objects.
Parameters:
-
func
FunctionThe function to execute
-
[args]
Array optionalThe function arguments
Returns:
Example:
var hiddenElement = this.element("#hidden");
this.execute(function(element) {
element.style.display = "block";
}, hiddenElement);
var body = this.execute(function() {
return document.body;
});
this.element("body").equals(body) // true;
executeAsync
-
func
-
timeout=1000
-
[args]
Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be asynchronous and must signal that is done by invoking the provided callback, which is always provided as the final argument to the function. The value to this callback will be returned to the client.
Asynchronous script commands may not span page loads. If an unload event is fired while waiting for a script result, an error should be returned to the client.
The script argument defines the script to execute in the form of a function. The function will be invoked with the provided args array and the values may be accessed via the arguments object in the order specified. The final argument will always be a callback function that must be invoked to signal that the script has finished.
Arguments may be any JSON-primitive, array, or JSON object. WebElement references will be converted to the corresponding DOM element. Likewise, any WebElements in the script result will be returned to the client as WebElement objects.
Parameters:
-
func
FunctionThe function to execute
-
timeout=1000
NumberSet the amount of time, in milliseconds, that asynchronous script permitted to run before they are aborted and a Timeout error is returned to the client.
-
[args]
Array optionalThe function arguments
Returns:
Example:
var input2 = this.executeAsync(function(done) {
window.setTimeout(function() {
done(document.getElementById("input2"));
}, 200);
}, 1000);
var asyncCalculation = this.executeAsync(function(a, b, done) {
window.setTimeout(function() {
done(a*b);
}, 100);
}, 2000, [5, 10]);
if (asyncCalculation === 50) {
console.log("Success! Running asynchronous code synchronously!");
}
forward
()
chainable
Navigate forwards in the browser history, if possible.
go
-
value
Navigate to a new URL.
Parameters:
-
value
StringThe URL to navigate to.
Example:
this.go("http://google.com");
keys
-
value
Send a sequence of key strokes to the active element. This command is similar to the send keys command in every aspect except the implicit termination: The modifiers are not released at the end of the call. Rather, the state of the modifier keys is kept between calls, so mouse interactions can be performed while modifier keys are depressed.
Parameters:
-
value
String | StringThe keys sequence to be sent.
moveMouse
-
xoffset
-
yoffset
-
[element=null]
Move the mouse by an offset of the specificed element. If no element is specified, the move is relative to the current mouse cursor. If an element is provided but no offset, the mouse will be moved to the center of the element. If the element is not visible, it will be scrolled into view. For alert() dialogs, this is equivalent to clicking the 'OK' button.
Parameters:
-
xoffset
NumberX offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
-
yoffset
NumberY offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
-
[element=null]
WebElement optionalWebElement to use for offset. If not specified or is null, the offset is relative to current position of the mouse.
refresh
()
chainable
Refresh the current page.
screenshot
()
String
Take a screenshot of the current page.