Getting started with automation testing and Nightwatch.js

Sabita Neupane
Devnetwork
Published in
4 min readDec 14, 2018

As we all known software development is very tricky thing and everyday it requires new feature to be added. And whenever any of the feature need to be added or enhanced into the software, then it actually add more complexity to the code which in return cause bugs or sometime it will not work. Due to this reason nowadays most of the tech companies employs huge numbers of QA. QA also oversees the entire development process, which includes software testing, from start to finish. Though there are numerous technique for quality assurance but the typically used technique are manual and automation testing.

When I decided I wanted to work as Quality Assurance I only knew about the manual testing. While working as QA, I found manual approach as the best method for testing the functionality as human can use their judgement and intuition. But nowadays manual testing is not what it used to be, as many manual testers are now transitioning into automation testing. Manual testing requires repetitive regression test even just for small change. So some of the people started eliminating this problem by automating their work.

Automation testing shorten development cycle, helps to avoid cumbersome repetitive tasks, and improve software quality.

Why automated testing

The benefit of automated testing is associated with how many times a given test can be repeated. Tests that needs to be performed few times, or it require human intuition then they are left for manual testing. Good test cases for automation can be those that needs to be run frequently, have many steps, or test with large amounts of data. However, there are some scenarios where manual testing is more preferable. For example, manually test can be more suitable during usability testing. These tests examines user experience where human observation is the most. But in other testing scenarios automated testing is preferable like when you are performing regression tests because the code changes frequently and your team can run regressions quickly by automating.

Automation testing in agile development method

Nowadays many of the tech company are following the agile development methodology. So, one of the main reason for the transitioning from manual to automation is the recent adaptation to Agile development method. In Agile method, development and testing can be done in several iterations where it also includes the concepts of Continuous Integration and Continuous Development. And in agile it is necessary for fast deployment, so testing methods also need to be faster. And automation testing can solve this problem of fast testing methods. There are lots of tools available for automation testing process like Selenium, TestNG, Cucumber, Test Studio, Nightwatch etc.

Getting started with Nightwatch.js

And after huge research on Automation testing, I found Nightwatch really helpful for e2e automation testing which examines the real-world scenarios of an application from start to finish, simulating the real user scenario and validating the system. Nightwatch.js enables us to “write end-to-end tests in Node.js quickly and effortlessly that run against a Selenium/WebDriver server”.

Actually, Nightwatch.js is UI automated testing framework powered by Node.js and uses the Selenium WebDriver API.

Step 1: Installing Selenium standalone server.

Nightwatch works with the Selenium standalone server so at first we need to download the selenium standalone server.

Prerequisite: make sure you have JDK installed.

$ npm install selenium-standalone@latest -g

To run selenium standalone server

$ selenium-standalone start

Step 2: Installing Chrome driver / geckodriver

We want to run our test on Chrome, Safari, and Firefox, so we must also install their respective drivers and this driver will be used by Selenium to control the browsers.

$ brew install geckodriver

or

$ npm install chromedriver --save-dev

Step 3: Installing Nightwatch

Nightwatch.js is an easier way to write tests that running them using Selenium. To install it, cd into your project, and then install the module from npm to your dev-dependencies:

 $ git clone https://github.com/nightwatchjs/nightwatch.git
$ cd nightwatch
$ npm install

Step 4: Adding Configuration file into nightwatch project

The test runner of expects a configuration file to be passed and the basic Nightwatch configuration happens through a json configuration file — nightwatch.json. Let’s create a nightwatch.json file, and fill it with:

{
“src_folders” : [“tests”],
“output_folder” : “reports”,

“selenium” : {
“start_process” : true,
“server_path” : “./bin/selenium-server-standalone-3.3.1.jar”,
“log_path” : “”,
“port” : 4444,
“cli_args” : {
“webdriver.chrome.driver” : “./bin/chromedriver”
}
},
“test_settings” : {
“default” : {
“launch_url” : “http://localhost",
“selenium_port” : 4444,
“selenium_host” : “localhost”,
“desiredCapabilities”: {
“browserName”: “chrome”,
“javascriptEnabled”: true,
“acceptSslCerts”: true
}
}
}
}

Step 5: Writing test

We have now installed Nightwatch, standalone Selenium server, and also driver for browsers. With all of these steps, now we are ready with all the tools to create end-to-end tests.

Let’s add a new file in the tests folder, called main.js.

module.exports = {
'Searching nightwatch in youtube': function (browser) {
browser
.url("http://www.youtube.com/")
.pause(2000)
.setValue('#search' , "Nightwatch js")
.pause(2000)
.keys(browser.Keys.ENTER)
.pause(2000)
},
after: function (browser) {
browser
.end();
}
};

Step 6: Running the test

To run your test, in your terminal do the command which will run the test file main.js.

$ nightwatch -t tests/main.js

And this is how I get started with Nightwatch.js and I really found Nightwatch as an easy tool to use.

You can follow the repo nightwatchjs-example for Nightwatch.js

--

--