What is selenium-webdriver?
The selenium-webdriver npm package is a browser automation library that provides an API for interacting with web browsers. It allows for the automation of web applications for testing purposes, but it's also used for automating web-based administration tasks. It supports multiple browsers including Chrome, Firefox, Internet Explorer, and Safari.
What are selenium-webdriver's main functionalities?
Browser Navigation
This feature allows for navigating to a web page. The code sample demonstrates how to open Firefox and navigate to Google's homepage.
const {Builder} = require('selenium-webdriver');
let driver = new Builder().forBrowser('firefox').build();
driver.get('http://www.google.com');
Element Interaction
This feature enables interaction with elements on a web page, such as input fields and buttons. The code sample shows how to find a search input by its name attribute and type 'selenium' into it.
const {Builder, By} = require('selenium-webdriver');
let driver = new Builder().forBrowser('firefox').build();
driver.findElement(By.name('q')).sendKeys('selenium');
Wait for Elements
This feature allows scripts to wait for elements to become available or visible on the page before interacting with them. The code sample demonstrates waiting up to 10 seconds for a search input to be located.
const {Builder, By, until} = require('selenium-webdriver');
let driver = new Builder().forBrowser('firefox').build();
driver.wait(until.elementLocated(By.name('q')), 10000);
Other packages similar to selenium-webdriver
puppeteer
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It is similar to selenium-webdriver but is specifically designed for Chrome and Chromium browsers. Puppeteer is often considered faster for browser automation tasks, especially with headless Chrome.
webdriverio
WebdriverIO is an automation library that wraps around the W3C WebDriver API. It provides a simpler API compared to selenium-webdriver and integrates well with modern testing frameworks. WebdriverIO supports both web and mobile browsers, making it a versatile choice for cross-platform testing.
nightwatch
Nightwatch.js is an automated testing framework for web applications and websites, written in Node.js. It uses the W3C WebDriver API for browser automation. It is designed to simplify the process of setting up continuous integration and writing automated tests. Nightwatch has a cleaner syntax compared to selenium-webdriver, which might be easier for beginners.
selenium-webdriver
Installation
Install the latest published version using npm
:
npm install selenium-webdriver
In addition to the npm package, you will to download the WebDriver
implementations you wish to utilize. As of 2.34.0, selenium-webdriver
natively supports the ChromeDriver.
Simply download a copy and make sure it can be found on your PATH
. The other
drivers (e.g. Firefox, Internet Explorer, and Safari), still require the
standalone Selenium server.
Running the tests
(New in 2.34.0) To run the tests, you will need to download a copy of the
ChromeDriver and make
sure it can be found on your PATH
.
npm test selenium-webdriver
To run the tests against multiple browsers, download the
Selenium server and
specify its location through the SELENIUM_SERVER_JAR
environment variable.
You can use the SELENIUM_BROWSER
environment variable to define a
comma-separated list of browsers you wish to test against. For example:
export SELENIUM_SERVER_JAR=path/to/selenium-server-standalone-2.33.0.jar
SELENIUM_BROWSER=chrome,firefox npm test selenium-webdriver
Usage
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
return driver.getTitle().then(function(title) {
return title === 'webdriver - Google Search';
});
}, 1000);
driver.quit();
Documentation
Full documentation is available on the Selenium project wiki.
Issues
Please report any issues using the Selenium issue tracker.
License
Apache License 2.0