Automated functional testing with Javascript using Mocha and Selenium [Part 1]

Over the past few years, I’ve become a firm believer in the value of automated testing, to such a point that I would never contemplate starting a serious project without at least some form of automated testing built in (be it unit testing, or functional testing). Like most, I’d say that my passion for automated testing has not developed from a sheer love of writing tests (although – if that’s you then you are a lucky person!) but rather because of a number of other things – ie. I’m passionate in my dislike of having to test things manually; passionate in my desire to deliver a quality product; and passionate about being able to refactor code without that fear of unknowingly breaking the entire build.

Fortunately for me, my interest in automated testing (fuelled by a automated testing pioneer colleague of mine) arose at the same time as feasible testing tools – such as JUnit for unit testing, and Selenium and Watir for functional testing – started to gain some serious traction, particularly in the J2EE world.

The moment you realise you broke the build

Given that I was then working as a contractor on a large J2EE web application, this allowed for me to really see the how impressive the benefits of automated testing really are (which, and I won’t go into detail here, have serious benefits to the bottom line of a project, robustness of the product, development time and general developer happiness). So much so, that when I moved full time into the world of Javascript at the start of this year I started looking to see what was on offer in this cutting edge new space.

In short, what I found was this – Javascript (particularly Node.js) has a lot of support for unit testing, such as the exceptional Mocha testing framework, but little support for functional testing. While this wasn’t a major issue given the fact that functional testing occurs in the browser, and so it’s just as easy to write tests in Ruby (or C#/Java/insert supported language here) and still get the same outcome. However, call me old fashioned, but if I’m writing a server side application in Javascript, with a client side Javascript application talking to it, being tested by Javascript unit tests – then it stands to reason that my functional tests should be Javascript as well – so imagine my delight when I found WebDriverJS, which provides a Javascript driver for Selenium. More to the point, after a bit of experimentation it became obvious that the WebDriverJS could be easily worked in with Mocha to provide a nice, easy to use (and pretty!) functional test suite.

So over the course of the next couple of days, I’ll be posting a few articles detailing some of the things that I’ve discovered while investigating the functional testing possibilities with Selenium and Javascript through the creation of a sample project. If you’ve had any experience using Selenium (or Watir) with Javascript, or have come across any great resources, then I’d love for you to leave a comment below.

For those interested in having a dig around while waiting, here are some of the resources that I’ve been using:

If this article has interested you, you may wish to follow me on Twitter (@noehlman), or you may wish to browse the random collections of code on my GitHub repository.

About these ads

2 thoughts on “Automated functional testing with Javascript using Mocha and Selenium [Part 1]

  1. Pingback: An Aside: Asteroid Mining? I want in! | Unexpected Literal

  2. Pingback: Automated functional testing with Javascript using Mocha and Selenium [Part 2] | Unexpected Literal

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s