Tuesday, September 26, 2017

Using Custom Commands to simplify process in NightwatchJS

Nightwatch js is a package which is used to test Web UI in browsers using selenium and javascript. Today i will tell you about a feature in nightwatchjs which is Custom commands which can be used to inject functionality to a method manually using dependency injection.

First we need to have it installed in the system by using the below command -

  • sudo npm install --save nightwatch
This will install the package locally. We also have to remember that node must be installed in the system to run the command node package manager i.e. npm. We need to then run the selenium server at the background. Download the jar and run the jar as a background process -
  • java -jar selenium-server-standalone-3.4.0.jar
It will also need chromedriver or geckodriver to be in system path to run them successfully.

A nightwatchjs requirement is that it should have a config file which is a conf.js or a json file that links all the resources to run the tests. Then it should have a tests folder where all the tests are suppose to reside. Something like below picture - 

Now after writing the tests one has to just run node 
  • nightwatch tests 
As we are keeping the test steps inside tests folder, on calling tests it will run all the tests inside the tests directory.

Now to come to custom commands we need to create folder called custom commands and keep the code inside which we want to run as a dependency for other tests to run something like below picture,


Now we have to link the custom command in the config file some thing like below picture,



Finally we call the login custom command inside each tests to perform the step as a prestep.


P.S. This feature is same like BeforeEach functions in other testing frameworks.

No comments: