Nightwatch.js how to ignore main page? Or how to use window.location.host?
Asked 07 September, 2021
Viewed 3.1K times
  • 51
Votes

I'm a big noob in nightwatch.js but i have to decide this task. Please help me...

I have a simple autotest and the essence of the test is looking for links to the main page on each page of the site, and if there are less than 2 links then the test doesn't pass and gives an error.

module.exports = {
  disabled: false,
  name: 'Homepage links',
  subtitle: ['There are less than 2 links to the main page.'],
  fn: function(browser) {
    browser.elements('css selector', 'a[href="/"]', function(result) {
      let allLinksToMain = result.value.length;

      if (allLinksToMain < 2) {
        browser.verify.ok(false, 'There are less than 2 links to the main page.');
      }
    });
  }
};

Do you know how to force autotest to ignore the main page?

Or maybe you know how to use window.location.host and window.location.protocol with nightwatch.js? I ask it, because i have some idea how to decide my task with clear JavaScript, but it doesn't work with nightwatch.js:

let mainPageUrl = window.location.protocol + "//" + window.location.host + "/";
let currentUrl = window.location.href;

let findLinks = document.querySelectorAll('a[href="/"]');

if (currentUrl === mainPageUrl) {
    console.log('This is the main page, do nothing!');
} else {
    console.log('Current URL: ' + currentUrl);
    if (findLinks.length >= 2) {
        console.log("The test passed without errors, links to the main page - " + findLinks.length);
    } else {
        console.log("The test didn't pass, links to the main page - " + findLinks.length);
    }
}

1 Answer