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
Next-gen browser and mobile automation test framework for Node.js
This package provides an easy-to-manage API and a lot of syntactical sugar on top of the WebDriver specification. You can use WebdriverIO as a standalone package or via a test runner using @wdio/cli
. WebdriverIO allows you to run tests locally using the WebDriver as well as remote user agents using cloud providers like Sauce Labs.
Installation
You can install WebdriverIO via NPM:
npm install webdriverio
Usage
WebdriverIO by default uses Puppeteer to automate a browser like Chrome, Firefox or Chromium Edge. So if you have Chrome installed, the following script should start a browser for you and get the title of the page:
import { remote } from 'webdriverio'
const browser = await remote({
capabilities: { browserName: 'chrome' }
})
await browser.navigateTo('https://www.google.com/ncr')
const searchInput = await browser.$('#lst-ib')
await searchInput.setValue('WebdriverIO')
const searchBtn = await browser.$('input[value="Google Search"]')
await searchBtn.click()
console.log(await browser.getTitle())
await browser.deleteSession()
See the raw protocol example using the webdriver
package to get a glance at the differences.
For more information on options, multiremote usage or integration into cloud services please check out the docs.
Package Sponsors: