What is webdriverio?
WebdriverIO is an open-source testing utility for Node.js that allows you to control a browser or a mobile application with just a few lines of code. It is built on top of WebDriver protocol and supports both desktop browsers and mobile applications. It provides a lot of useful features to create end-to-end tests, supports synchronous and asynchronous modes, and integrates with various test frameworks like Mocha, Jasmine, and Cucumber.
What are webdriverio's main functionalities?
Browser Automation
Automate web browsers by performing actions like navigating to URLs, clicking on elements, and retrieving page information.
const { remote } = require('webdriverio');
(async () => {
const browser = await remote({
capabilities: { browserName: 'chrome' }
});
await browser.url('https://example.com');
const title = await browser.getTitle();
console.log('Title was: ' + title);
await browser.deleteSession();
})();
Element Interaction
Interact with web elements such as input fields, buttons, and links by sending keystrokes, clicking, and retrieving attributes.
const { remote } = require('webdriverio');
(async () => {
const browser = await remote({
capabilities: { browserName: 'chrome' }
});
await browser.url('https://example.com/login');
await browser.setValue('#username', 'user123');
await browser.setValue('#password', 'pass123');
await browser.click('#submit');
await browser.deleteSession();
})();
Mobile Application Testing
Test mobile applications by launching them on simulators, emulators, or real devices and interacting with them just like with browser automation.
const { remote } = require('webdriverio');
(async () => {
const browser = await remote({
path: '/wd/hub',
capabilities: {
platformName: 'Android',
'appium:deviceName': 'emulator',
'appium:app': '/path/to/your.app'
}
});
// Your mobile testing code here
await browser.deleteSession();
})();
Integration with Test Runners
Easily integrate with test runners like Mocha, Jasmine, or Cucumber to create structured and maintainable test suites.
const { remote } = require('webdriverio');
describe('My application', () => {
it('should work with webdriverio', async () => {
const browser = await remote({
capabilities: { browserName: 'chrome' }
});
await browser.url('https://example.com');
expect(await browser.getTitle()).toBe('Expected Title');
await browser.deleteSession();
});
});
Other packages similar to webdriverio
selenium-webdriver
Selenium WebDriver is one of the most popular browser automation tools. It requires setting up a standalone server and writing more boilerplate code compared to WebdriverIO, which provides a more concise API and integrated test runner support.
puppeteer
Puppeteer is a Node library developed by the Chrome DevTools team. It provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer is typically faster and more stable for Chrome/Chromium automation because it uses the DevTools protocol, but it does not support multiple browsers out of the box like WebdriverIO does.
cypress
Cypress is a front-end testing tool built for the modern web. It is both a library for writing tests as well as a test runner. It offers a rich interactive interface for running tests but is limited to running tests within its own browser-based test runner, which can be a limitation compared to WebdriverIO's support for various browsers and mobile platforms.
nightwatch
Nightwatch.js is an automated testing framework for web applications and websites, written in Node.js and using the W3C WebDriver API. It is similar to WebdriverIO but has its own test runner and asserts library, which can make it easier to set up and start writing tests. However, it might not be as feature-rich as WebdriverIO in terms of community plugins and integrations.
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 webdriverio
Usage
webdriverio
implements all commands of the Selenium JsonWireProtocol.
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)
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
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) {
console.log(e.body.value.class);
console.log(e.body.value.message);
})
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 which to extend with your own set of commands there is a method
called addCommand
available from the client object:
var client = require("webdriverio").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 WebdriverIO, you can easily add
mocha tests and run them locally:
npm install -g selenium-standalone http-server phantomjs
start-selenium
http-server
npm test
Selenium cloud providers
WebdriverIO 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
selector, String|String[]
value, Function
callback)
adds a value to an object found by a 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. - call(callback)
call given function in async order of current command queue - chooseFile(
String
selector, String
localFilePath, Function
callback)
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(
String
selector, Function
callback)
clear an element of text - click(
String
selector, Function
callback)
Clicks on an element based on a 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
selector, Function
callback)
Clicks on an element based on a selector - drag(
String
selector, Number
startX, Number
startY, Number
endX, Number
endY, Number
touchCount, Number
duration, Function
callback)
Perform a drag on the screen or an element (works only on Appium) - dragAndDrop(
String
sourceCssSelector, String
destinationCssSelector, Function
callback)
Drags an item to a destination - dragDown(
String
selector, Number
touchCount, Number
duration, Function
callback)
Perform a drag down on an element (works only on Appium) - dragLeft(
String
selector, Number
touchCount, Number
duration, Function
callback)
Perform a drag left on an element (works only on Appium) - dragRight(
String
selector, Number
touchCount, Number
duration, Function
callback)
Perform a drag right on an element (works only on Appium) - dragUp(
String
selector, Number
touchCount, Number
duration, Function
callback)
Perform a drag up on an element (works only on Appium) - emit(
String
eventName, [arg1], [arg2], [...])
Execute each event listeners in order with the supplied arguments. - end(
Function
callback)
Ends a sessions (closes the browser) - endAll(
Function
callback)
Ends all sessions (closes the browser) - execute(
String
or Function
script, Array
arguments, Function
callback)
Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. If script is a Function
, arguments is required. - flick(
String
selector, Number
startX, Number
startY, Number
endX, Number
endY, Number
touchCount, Function
callback)
Perform a flick on the screen or an element (works only on Appium) - getAttribute(
String
selector, String
attribute name, Function
callback)
Get an attribute from an dom obj based on the selector and attribute name - getCookie(
String
name, Function
callback)
Get cookie for the current page. If no cookie name is specified the command will return all cookies. - getCssProperty(
String
selector, String
css property name, Function
callback)
Gets a css property from a dom object selected with a selector - getCurrentTabId(
Function
callback)
Retrieve the current window handle. - getElementSize(
String
selector, Function
callback)
Gets the width and height for an object based on the selector - getLocation(
String
selector, Function
callback)
Gets the x and y coordinate for an object based on the selector - getLocationInView(
String
selector, Function
callback)
Gets the x and y coordinate for an object based on the selector in the view - getOrientation(
Function
callback)
Get the current browser orientation. - 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
selector, Function
callback)
Gets the tag name of a dom obj found by the selector - getText(
String
selector, Function
callback)
Gets the text content from a dom obj found by the selector - getTitle(
Function
callback)
Gets the title of the page - getValue(
String
selector, Function
callback)
Gets the value of a dom obj found by selector - isSelected(
String
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 selector). - isVisible(
String
selector, Function
callback)
Return true or false if the selected dom obj is visible (found by selector) - leftClick(
String
selector, Function
callback)
Apply left click at an element. If selector is not provided, click at the last moved-to location. - hold(
String
selector,Function
callback)
Long press on an element using finger motion events. - middleClick(
String
selector, Function
callback)
Apply middle click at an element. If selector is not provided, click at the last moved-to location. - moveToObject(
String
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 - on(
String
eventName, Function
fn)
Register event listener on specific event (the following are already defined: init
,command
,end
,error
) - once(
String
eventName, Function
fn)
Adds a one time listener for the event (the following are already defined: init
,command
,end
,error
) - pause(
Integer
milliseconds, Function
callback)
Pauses the commands by the provided milliseconds - refresh(
Function
callback)
Refresh the current page - release(
String
selector, Function
callback)
Finger up on an element - removeListener(
String
eventName, Function
fn)
Remove a listener from the listener array for the specified event - removeAllListeners([
String
eventName])
Removes all listeners, or those of the specified event - rightClick(
String
selector, Function
callback)
Apply right click at an element. If selector is not provided, click at the last moved-to location. - saveScreenshot(
String
path to file, Function
callback)
Saves a screenshot as a png from the current state of the browser - scroll(
String
selector, Function
callback)
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(
Object
cookie, Function
callback)
Sets a cookie for current page. - setOrientation(
String
orientation, Function
callback)
Set the current browser orientation. - setValue(
String
selector, String|String[]
value, Function
callback)
Sets a value to an object found by a 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
selector, Function
callback)
Submits a form found by the selector - switchTab(
String
tab ID)
switch focus to a particular tab/window - tap(
String
selector,Number
x,Number
y,Number
tapCount,Number
touchCount,Number
duration,Function
callback)
Perform a tap on the screen or an element (works only on Appium) - touch(
String
selector, Function
callback)
Finger down on an element. - waitFor(
String
selector, Integer
milliseconds, Function
callback)
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.
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
NPM Maintainers
The npm module for this library is maintained by: