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.
(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.)
Also, before you start learning up any new skill or concept, I suggest you take a look at my course “10 Steps to Learn Anything Quickly”.
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.
(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.
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.
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:
Then, execute the code by running:
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
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
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:
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:
If the test executes too quickly, you can comment out the driver.quit to keep the browser window open.
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.