Nightwatch.js - How to do assertion on new browser tab
Asked 07 September, 2021
Viewed 1.9K times
  • 58
Votes

I have to test one scenario. User types desired URL to text which is linked with button. When user clicks this button, user will be directed to typed URL.

When I do assertion for new tab (Google), output is always like "localhost:8080" which is my previous tab (currentPage).

I think I should set new tab to "this" but, I could not figure it out. If you can help me, I will be appreciated.

linkTest.spec.js

module.exports = {
tags: ['component', 'linkTest'],
before: function(client, done) {
this.currentPage = client.maximizeWindow().page.linkTestPage();
this.currentPage
  .navigate(client.globals.devUrl)
  .waitForElementVisible('body', 60000)
  .customPerform(function() {
    done();
  });
 },

'CASE ID - Case Name'() {
let googleUrl = 'https://www.google.com/';

this.currentPage
  .checkInitialElements()
  .setExternalUrlText(googleUrl)
  .clickLinkLabel();

// Handle with new tab
this.client.windowHandles(function(result) {
  let googleTab = result.value[1]; // New Google tab index
  this.switchWindow(googleTab) // Switch to Google tab
    .assert.urlContains('Google'); // Verify new tab is Google
  });
},

after: function(client, done) {
client.end().customPerform(done);
 },
};

customPerform.js

  exports.command = function(fn, callback) {
  this.perform(fn);
  if (typeof callback === 'function') {
  callback.call(self);
  }
  return this; // allows the command to be chained.
 };

1 Answer