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

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:

[sourcecode language=”javascript” padlinenumbers=”true”] var webdriver = require(‘selenium-webdriver’);

var driver = new webdriver.Builder().

driver.findElement(webdriver.By.name(‘q’)).sendKeys(‘simple programmer’);

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:

[sourcecode language=”javascript” padlinenumbers=”true”] 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().
var searchBox = driver.findElement(webdriver.By.name(‘q’));
searchBox.sendKeys(‘simple programmer’);
searchBox.getAttribute(‘value’).then(function(value) {
assert.equal(value, ‘simple programmer’);

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.

  • Pingback: Dew Drop – February 4, 2014 (#1716) | Morning Dew()

  • Pingback: Selenium With Node.js and Mocha: Automated Test...()

  • Rohit Pagariya

    Thanks for the tutorial. I am trying to run the above tests in IE but can’t get them to work. I have searched online for hours and I am no where close to a solution. Here’s what I did so far:
    1. Downloaded IEDriverServer.exe and Selenium-server-standalone-xxxx.jar and added them to the PATH
    2. Tried to run the tests in firefox and they don’t work either.

    Were you able to run these tests in IE? If yes, could post an update to the article?

    • jsonmez

      I haven’t run them in IE, I was using Chrome and Firefox.

      • praneeth


        Just wondering whats the advantage of this framework and how it makes different from other frame works?


  • hungsu

    This is a really great tutorial, I’m starting to work with Selenium on my own and just getting started is incredibly hard. This gets anyone going in minutes :D

    Small thing, I had to restart my computer before changes to my PATH seemed to take effect.

  • sridhar

    How do i automate select list

  • Mary Ann May-Pumphrey

    I cannot get the instructions above to work, even with chrome. Here’s a snippet of what I’m seeing:

    localhost:selenium-with-node-js mamp$ node first_test.js


    throw error;


    Error: Timed out waiting for the WebDriver server at http://192….

    John: Any ideas on this?

    • jsonmez

      It seems there might be an issue with Chrome driver not being found. You might try Firefox instead, then you shouldn’t need a separate driver.

  • Mayank Dabhi

    I have installed NODE. You said I need put chrome driver in my path. But I am not getting which path you talking about. ????

  • Ben Pringle

    Error: timeout of 0ms exceeded

    • Jim Liu

      Same here.

  • Pingback: Web Automation Testing | Capital Engineering()

  • Jemilo II

    Awesome, actually working with selenium and mocha John. I googled it and when I saw simple programmer, I knew I didn’t have to search more thanks!