Remove the frustration of writing automation using nightwatch.js
Writing automation as part of your development process is a pretty difficult task. Let’s face it, as developers writing tests is hard work and takes almost as much time as writing the feature itself.
At Bizzabo we used to have Java selenium automation tests. When they broke due to a bug or a UI change, someone needed to handle it, but it’s Java, which is a less familiar and less attractive area for a Front End developer. Furthermore, it takes time to build, run and see what went wrong compared to what a FED developer is used to.
This happened pretty often until we reached a situation where the tests broke too frequently and we could no longer keep tabs and maintain it. Ultimately, it just died.
Tools like selenium in Java, or Watir in ruby, are good tools and are well used by R&D teams all around, but for us, the Front End developers find it to be less convenient. We develop web applications using JS Libraries and node.js, plus grunt and gulp are our configuration tools. See the resemblance? That’s right, they are all tools in javascript, our comfort zone.
This is where nightwatch.js comes in. Nightwatch is an automated testing framework for web applications. It’s based on nodejs and uses selenium’s webdriver and can run on various browsers and browserstack
Long story short, if you feel that “winter is coming” (I mean, the library is called nightwatch after all…) and you want to give the automation tests another go and use nightwatch.js as your automation testing framework, you’re in the right place. Let’s follow a quick tutorial:
setting up your nightwatch testing project:
- create a project structure (this is how we did it, entirely your choice)
download selenium from selenium website
2. run the following code:
3. Nightwatch expects a configuration file to be passed, using by default a nightwatch.json file from the current directory. Edit the nightwatch.json file as displayed to enable tests on firefox :
We decided to separate the code of the page objects and element queries with test code itself. This allows the test flow to be very clear for every developer. The page objects are a way to abstract your interactions with the web page by exporting the Javascript API for element interactions.
An example of a test in a login screen:
Below is a page object with queries on the page itself:
and here’s the test itself:
And that’s it! You have your first automation tests that log-in to a page.
Writing tests became simpler for us at Bizzabo thanks to nightwatch as I’m sure it will be for you. We also found that our tests work best when they are more basic and straightforward Stick with simple flows of your product rather than long and complicated flows that tend to break easily.
In addition, here’s a quick tip: start with most common and strategic areas in your product to provide the best and quickest safety-net for your R&D to continuously test and release to production.
While we’re already talking about nightwatch.js, in the words of Jaime Lannister:
“Give my regards to the Night’s Watch. I’m sure it will be thrilling. And if it’s not, it’s only for life.”