Package Exports
- webdriverio
- webdriverio/lib/utils/ErrorHandler.js
- webdriverio/lib/webdriverio
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (webdriverio) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
WebdriverIO

This library is a webdriver module for Node.js. It makes it possible to write super easy selenium tests in your favorite BDD or TDD test framework. It was originated by Camilo Tapia's inital selenium project called WebdriverJS.
Have a look at the many examples.
For news or announcements follow @webdriverio on Twitter.
How to install it
npm install webdriverioUsage
Make sure you have a running Selenium standalone/grid/hub.
Or use selenium-standalone package to run one easily.
var webdriverio = require('../index');
var options = {
desiredCapabilities: {
browserName: 'chrome'
}
};
webdriverio
.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).
Options
desiredCapabilities
Type: Object
Example:
browserName: 'chrome', // options: firefox, chrome, opera, safari
version: '27.0', // browser version
platform: 'XP', // OS platform
tags: ['tag1','tag2'], // specify some tags (e.g. if you use Sauce Labs)
name: 'my test' // set name for test (e.g. if you use Sauce Labs)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
Selector API
The JsonWireProtocol provides several strategies to query an element. WebdriverIO simplifies these to make it more familiar with the common existing selector libraries like Sizzle. The following selector types are supported:
- CSS query selector
e.g.client.click('h2.subheading a', function(err,res) {...})etc. - link text
To get an anchor element with a specific text in it (f.i.<a href="http://webdriver.io">WebdriverIO</a>) query the text starting with an equal (=) sign. In this example use=WebdriverIO - partial link text
To find a anchor element whose visible text partially matches your search value, query it by using*=in front of the query string (e.g.*=driver) - tag name
To query an element with a specific tag name use<tag>or<tag /> - name attribute
For quering elements with a specific name attribute you can eather use a normal CSS3 selector or the provided name strategy from the JsonWireProtocol by passing something like[name="some-name"]as selector parameter - xPath
It is also possible to query elements via a specific xPath. The selector has to have a format like for example//BODY/DIV[6]/DIV[1]/SPAN[1]
In near future WebdriverIO will cover more selector features like form selector (e.g. :password,:file etc)
or positional selectors like :first or :nth.
Eventhandling
WebdriverIO inherits several function from the NodeJS EventEmitter object. Additionally it provides an experimental way to register events on browser side (like click, focus, keypress etc.).
Eventhandling
The following functions are supported: on,once,emit,removeListener,removeAllListeners.
They behave exactly as described in the official NodeJS docs.
There are some predefined events (error,init,end, command) which cover important
WebdriverIO events.
Example:
client.on('error', function(e) {
// will be executed everytime an error occured
// e.g. when element couldn't be found
console.log(e.body.value.class); // -> "org.openqa.selenium.NoSuchElementException"
console.log(e.body.value.message); // -> "no such element ..."
})All commands are chainable, so you can use them while chaining your commands
var cnt;
client
.init()
.once('countme', function(e) {
console.log(e.elements.length, 'elements were found');
})
.elements('.myElem', function(err,res) {
cnt = res.value;
})
.emit('countme', cnt)
.end();Note: make sure you check out the Browserevent side project that enables event-handling on client side (Yes, in the browser!! ;-).
Adding custom commands
If you want to extend the client with your own set of commands there is a
method called addCommand available from the client object:
var client = require("webdriverio").remote();
// example: create a command the returns the current url and title as one result
// last parameter has to be a callback function that needs to be called
// when the command has finished (otherwise the queue stops)
client.addCommand("getUrlAndTitle", function(customVar, cb) {
this.url(function(err,urlResult) {
this.getTitle(function(err,titleResult) {
var specialResult = {url: urlResult.value, title: titleResult};
cb(err,specialResult);
console.log(customVar); // "a custom variable"
})
});
});
client
.init()
.url('http://www.github.com')
.getUrlAndTitle('a custom variable', 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 WebdriverIO, you can easily add mocha tests and run them locally:
npm install -g selenium-standalone http-server phantomjs
# start a local Selenium instances
start-selenium
# serves the test directory holding the test files
http-server
# runs tests !
npm testSelenium cloud providers
WebdriverIO supports
See the corresponding examples.
List of current commands methods
These are the current implemented command 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. WebdriverIO has all JSONWire protocol commands implemented and even a whole bunch of undocumented Appium commands of the Selenium.
- addValue(
Stringselector,String|String[]value,Functioncallback)
adds a value to an object found by a selector. You can also use unicode characters likeLeft arroworBack space. You'll find all supported characters here. To do that, the value has to correspond to a key from the table. - call(callback)
call given function in async order of current command queue - chooseFile(
Stringselector,StringlocalFilePath,Functioncallback)
Given a selector corresponding to an<input type=file>, will upload the local file to the browser machine and fill the form accordingly. It does not submit the form for you. - clearElement(
Stringselector,Functioncallback)
clear an element of text - click(
Stringselector,Functioncallback)
Clicks on an element based on a selector. - close([
Stringtab ID to focus on,]Functioncallback)
Close the current window (optional: and switch focus to opended tab) - deleteCookie(
Stringname,Functioncallback)
Delete a cookie for current page. - doubleClick(
Stringselector,Functioncallback)
Clicks on an element based on a selector - drag(
Stringselector,NumberstartX,NumberstartY,NumberendX,NumberendY,NumbertouchCount,Numberduration,Functioncallback)
Perform a drag on the screen or an element (works only on Appium) - dragAndDrop(
StringsourceCssSelector,StringdestinationCssSelector,Functioncallback)
Drags an item to a destination - dragDown(
Stringselector,NumbertouchCount,Numberduration,Functioncallback)
Perform a drag down on an element (works only on Appium) - dragLeft(
Stringselector,NumbertouchCount,Numberduration,Functioncallback)
Perform a drag left on an element (works only on Appium) - dragRight(
Stringselector,NumbertouchCount,Numberduration,Functioncallback)
Perform a drag right on an element (works only on Appium) - dragUp(
Stringselector,NumbertouchCount,Numberduration,Functioncallback)
Perform a drag up on an element (works only on Appium) - emit(
StringeventName, [arg1], [arg2], [...])
Execute each event listeners in order with the supplied arguments. - end(
Functioncallback)
Ends a sessions (closes the browser) - endAll(
Functioncallback)
Ends all sessions (closes the browser) - execute(
StringorFunctionscript,Arrayarguments,Functioncallback)
Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. If script is aFunction, arguments is required. - flick(
Stringselector,NumberstartX,NumberstartY,NumberendX,NumberendY,NumbertouchCount,Functioncallback)
Perform a flick on the screen or an element (works only on Appium) - getAttribute(
Stringselector,Stringattribute name,Functioncallback)
Get an attribute from an dom obj based on the selector and attribute name - getCookie(
Stringname,Functioncallback)
Get cookie for the current page. If no cookie name is specified the command will return all cookies. - getCssProperty(
Stringselector,Stringcss property name,Functioncallback)
Gets a css property from a dom object selected with a selector - getCurrentTabId(
Functioncallback)
Retrieve the current window handle. - getElementSize(
Stringselector,Functioncallback)
Gets the width and height for an object based on the selector - getLocation(
Stringselector,Functioncallback)
Gets the x and y coordinate for an object based on the selector - getLocationInView(
Stringselector,Functioncallback)
Gets the x and y coordinate for an object based on the selector in the view - getOrientation(
Functioncallback)
Get the current browser orientation. - getSource(
Functioncallback)
Gets source code of the page - getTabIds(
Functioncallback)
Retrieve the list of all window handles available to the session. - getTagName(
Stringselector,Functioncallback)
Gets the tag name of a dom obj found by the selector - getText(
Stringselector,Functioncallback)
Gets the text content from a dom obj found by the selector - getTitle(
Functioncallback)
Gets the title of the page - getValue(
Stringselector,Functioncallback)
Gets the value of a dom obj found by selector - isSelected(
Stringselector,Functioncallback)
Return true or false if an OPTION element, or an INPUT element of type checkbox or radiobutton is currently selected (found by selector). - isVisible(
Stringselector,Functioncallback)
Return true or false if the selected dom obj is visible (found by selector) - leftClick(
Stringselector,Functioncallback)
Apply left click at an element. If selector is not provided, click at the last moved-to location. - hold(
Stringselector,Functioncallback)
Long press on an element using finger motion events. - middleClick(
Stringselector,Functioncallback)
Apply middle click at an element. If selector is not provided, click at the last moved-to location. - moveToObject(
Stringselector,Functioncallback)
Moves the page to the selected dom object - newWindow(
Stringurl,Stringname for the new window,Stringnew window features (e.g. size, position, scrollbars, etc.),Functioncallback)
equivalent function toWindow.open()in a browser - on(
StringeventName,Functionfn)
Register event listener on specific event (the following are already defined:init,command,end,error) - once(
StringeventName,Functionfn)
Adds a one time listener for the event (the following are already defined:init,command,end,error) - pause(
Integermilliseconds,Functioncallback)
Pauses the commands by the provided milliseconds - refresh(
Functioncallback)
Refresh the current page - release(
Stringselector,Functioncallback)
Finger up on an element - removeListener(
StringeventName,Functionfn)
Remove a listener from the listener array for the specified event - removeAllListeners([
StringeventName])
Removes all listeners, or those of the specified event - rightClick(
Stringselector,Functioncallback)
Apply right click at an element. If selector is not provided, click at the last moved-to location. - saveScreenshot(
Stringpath to file,Functioncallback)
Saves a screenshot as a png from the current state of the browser - scroll(
Stringselector,Functioncallback)
Scroll to a specific element. You can also pass two offset values as parameter to scroll to a specific position (e.g.scroll(xoffset,yoffset,callback)). - setCookie(
Objectcookie,Functioncallback)
Sets a cookie for current page. - setOrientation(
Stringorientation,Functioncallback)
Set the current browser orientation. - setValue(
Stringselector,String|String[]value,Functioncallback)
Sets a value to an object found by a selector (clears value before). You can also use unicode characters likeLeft arroworBack space. You'll find all supported characters here. To do that, the value has to correspond to a key from the table. - submitForm(
Stringselector,Functioncallback)
Submits a form found by the selector - switchTab(
Stringtab ID)
switch focus to a particular tab/window - tap(
Stringselector,Numberx,Numbery,NumbertapCount,NumbertouchCount,Numberduration,Functioncallback)
Perform a tap on the screen or an element (works only on Appium) - touch(
Stringselector,Functioncallback)
Finger down on an element. - waitFor(
Stringselector,Integermilliseconds,Functioncallback)
Waits for an object in the dom (selected by selector) for the amount of milliseconds provided. the callback is called with false if the object isnt found.
More on Selenium and its protocol
NPM Maintainers
The npm module for this library is maintained by: