Waltir
By: Waltir

Using .env (Environment) Variables In Nightwatch JS

Cover Image for Using .env (Environment) Variables In Nightwatch JS

Nightwatch.js is a powerful testing framework that can be used to automate browser testing, and it can also be used to test both mobile and desktop web applications. In this article, we will explore how to use environment variables inside Nightwatch tests to test a mobile and desktop application.

Before we begin, it's important to note that you will need to have Nightwatch installed on your system. You can do this by running the following command:

npm install nightwatch

Environment variables are a convenient way to store configuration values that can be accessed by your tests. In Nightwatch, you can use environment variables to configure the viewport size or device type for your tests.

For example, you can set an environment variable called MOBILE to true or false to determine whether your tests should be run on a mobile or desktop device. Here's an example of how you might set this environment variable on a Linux or macOS system:

export MOBILE=true

You can then use this environment variable in your Nightwatch tests to configure the viewport size or device type.

Here's an example of how you might use the environment variable to test a mobile web application:

module.exports = {
  'Test mobile web application': function (browser) {
    var mobile = process.env.MOBILE;

    browser
      .url('http://example.com')
      if (mobile === 'true') {
        browser.setViewportSize({width: 320, height: 480});
      } else {
        browser.resizeWindow(1920, 1080);
      }
      .assert.elementPresent('.menu')
      .end();
  }
};

This test will check the value of the MOBILE environment variable, if it's true it will set the viewport size to 320x480 pixels, which simulates a mobile device, if it's false it will set the size of the browser window to 1920x1080 pixels, which simulates a desktop device, and assert that the menu element is present on the page.

You can also use the process.env object to access other environment variables, such as DEVICE, URL, or API_KEY, and use them in your tests.

It's important to keep in mind that you should use environment variables to store sensitive data like API keys, or other credentials that you don't want to hard code into your tests.

Environment variables can be a useful tool for configuring your Nightwatch tests. By using environment variables, you can easily switch between testing a mobile or desktop application, or configure other aspects of your tests without having to change the test code. This makes it easier to maintain your tests and make changes to your test environment without having to update your test code.

More Posts

Cover Image for Blocking Ad Traffic In Nightwatch JS
Blocking Ad Traffic In Nightwatch JS
Waltir
By: Waltir

Example showing how you can block unwanted ad traffic in your Nightwatch JS tests....

Cover Image for Blocking Ad Traffic In Cypress
Blocking Ad Traffic In Cypress
Waltir
By: Waltir

Example showing how you can block unwanted ad traffic in your Cypress tests....

Cover Image for Three Ways To Resize The Browser In Nightwatch
Three Ways To Resize The Browser In Nightwatch
Waltir
By: Waltir

Outlining the three different ways to resize the browser in Nightwatch JS with examples....

Cover Image for Happy Path VS Sad Path Testing
Happy Path VS Sad Path Testing
Waltir
By: Waltir

As a test engineer it is crucial that both happy path and sad path use cases have been considered and fully tested...