In this tutorial, we'll take a look at WebDriverJs which is a tool used for browser automation. Chrome will be used throughout however modern browsers tend to have available drivers for use with WebDriver (even mobile) so do check them out if you wish to automate other browsers.
While unit tests are certainly valuable for modern web applications, at some point, as your application grows, you'll find bugs crop up which weren't caught by a unit test but would have theoretically been caught by an integration/acceptance test.
Should you wish to follow a testing strategy which involves browser testing, this guide will give you an initial introduction to testing with WebDriverJs so you're equipped with enough knowledge to get started.
This tutorial assumes you're familiar with JavaScript and can run JavaScript code using node.js.
If you'd like to follow along, feel free to checkout this sample project which contains a few WebDriver examples for you to run. You'll also need to install Chromedriver and have it available in your path.
Selenium WebDriver typically has a server and a client. Apart from WebDriver contributors, most people will only be interested in the client API which allows them to control a browser through their script. To get started, install the JavaScript bindings for WebDriver:
npm install selenium-webdriver
Once you've installed that module via NPM, you can require the module in your node code like this:
require('selenium-webdriver');
Alternatively, if you check out the sample project, you can simply run an npm install inside the folder as the WebDriverJs module is listed as a dependancy in the package.json file.
While you can browse the official documentation, my personal favourite is the source itself. This webdriver.js file lists many WebDriver methods, e.g. you'll notice a getAttribute and a getText. Here are some methods which may be of interest:
get - Navigate the browser to a URL.findElements - Similar to document.querySelectorAll in the browser.executeScript - Execute raw JavaScript onto the current page.getText - Get the text content of an element including its children.isDisplayed - Find out if an element is displayed on the page.One factor about the JavaScript bindings for WebDriver in particular is that almost every method is asynchronous. It means the following code doesn't actually get the title of the web page:
var title = browser.getTitle();
//logs { then: [Function: then], cancel: [Function: cancel], isPending: [Function: isPending] }
console.log(title);Instead what you need to do, is this:
var promise = browser.getTitle();
promise.then(function(title) {
console.log(title);
});This is because WebDriverJs uses promises in order to make dealing with async code a bit more pleasant. Note that the promise implementation as part of WebDriverJs does not conform exactly to the Promises/A+ standard.
The key thing to take away here is that most WebDriver methods will return a then method which accepts two optional (function) arguments. The first argument is a callback which may receive a value.
In the above example, we asked for a title, therefore our callback will receive that title as its first argument. The second optional function argument we can pass to the then method allows us to catch errors, if at all.
Let's recap on where we are so far:
Have a look at this code example:
var webdriver = require('selenium-webdriver');
var browser = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
browser.get('http://en.wikipedia.org/wiki/Wiki');
browser.findElements(webdriver.By.css('[href^="/wiki/"]')).then(function(links){
console.log('Found', links.length, 'Wiki links.' )
browser.quit();
});Run the Wiki example like this:
$ node Wiki.js Found 367 Wiki links.
In the code example, the first few lines are essentially boilerplate. It initialises the browser object and specifies some initial configuration, like what browser to actually use. Starting with the call to browser.get, we have the code we really care about.
findElements method which will go ahead and asynchronously evaluate the selector expression.Finding elements on the page is one piece of the puzzle, let's take a look at another example which demonstrates carrying out a Google search and clicking on the result we expect to be on the page.
/*
* Carry out a Google Search
*/
"use strict";
var webdriver = require('selenium-webdriver');
var browser = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
function logTitle() {
browser.getTitle().then(function(title) {
console.log('Current Page Title: ' + title);
});
}
function clickLink(link) {
link.click();
}
function handleFailure(err) {
console.error('Something went wrong\n', err.stack, '\n');
closeBrowser();
}
function findTutsPlusLink() {
return browser.findElements(webdriver.By.css('[href="http://code.tutsplus.com/"]')).then(function(result) {
return result[0];
});
}
function closeBrowser() {
browser.quit();
}
browser.get('https://www.google.com');
browser.findElement(webdriver.By.name('q')).sendKeys('tuts+ code');
browser.findElement(webdriver.By.name('btnG')).click();
browser.wait(findTutsPlusLink, 2000).then(clickLink).then(logTitle).then(closeBrowser, handleFailure);Running the above code:
$ node GoogleSearch.js Current Page Title: Tuts+ Code Tutorials
A few interesting snippets are shown here. First, we can get a feel for what it's like to use function declarations - instead of anonymous function callbacks (that are passed to then), the result is something like a fluent API (see the last line). Also, since we have the ability to create custom promises (deferreds), we can be as fluent as we desire!
Note that we attach an error callback in the last call to then, even if an error occurs earlier on, it'll still propagate up.
We navigate to the Google home page and search for 'tuts+ code'. Since we're operating on the browser object, WebDriver's internal Control Flow mechanism knows to schedule each command to happen one after the other, this saves us the hassle of having to chain everything together and also explains why there are two calls to findElement, one after the other, without having to be chained to each other.
When we carry out the Google search from the homepage, no page reload occurs, therefore WebDriver will immediately try and find the elements we've instructed it to on the search results page. Knowing when to wait for elements is a critical part of browser automation.
The old and naughty way of doing things was to use a sleep. Since the point at which an element appears can heavily depend on external factors (e.g. network connection speed), developers can sometimes instruct WebDriver to wait for a fixed period of time before continuing. This, of course, is riddled with problems.
Fortunately, the wait method makes automating modern web pages a lot nicer. You call wait with two arguments, the first is a function which needs to evaluate to true by a time period defined as the second argument to wait. WebDriver regularly calls your callback until either it returns true, or time has run out in which case an error is thrown.
While there are many methods to call on the context of DOM elements, you can also call methods on the browser itself to give you more control over the browser state. Here are a few simple examples to give you a better idea:
Set the dimensions of the browser window
browser.manage().window().setSize(1280, 720)
Connect the browser to a proxy:
var proxy = require('selenium-webdriver/proxy');
browser = new webdriver.Builder()
.usingServer()
.withCapabilities({'browserName': 'chrome' })
.setProxy(proxy.manual({
http: '127.0.0.1:9000'
}))
.build();You can also read, write, and delete cookies, take a screenshot of the window, set some individual browser settings, and more.
There are a number of options available when wanting to control a browser programatically. First of all, we took a look at the JavaScript bindings for WebDriver however there are some others:
WebDriverJs, e.g. the version we installed using npm install selenium-webdriver is just one version of a WebDriver Client API written in JavaScript. If you're keen to programatically control browsers via JavaScript, there are also other options:
Using something like WD.js or Nightwatch can mean a number of things:
If you wish to start using WebDriver for the sake of testing, then that's great. You should also keep in mind that browser automation doesn't have to stop at testing, how about just automating a repetitive task?
For example, check out this article on Getting to Philosophy, it essentially explains how continuously clicking on the first link in Wiki articles will eventually land you on the Philosophy article!
This makes for a fun task to automate! Checkout out this animated gif or the source to see it in action.
Create Modern Vue Apps Using Create-Vue and Vite
/
5Pros and Cons of Using WordPress
/
-4How to Fix the “There Has Been a Critical Error in Your Website” Error in WordPress
/
1How To Fix The “There Has Been A Critical Error in Your Website” Error in WordPress
How to Create a Privacy Policy Page in WordPress
/
7WordPress Website Maintenance Guide For Beginners
/
6How Long Does It Take to Learn JavaScript?
/
-1How Long Does it Take to Learn JavaScript
/
1The Best Way to Deep Copy an Object in JavaScript
/
-2
4
-6Adding and Removing Elements From Arrays in JavaScript
/
-1Create a JavaScript AJAX Post Request: With and Without jQuery
/
95 Real-Life Uses for the JavaScript reduce() Method
/
-10How to Enable or Disable a Button With [removed] jQuery vs. Vanilla
/
2How to Enable or Disable a Button With [removed] jQuery vs Vanilla
/
-3Confirm Yes or No With JavaScript
/
-2How to Change the URL in [removed] Redirecting
/
-415+ Best WordPress Twitter Widgets
/
-227 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/
1021 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/
-1130 HTML Best Practices for Beginners
/
-731 Best WordPress Calendar Plugins and Widgets (With 5 Free Plugins)
/
625 Ridiculously Impressive HTML5 Canvas Experiments
/
-5How to Implement Email Verification for New Members
/
2How to Create a Simple Web-Based Chat Application
/
530 Popular WordPress User Interface Elements
/
-4Top 18 Best Practices for Writing Super Readable Code
/
3Best Affiliate WooCommerce Plugins Compared
/
-1018 Best WordPress Star Rating Plugins
/
1410+ Best WordPress Twitter Widgets
/
420+ Best WordPress Booking and Reservation Plugins
/
5Working With Tables in React: Part Two
/
-4Best CSS Animations and Effects on CodeCanyon
/
-1630 CSS Best Practices for Beginners
/
4How to Create a Custom WordPress Plugin From Scratch
/
510 Best Responsive HTML5 Sliders for Images and Text… and 3 Free Options
/
-516 Best Tab and Accordion Widget Plugins for WordPress
/
-418 Best WordPress Membership Plugins and 5 Free Plugins
/
325 Best WooCommerce Plugins for Products, Pricing, Payments and More
/
310 Best WordPress Twitter Widgets
1 /
1512 Best Contact Form PHP Scripts for 2020
/
-420 Popular WordPress User Interface Elements
/
-210 Best WordPress Star Rating Plugins
/
-412 Best CSS Animations on CodeCanyon
/
112 Best WordPress Booking and Reservation Plugins
/
120 Best WordPress Calendar Plugins and Widgets
/
-212 Elegant CSS Pricing Tables for Your Latest Web Project
/
724 Best WordPress Form Plugins for 2020
/
-214 Best PHP Event Calendar and Booking Scripts
/
-2Create a Blog for Each Category or Department in Your WooCommerce Store
/
28 Best WordPress Booking and Reservation Plugins
/
-17Best Exit Popups for WordPress Compared
/
20Best Exit Popups for WordPress Compared
/
-611 Best Tab & Accordion WordPress Widgets & Plugins
/
-1012 Best Tab & Accordion WordPress Widgets & Plugins
1New Course: Practical React Fundamentals
/
3Short Course: Better Angular App Architecture With Modules
/
2Preview Our New Course on Angular Material
/
4Object-Oriented PHP With Classes and Objects
/
3Best Practices for ARIA Implementation
/
-3Accessible Apps: Barriers to Access and Getting Started With Accessibility
/
-3Dramatically Speed Up Your React Front-End App Using Lazy Loading
/
1915 Best Modern JavaScript Admin Templates for React, Angular, and Vue.js
/
-1515 Best Modern JavaScript Admin Templates for React, Angular and Vue.js
/
219 Best JavaScript Admin Templates for React, Angular, and Vue.js
/
3New Course: Build an App With JavaScript and the MEAN Stack
/
12Hands-on With ARIA: Accessibility Recipes for Web Apps
/
910 Best WordPress Facebook Widgets
13 /
-5Hands-on With ARIA: Accessibility for eCommerce
/
6New eBooks Available for Subscribers
/
-5Hands-on With ARIA: Homepage Elements and Standard Navigation
/
10
-8Site Accessibility: Getting Started With ARIA
/
4How Secure Are Your JavaScript Open-Source Dependencies?
/
-1New Course: Secure Your WordPress Site With SSL
/
-3Testing Components in React Using Jest and Enzyme
/
-9Testing Components in React Using Jest: The Basics
/
-515 Best PHP Event Calendar and Booking Scripts
/
-1Create Interactive Gradient Animations Using Granim.js
/
10How to Build Complex, Large-Scale Vue.js Apps With Vuex
1Examples of Dependency Injection in PHP With Symfony Components
/
-6Set Up Routing in PHP Applications Using the Symfony Routing Component
1 /
11A Beginner’s Guide to Regular Expressions in JavaScript
/
13Introduction to Popmotion: Custom Animation Scrubber
/
-6Introduction to Popmotion: Pointers and Physics
/
-10New Course: Connect to a Database With Laravel’s Eloquent ORM
/
-5How to Create a Custom Settings Panel in WooCommerce
/
-12Building the DOM faster: speculative parsing, async, defer and preload
120 Useful PHP Scripts Available on CodeCanyon
3 /
51How to Find and Fix Poor Page Load Times With Raygun
/
1Introduction to the Stimulus Framework
/
-14Single-Page React Applications With the React-Router and React-Transition-Group Modules
/
2
712 Best Contact Form PHP Scripts
1 /
-5Getting Started With the Mojs Animation Library: The ShapeSwirl and Stagger Modules
/
1Getting Started With the Mojs Animation Library: The Shape Module
/
-3Getting Started With the Mojs Animation Library: The HTML Module
/
4Project Management Considerations for Your WordPress Project
/
-88 Things That Make Jest the Best React Testing Framework
/
4Creating an Image Editor Using CamanJS: Layers, Blend Modes, and Events
/
-2New Short Course: Code a Front-End App With GraphQL and React
/
1Creating an Image Editor Using CamanJS: Applying Basic Filters
/
-13Creating an Image Editor Using CamanJS: Creating Custom Filters and Blend Modes
/
-1Challenge: Create a To-Do List in React
1 /
1Deploy PHP Web Applications Using Laravel Forge
/
-2Getting Started With the Mojs Animation Library: The Burst Module
/
6
-1010 Things Men Can Do to Support Women in Tech
/
1A Gentle Introduction to Higher-Order Components in React: Best Practices
/
-4Challenge: Build a React Component
/
-4A Gentle Introduction to HOC in React: Learn by Example
/
5A Gentle Introduction to Higher-Order Components in React
/
-6Creating Pretty Popup Messages Using SweetAlert2
/
4Creating Stylish and Responsive Progress Bars Using ProgressBar.js
/
1818 Best Contact Form PHP Scripts for 2022
/
-3Creating a Blogging App Using Angular & MongoDB: Delete Post
/
-16Set Up an OAuth2 Server Using Passport in Laravel
/
1Creating a Blogging App Using Angular & MongoDB: Edit Post
/
-2Creating a Blogging App Using Angular & MongoDB: Add Post
/
-1Creating a Blogging App Using Angular & MongoDB: Show Post
/
9Creating a Blogging App Using Angular & MongoDB: Home
/
8
3Creating a Blogging App Using Angular & MongoDB: Login
/
13Creating Your First Angular App: Implement Routing
/
12Persisted WordPress Admin Notices: Part 4
/
-4Creating Your First Angular App: Components, Part 2
/
4Persisted WordPress Admin Notices: Part 3
/
3
8Persisted WordPress Admin Notices: Part 2
/
-3Create Your First Angular App: Storing and Accessing Data
/
3Persisted WordPress Admin Notices: Part 1
/
-3Error and Performance Monitoring for Web & Mobile Apps Using Raygun
/
6Using Luxon for Date and Time in JavaScript
7 /
-6How to Create an Audio Oscillator With the Web Audio API
/
-4How to Cache Using Redis in Django Applications
/
220 Essential WordPress Utilities to Manage Your Site
/
-5Introduction to API Calls With React and Axios
/
18Beginner’s Guide to Angular 4: HTTP
/
11Rapid Web Deployment for Laravel With GitHub, Linode, and RunCloud.io
/
9Beginners Guide to Angular 4: Routing
/
-9Beginner’s Guide to Angular 4: Components
/
1Creating a Drop-Down Menu for Mobile Pages
/
5Introduction to Forms in Angular 4: Writing Custom Form Validators
/
5Getting Started With Redux: Connecting Redux With React
/
15Getting Started With Redux: Learn by Example
/
16Getting Started With Redux: Why Redux?
/
9Understanding Recursion With JavaScript
/
-3How to Auto Update WordPress Salts
/
-7How to Download Files in Python
/
-3
-22Eloquent Mutators and Accessors in Laravel
1 /
18
-11
1310 Best HTML5 Sliders for Images and Text
/
6Site Authentication in Node.js: User Signup
/
-5Creating a Task Manager App Using Ionic: Part 2
/
1Creating a Task Manager App Using Ionic: Part 1
/
1
4Introduction to Forms in Angular 4: Template-Driven Forms
/
-224 Essential WordPress Utilities to Manage Your Site
/
-225 Essential WordPress Utilities to Manage Your Site
/
-11Get Rid of Bugs Quickly Using BugReplay
1 /
-16Manipulating HTML5 Canvas Using Konva: Part 1, Getting Started
/
610 Must-See Easy Digital Downloads Extensions for Your WordPress Site
/
-122 Best WordPress Booking and Reservation Plugins
/
-6Understanding ExpressJS Routing
/
-715 Best WordPress Star Rating Plugins
/
12Inheritance and Extending Objects With JavaScript
/
-9Introduction to the CSS Grid Layout With Examples
1 /
2
8Performant Animations Using KUTE.js: Part 5, Easing Functions and Attributes
/
-9Performant Animations Using KUTE.js: Part 4, Animating Text
/
-13Performant Animations Using KUTE.js: Part 3, Animating SVG
/
-10New Course: Code a Quiz App With Vue.js
/
-1Performant Animations Using KUTE.js: Part 2, Animating CSS Properties
/
3
8Performant Animations Using KUTE.js: Part 1, Getting Started
/
110 Best Responsive HTML5 Sliders for Images and Text (Plus 3 Free Options)
/
-3Single-Page Applications With ngRoute and ngAnimate in AngularJS
/
-12Deferring Tasks in Laravel Using Queues
/
-5
4Site Authentication in Node.js: User Signup and Login
/
12Working With Tables in React, Part Two
/
-16Working With Tables in React, Part One
/
6How to Set Up a Scalable, E-Commerce-Ready WordPress Site Using ClusterCS
/
-4New Course on WordPress Conditional Tags
/
2TypeScript for Beginners, Part 5: Generics
/
1
-11Building With Vue.js 2 and Firebase
6 /
11
-8
1Best Unique Bootstrap JavaScript Plugins
/
-9
-5Essential JavaScript Libraries and Frameworks You Should Know About
/
7
2Vue.js Crash Course: Create a Simple Blog Using Vue.js
/
-13Build a React App With a Laravel RESTful Back End: Part 1, Laravel 5.5 API
/
4API Authentication With Node.js
/
-4Beginner’s Guide to Angular: HTTP
/
7Beginner’s Guide to Angular: Routing
/
-4Beginners Guide to Angular: Routing
/
-6Beginner’s Guide to Angular: Services
/
4How to Create a Custom Authentication Guard in Laravel
/
6Learn Computer Science With [removed] Part 3, Loops
/
-3Build Web Applications Using Node.js
/
1Learn Computer Science With [removed] Part 4, Functions
/
4Learn Computer Science With [removed] Part 2, Conditionals
/
14Learn Computer Science With [removed] Part 1, The Basics
/
-4Create Interactive Charts Using Plotly.js, Part 5: Pie and Gauge Charts
/
-9Create Interactive Charts Using Plotly.js, Part 4: Bubble and Dot Charts
/
18Create Interactive Charts Using Plotly.js, Part 3: Bar Charts
/
-13Awesome JavaScript Libraries and Frameworks You Should Know About
/
-4Create Interactive Charts Using Plotly.js, Part 2: Line Charts
/
-8Bulk Import a CSV File Into MongoDB Using Mongoose With Node.js
/
-5Build a To-Do API With Node, Express, and MongoDB
/
-7Getting Started With End-to-End Testing in Angular Using Protractor
/
9TypeScript for Beginners, Part 4: Classes
/
-10Object-Oriented Programming With JavaScript
/
610 Best Affiliate WooCommerce Plugins Compared
/
8Stateful vs. Stateless Functional Components in React
/
10Make Your JavaScript Code Robust With Flow
/
-6Build a To-Do API With Node and Restify
/
7Testing Components in Angular Using Jasmine: Part 2, Services
/
2Testing Components in Angular Using Jasmine: Part 1
/
-2Creating a Blogging App Using React, Part 6: Tags
/
-11React Crash Course for Beginners, Part 3
/
1React Crash Course for Beginners, Part 2
/
10React Crash Course for Beginners, Part 1
/
2Set Up a React Environment, Part 4
1 /
8Set Up a React Environment, Part 3
/
-11New Course: Get Started With Phoenix
/
-10Set Up a React Environment, Part 2
/
2Set Up a React Environment, Part 1
/
1Command Line Basics and Useful Tricks With the Terminal
/
5How to Create a Real-Time Feed Using Phoenix and React
/
-8Build a React App With a Laravel Back End: Part 2, React
/
2Build a React App With a Laravel RESTful Back End: Part 1, Laravel 9 API
/
-6Creating a Blogging App Using React, Part 5: Profile Page
/
-5Pagination in CodeIgniter: The Complete Guide
/
23JavaScript-Based Animations Using Anime.js, Part 4: Callbacks, Easings, and SVG
/
-1JavaScript-Based Animations Using Anime.js, Part 3: Values, Timeline, and Playback
Learn to Code With [removed] Part 1, The Basics
/
510 Elegant CSS Pricing Tables for Your Latest Web Project
/
-7Getting Started With the Flux Architecture in React
/
-3Getting Started With Matter.js: The Composites and Composite Modules
/
4Getting Started With Matter.js: The Engine and World Modules
/
1310 More Popular HTML5 Projects for You to Use and Study
/
-24Understand the Basics of Laravel Middleware
/
-6Iterating Fast With Django & Heroku
/
8Creating a Blogging App Using React, Part 4: Update & Delete Posts
/
4Creating a jQuery Plugin for Long Shadow Design
/
-10How to Register & Use Laravel Service Providers
2 /
-13Unit Testing in React: Shallow vs. Static Testing
/
5Creating a Blogging App Using React, Part 3: Add & Display Post
/
16Creating a Blogging App Using React, Part 2: User Sign-Up
20 /
-6Creating a Blogging App Using React, Part 1: User Sign-In
/
99 More Popular HTML5 Projects for You to Use and Study
/
18Creating a Grocery List Manager Using Angular, Part 2: Managing Items
/
8Dynamic Page Templates in WordPress, Part 3
/
-1Angular vs. React: 7 Key Features Compared
/
5Creating a Grocery List Manager Using Angular, Part 1: Add & Display Items
/
3New eBooks Available for Subscribers in June 2017
/
7Create Interactive Charts Using Plotly.js, Part 1: Getting Started
/
11The 5 Best IDEs for WordPress Development (And Why)
/
133 Popular WordPress User Interface Elements
/
2New Course: How to Hack Your Own App
/
-10How to Install Yii on Windows or a Mac
/
5What Is a JavaScript Operator?
/
-5How to Register and Use Laravel Service Providers
/
8
waly Good blog post. I absolutely love this…