Selenium With Node.js and Mocha: Automated Testing With JavaScript

By John Sonmez

I’ve always found it a bit strange that we’ve had to use a language like C# or Java to write automated tests using Selenium.

Not that I dislike either of these languages, but—especially in modern JavaScript times—it is a little bit strange to write a web application in primarily JavaScript and then write the automated test for it using C#, Java, Ruby or Python.

That is why I was pretty excited to see that there were Selenium bindings for JavaScript that would allow you to write your automated tests in JavaScript and run them from Node. You can finally use Selenium with Node.

In this post I am going to take you through the process of getting setup and running a simple JavaScript based test using Selenium with Node.js and Mocha.

(The instructions here are for Windows, but you could easily follow most of this post if you are planning on running your tests on Mac or Linux—in fact, it should be much easier.)

Installing Node.js

Before we can get started, we first need to get Node.js installed.

Just head over to the Node.js site’s download page to find downloads for Node.js for each platform.

2014 01 31 16 05 04 thumb Selenium With Node.js and Mocha: Automated Testing With JavaScript

Just select the appropriate version and install.  You’ll need Node.js to run the JavaScript tests because you need some way to execute your JavaScript locally.

(If you are interested in learning more about Node.js, I recommend the excellent Node.js in Action book.)

Getting Selenium with Node working

Once we have Node installed, we need to get Selenium webdriver so that we can actually have a way to automate a browser.

Not so fast though, before we can get that part working, we’ll need to make sure we have the latest chrome driver.  Go download the Chrome driver first and put it in your PATH.

2014 01 31 16 17 01 thumb Selenium With Node.js and Mocha: Automated Testing With JavaScript

2014 01 31 16 18 13 thumb Selenium With Node.js and Mocha: Automated Testing With JavaScript

Now, we are ready to get the Selenium webdriver bindings that we’ll use to automate the browser from JavaScript.  The bindings will just expose the Selenium API through JavaScript, which we can access by importing a module in Node.

Go to the location where you are going to create your test project and either open up a command line or terminal window there.

We’ll use Node’s built-in package manager, or NPM, to install the Selenium webdriver package.

Just run “npm install selenium-webdriver” to install Selenium webdriver in the current project directory.

2014 01 31 16 24 07 thumb Selenium With Node.js and Mocha: Automated Testing With JavaScript

Let’s do a quick test to make sure everything is working so far.

Create a file named first_test.js in your project with the following code:

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('simple programmer');
driver.findElement(webdriver.By.name('btnG')).click();
driver.quit();

Then, execute the code by running:

node first_test.js

You should see the Chrome browser popup and it should search for “Simple Programmer”, then quit.  (If it goes too fast, take out the driver.quit line.)

(Also, a good book in Selenium is Selenium Testing Tools Cookbook.  You can also check out my courses on Pluralsight.)

Adding some Mocha

Mocha is a test framework commonly used to write unit tests for JavaScript code, but we can also use it as a driver to drive our Selenium tests when using Selenium with Node.

We can get Mocha using the Node package manager as well, but for Mocha we want to use the –g option which will install the package globally instead of just locally to our project.  This will allow us to run Mocha on Windows without using Cygwin.

Go ahead and install Mocha by typing:

npm install –g mocha

2014 01 31 20 55 35 thumb Selenium With Node.js and Mocha: Automated Testing With JavaScript

Writing your first test

Now we are ready to write our first test using Mocha.

Create a new file in your directory called mocha_test.js with the following contents:

var assert = require('assert'),
test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver');

test.describe('Google Search', function() {
  test.it('should work', function() {
    var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();
driver.get('http://www.google.com');
    var searchBox = driver.findElement(webdriver.By.name('q'));
    searchBox.sendKeys('simple programmer');
    searchBox.getAttribute('value').then(function(value) {
      assert.equal(value, 'simple programmer');
    });
	driver.quit();
  });
});

This simple test will test that we can type “simple programmer” into Google’s search box and verify that the text is there.

A pretty simple test, but it demonstrates the basic skeleton of creating an automated test with Mocha using Selenium with Node.js.

You can execute the test by typing:

mocha mocha_test.js

If the test executes too quickly, you can comment out the driver.quit to keep the browser window open.

2014 01 31 21 03 11 thumb Selenium With Node.js and Mocha: Automated Testing With JavaScript

Going further…

Well, that should get you started with using JavaScript and Node to run a Selenium test driven by Mocha.

You can use this basic setup to create much more robust automated tests.  I call these kinds of tests blackbox automated tests or BATs(Check out my series if you want to learn more about creating a automated testing framework.)

And if you liked this post, go ahead and sign up here to make sure you don’t miss other content like this.