Michele
Michele

Reputation: 1

Use of windowHandles within Nightwatch Page Objects

I am using Nightwatch JS page objects to create my tests. When clicking on a link that opens a new tab, I cannot switch focus to the new tab.

Any other searches haven't used windowHandles within the page objects.

var verifyHomepage = {
    verifyPrivacy: function () {
        return this.waitForElementVisible('body', 20000)
            .click('@privacyLink')
            .windowHandles(function (result) {
                var handle = result.value[1];
                this.switchWindow(handle);
            })
        .waitForElementVisible('body', 20000)
        .assert.urlContains('/regression/en-us/PrivacyStatement')
        .assert.containsText('@privacyHeader', 'Privacy Statement');
    }
};


module.exports = {
    commands: [verifyHomepage],
    url: function () {
        return this.api.launchUrl;
    },

    elements: {
        // Fill in the empty quotes with your selectors
        header: '#customBlock > div > h1',
        login: '#loginBtn',
        homeLogo: '#siteLogo',
        footerLogo: '#siteLogoFooter',
        homeLink: '#footerBtnHome > a',
        privacyLink: '#footerPrivacyStatment > a',
        privacyHeader: '#mainBlock > div > h1'
    }
};

this.waitForElementVisible(...).click(...).windowHandles is not a function

Upvotes: 0

Views: 804

Answers (1)

lunar_recon
lunar_recon

Reputation: 31

From what I can tell, the issue you're running into is that the .windowHandles command requires the this.api instead of just this, and since it's being called within the .waitForElementVisible callback function, it inherits this. So, one thing you could try is to put the .windowHandles command inside of a .click callback function like so:

var verifyHomepage = {
    verifyPrivacy: function () {
        return this.waitForElementVisible('body', 20000)
            .click('@privacyLink', function () {
                this.api.windowHandles(function (result) {
                var handle = result.value[1];
                this.switchWindow(handle, function () {
                    this.waitForElementVisible('body', 20000)
                    .assert.urlContains('/regression/en-us/PrivacyStatement')
                    .assert.containsText('@privacyHeader', 'Privacy Statement');
               });
            })
        })  
    }
};

I also recommend moving the commands running against the new page into the .switchWindow callback like I show, since you may run into issues with them trying to reference the original window handle otherwise.

Upvotes: 1

Related Questions