Waltir
By: Waltir

Cleaning Up Nightwatch JS Test Sessions

Cover Image for Cleaning Up Nightwatch JS Test Sessions

Nightwatch.js is a powerful end-to-end testing framework for web applications. One of the advantages of using Nightwatch.js is that it allows you to run tests in a clean environment, which is essential for accurate testing. However, after running tests, it's important to clean up the test sessions to ensure that the next test run starts in a clean environment. In this article, we'll describe how to clean up Nightwatch test sessions using deleteCookies and the --private chromeOptions arg.

Using deleteCookies

The deleteCookies function allows you to delete all cookies in the current browser session. This can be especially useful when you want to test your application in a clean environment. Here's an example of how to use deleteCookies:

module.exports = {
  'Test Cleaning Up Cookies': function(browser) {
    browser
      .url('http://www.google.com')
      .waitForElementVisible('body')
      .assert.elementPresent('body')
      .deleteCookies()
      .end();
  }
};

In this example, the url function is called with the parameter 'http://www.google.com', which tells Nightwatch to navigate to the Google homepage. The waitForElementVisible function is then called with the parameter 'body', which waits for the body element to be visible on the page. An assertion is then made to ensure that the body element is present on the page. Finally, the deleteCookies function is called, which deletes all cookies in the current browser session.

Using the --private chromeOptions arg

The --private chromeOptions arg allows you to start the browser in private browsing mode. In private browsing mode, the browser does not store any history, cache, or cookies. This can be useful when you want to test your application in a completely clean environment. Here's an example of how to use the --private chromeOptions arg:

module.exports = {
  'Test Cleaning Up Cookies': function(browser) {
    browser
      .url('http://www.google.com')
      .waitForElementVisible('body')
      .assert.elementPresent('body')
      .end();
  },

  beforeEach: function(browser) {
    browser.options.desiredCapabilities = {
      browserName: 'chrome',
      chromeOptions: {
        args: [
          '--private'
        ]
      }
    };
  }
};

In this example, the chromeOptions property is set in the beforeEach function, which is called before each test run. The --private arg is added to the args array, which starts the browser in private browsing mode. The url function is then called with the parameter 'http://www.google.com', which navigates to the Google homepage. The waitForElementVisible function is then called with the parameter 'body', which waits for the body element to be visible on the page. An assertion is then made to ensure that the body element is present on the page.

In conclusion, cleaning up Nightwatch test sessions is an important step in ensuring accurate testing. The deleteCookies function and the --private chromeOptions arg are two tools that can help you achieve this.

In order to ensure accurate and reliable testing, it's important to clean up Nightwatch test environments and sessions after each test run. In this article, we'll describe two methods for cleaning up Nightwatch test environments: using deleteCookies and the --private chromeOptions argument.

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...