Waltir
By: Waltir

Testing Duplicate Elements With Nightwatch JS

Cover Image for Testing Duplicate Elements With Nightwatch JS

When testing web pages with Nightwatch.js, it's important to ensure that there are no duplicate elements present on the page. Duplicate elements can cause confusion for users and can lead to unexpected behavior. In this article, we'll show you how to make assertions against duplicate elements on a web page using Nightwatch.js, and we'll provide some examples to help you get started.

Setup:

Before we can begin writing tests to check for duplicate elements, we'll need to create a new test file in our Nightwatch.js project. We can do this by creating a new JavaScript file in the tests directory of our project, and then adding the following code:

module.exports = {
  'Check for Duplicate Elements': function(browser) {
    // Write test code here
  }
}

This code creates a new test case called Check for Duplicate Elements, which we'll use to write our assertions.

Assertions:

To check for duplicate elements on a web page, we can use the elements command provided by Nightwatch.js. This command returns an array of matching elements on the page. We can then use the assert command to check the length of the array to ensure that there are no duplicate elements.

Here's an example test case that checks for duplicate div elements on a web page:

module.exports = {
  'Check for Duplicate Divs': function(browser) {
    browser.url('https://www.example.com')
      .elements('css selector', 'div', function(result) {
        browser.assert.equal(result.value.length, new Set(result.value).size, 'Expected no duplicate divs on the page');
      })
      .end();
  }
}

In this example, we're using the elements command to find all of the div elements on the page. We're then using a Set to remove any duplicates from the resulting array, and then comparing the length of the original array to the length of the Set. If the lengths are equal, then there are no duplicates on the page. If the lengths are different, then there are duplicate elements present.

Here's another example that checks for duplicate input elements:

module.exports = {
  'Check for Duplicate Inputs': function(browser) {
    browser.url('https://www.example.com')
      .elements('css selector', 'input', function(result) {
        browser.assert.equal(result.value.length, new Set(result.value).size, 'Expected no duplicate inputs on the page');
      })
      .end();
  }
}

In this example, we're using the elements command to find all of the input elements on the page. We're then using a Set to remove any duplicates from the resulting array, and then comparing the length of the original array to the length of the Set. If the lengths are equal, then there are no duplicates on the page. If the lengths are different, then there are duplicate elements present.

By using Nightwatch.js to check for duplicate elements on a web page, you can ensure that your pages are free of unexpected behavior that could confuse users. By regularly running these tests during your development process, you can catch any duplicate elements early on and fix them before they cause problems.

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