Configure test cases according to the different environments in NightwatchJS

Configure test cases according to the different environments in NightwatchJS

10 March 2022

Selenium is a mostly used tool for browser testing. Being an open-source framework allowed Selenium to be compatible with multiple test automation frameworks for programming languages and if we talk about Automation testing with Selenium and JavaScript, there is a framework that never fails to take the spotlight and that is the Nightwatch.js.

In this blog, I am not going to talk details about the nightwatch js but will cover the important part that is the configuration of environment variables and global variables in the nightwatch js.

Before going forward I want to explain some basics about nightwatch so that everyone should be clear about what is the nightwatch and why nightwatch is so popular.

What Is Nightwatch.js?

Nightwatch.js is an open-source automation testing framework which is providing complete E2E (end to end) solutions to automate testing with Selenium Javascript for web-based applications, browser applications, and websites. The Nightwatch JS framework mostly uses Selenium and provides several commands and assertions within the Nightwatch JS framework to perform operations on the DOM elements. It internally uses the powerful W3C WebDriver API or the Selenium WebDriver and simplifies writing end to end automated tests in Node.js and effortlessly sets up for Continuous Integration.

Why is Nightwatch.js So Popular?

Before we go deep into executing test scripts in this Nightwatch.js tutorial for beginners. It is important to understand the reasons for the popularity of Nightwatch.js. Nightwatch.js provides an end to end functional browser testing in a pure node.js environment which enables testing of web applications independent from third party software. The main purpose of lightweight and robust automated testing frameworks such as Nightwatch.js is to enable a single integrated solution for application testing. Since Nightwatch JS is built on Node.js, it has some major advantages over any other testing tools. This automated browser testing powered by Nightwatch.js eliminates the dependency upon third party software consequently enhancing data integrity among diverse system components.

Nightwatch JS provides the following features:

  • In Built Test Runner: It provides an inbuilt command-line test runner suite with Grunt support for executing the automated tests.
  • Test Strategy: It has the feature to execute the tests efficiently and can be performed in many ways such as parallelly, sequentially or in groups, and tags.
  • Cloud Services: it provides good support for Cross Browser Testing with Selenium JavaScript by providing integration with the cloud-based Selenium testing providers like LambdaTest.
  • Selenium Server: It has the ability to automatically control the standalone Selenium server with JUnit XML reporting built in.
  • Assertions, CSS, and XPath: It provides Several commands and assertions for DOM operations, CSS and XPath selectors and can be used to identify the elements on the page. This makes the Nightwatch JS framework flexible and easy to extend especially while implementing the application-specific commands and assertions.
  • Continuous Integration: It offers good support for Continuous Integration and hence can be used to integrate the tests with the continuous build processing systems such as Jenkins, TeamCity, etc. and assist developers in building and testing software continuously.

Apart from all the above features that it provides, it is popular for having a clean and easy syntax, making it easier to write the tests efficiently and quickly by only making use of Node.js CSS selectors and XPath locator in Selenium.

Coding Standards

Everyone knows the way of writing code will decide the code stability and code performance. For the automation we have clear about following points

  • Elements or selectors should be stored in the separate file (POM is providing the same.)
  • Constants also should be located in the separate file.
  • Global variables should be placed in the configuration file.
  • Global variables should be added according to the environment.

Now let’s move to the main part of the environment setup.

Environment SetUp

All configuration related settings are available in the nightwatch.config.js file. There are the multiple option like

  • Add test path
  • Add page object path.
  • Custom commands path and custom assertion path and all.
  • Global path
    • In the global file you can add global variables so access in the test cases.
  • Test Setting
    • This is the main file where you can add all the environment variables setup.
  • Browserstack
    • This is the most important feature provided by nightwatch js. You can add your browser stack credentials and then your test cases will run on browser stack.
    • There are options like you have to run on chrome or there platform.
  • test_settings
    • In the test settings you can add json according to your environments.
    • Here you can add your driver setup.
    • In the test setting we can add json according to the different environments and naming conventions should be according to the environments.
    • You also need to configure screenshots settings there according to your environments.
    • We also need to configure global settings here.

Configure-test-cases-according-to-the-different-environments-in-NightwatchJS-image1

Request a quote