Webdriver/selenium 2.0 javascript bindings for nodejs
This library is a webdriver module for nodejs. It makes it possible to write
super easy selenium tests in your favorite BDD or TDD test framework.
Have a look at the many examples.
Breaking 1.x API changes, see upgrading to 1.x.
How to install it
npm install webdriverjs
Usage
webdriverjs
implements most of selenium's JsonWireProtocol.
Make sure you have a running selenium standalone/grid/hub.
Or use selenium-standalone package to run one easily.
var webdriverjs = require('../index');
var options = {
desiredCapabilities: {
browserName: 'chrome'
}
};
webdriverjs
.remote(options)
.init()
.url('http://www.google.com')
.title(function(err, res) {
console.log('Title was: ' + res.value);
})
.end();
See the full list of options you can pass to .remote(options)
See helpers and protocol methods.
Options
desiredCapabilities
Type: Object
Example:
browserName: 'chrome',
version: '27.0',
platform: 'XP',
tags: ['tag1','tag2'],
name: 'my test'
See the selenium documentation for a list of the available capabilities
.
logLevel
Type: String
Default: silent
Options: verbose | silent | command | data | result
screenshotPath
Saves a screenshot to a given path if selenium driver crashes
Type: String
|null
Default: null
singleton
Type: Boolean
Default: false
Set to true if you always want to reuse the same remote
Adding custom commands
If you which to extend with your own set of commands there is a method
called addCommand
available from the client object:
var client = require("webdriverjs").remote();
client.addCommand("getUrlAndTitle", function(cb) {
this.url(function(err,urlResult) {
this.getTitle(function(err,titleResult) {
var specialResult = {url: urlResult.value, title: titleResult};
cb(err,specialResult);
})
});
});
client
.init()
.url('http://www.github.com')
.getUrlAndTitle(function(err,result){
assert.equal(null, err)
assert.strictEqual(result.url,'https://github.com/');
assert.strictEqual(result.title,'GitHub · Build software better, together.');
})
.end();
Local testing
If you want to help us in developing webdriverjs, you can easily add
mocha tests and run them locally:
npm install -g selenium-standalone http-server
start-selenium
http-server test
npm test
Selenium cloud providers
Webdriverjs supports
See the corresponding examples.
List of current helper methods
These are the current implemented helper methods. All methods take from 0
to a couple of parameters. Also all methods accept a callback so that we
can assert values or have more logic when the callback is called.
- addValue(
String
css selector, String|String[]
value, Function
callback)
adds a value to an object found by a css selector. You can also use unicode characters like Left arrow
or Back space
. You'll find all supported characters here. To do that, the value has to correspond to a key from the table. - buttonClick(
String
css selector, Function
callback)
click on a button using a css selector - call(callback)
call given function in async order of current command queue - clearElement(
String
css selector, Function
callback)
clear an element of text - click(
String
css selector, Function
callback)
Clicks on an element based on a css selector - close([
String
tab ID to focus on,] Function
callback)
Close the current window (optional: and switch focus to opended tab) - deleteCookie(
String
name, Function
callback)
Delete a cookie for current page. - doubleClick(
String
css selector, Function
callback)
Clicks on an element based on a css selector - dragAndDrop(
String
sourceCssSelector, String
destinationCssSelector, Function
callback)
Drags an item to a destination - end(
Function
callback)
Ends a sessions (closes the browser) - endAll(
Function
callback)
Ends all sessions (closes the browser) - execute(
String
script, Array
arguments, Function
callback)
Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. - getAttribute(
String
css selector, String
attribute name, Function
callback)
Get an attribute from an dom obj based on the css selector and attribute name - getCookie(
String
name, Function
callback)
Gets the cookie for current page. - getCssProperty(
String
css selector, String
css property name, Function
callback)
Gets a css property from a dom object selected with a css selector - getCurrentTabId(
Function
callback)
Retrieve the current window handle. - getElementCssProperty(
String
find by, String
finder, String
css property name, Function
callback)
Gets a css property from a dom object selected with one of the base selecting mechanisms in the webdriver protocol (class name, css selector, id, name, link text, partial link text, tag name, xpath) - getElementSize(
String
css selector, Function
callback)
Gets the width and height for an object based on the css selector - getLocation(
String
css selector, Function
callback)
Gets the x and y coordinate for an object based on the css selector - getLocationInView(
String
css selector, Function
callback)
Gets the x and y coordinate for an object based on the css selector in the view - getSource(
Function
callback)
Gets source code of the page - getTabIds(
Function
callback)
Retrieve the list of all window handles available to the session. - getTagName(
String
css selector, Function
callback)
Gets the tag name of a dom obj found by the css selector - getText(
String
css selector, Function
callback)
Gets the text content from a dom obj found by the css selector - getTitle(
Function
callback)
Gets the title of the page - getValue(
String
css selector, Function
callback)
Gets the value of a dom obj found by css selector - isSelected(
String
css selector, Function
callback)
Return true or false if an OPTION element, or an INPUT element of type checkbox or radiobutton is currently selected (found by css selector). - isVisible(
String
css selector, Function
callback)
Return true or false if the selected dom obj is visible (found by css selector) - moveToObject(
String
css selector, Function
callback)
Moves the page to the selected dom object - newWindow(
String
url, String
name for the new window, String
new window features (e.g. size, position, scrollbars, etc.), Function
callback)
equivalent function to Window.open()
in a browser - pause(
Integer
milliseconds, Function
callback)
Pauses the commands by the provided milliseconds - refresh(
Function
callback)
Refresh the current page - saveScreenshot(
String
path to file, Function
callback)
Saves a screenshot as a png from the current state of the browser - setCookie(
Object
cookie, Function
callback)
Sets a cookie for current page. - setValue(
String
css selector, String|String[]
value, Function
callback)
Sets a value to an object found by a css selector (clears value before). You can also use unicode characters like Left arrow
or Back space
. You'll find all supported characters here. To do that, the value has to correspond to a key from the table. - submitForm(
String
css selector, Function
callback)
Submits a form found by the css selector - switchTab(
String
tab ID)
switch focus to a particular tab/window - waitFor(
String
css selector, Integer
milliseconds, Function
callback)
Waits for an object in the dom (selected by css selector) for the amount of milliseconds provided. the callback is called with false if the object isnt found.
List of current implemented wire protocol bindings
Here are the implemented bindings (and links to the official json protocol binding)
More on selenium and its protocol
Upgrading to 1.x
The 1.x refactor was done to fix many bugs due to the chain API.
We also removed some features that were not well designed/fully functionning:
- default logger is now silent
singleton
option defaults to false
webdriverjs.endAll
and webdriverjs.sessions
methods are gone. You must access them on the
client
instance
NPM Maintainers
The npm module for this library is maintained by: