在Nightwatch页面对象中使用windowHandles

问题描述 投票:0回答:1

我正在使用Nightwatch JS页面对象来创建测试。单击打开新选项卡的链接时,我无法将焦点切换到新选项卡。

其他任何搜索都没有在页面对象中使用windowHandles。

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不是函数

javascript function nightwatch.js
1个回答
0
投票

据我所知,您遇到的问题是.windowHandles命令需要this.api而不是this,并且由于它是在.waitForElementVisible回调函数中调用的,因此它继承了[ C0]。因此,您可以尝试的一件事是将this命令放在.windowHandles回调函数中,如下所示:

.click

[我也建议像我展示的那样,将针对新页面运行的命令移至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'); }); }) }) } }; 回调中,因为您可能会在尝试引用原始窗口句柄时遇到问题。

© www.soinside.com 2019 - 2024. All rights reserved.