首页 > 解决方案 > 守夜人 - 仍然无法切换到另一个窗口

问题描述

来自 github 的示例对我不起作用。

https://github.com/nightwatchjs/nightwatch/issues/369

这是我的代码。

When('I open a new browser window', () => {
    var host = 'http://www.google.com';
    client
        .windowHandle(function(wh){console.log(wh.value)})
        .url(host)
        .waitForElementVisible('#hplogo', 1000)
        .execute(function(newWindow){
            window.open('http://www.twitter.com', null, "height=1024,width=768");
        }, [host])
        .assert.urlContains('google')
        .window_handles(function(result) {
            var temp = result.value[1];
            this.switchWindow(temp);
        })
        .windowHandle(function(wh){console.log(wh.value)})
        .assert.urlContains('twitter')
        .end();
});

console.log 之前和之后都.switchWindow打印出相同的字符串。

请问有人有什么想法吗...?

编辑

考虑到pcalkins所说的,我对代码进行了一些更改。

这是现在的代码:

When('I open a new browser window', () => {
    var host = 'http://www.google.com';
    client
        .windowHandle(function(wh){console.log("\nBEFORE: " + wh.value)})
        .url(host)
        .waitForElementVisible('#hplogo', 1000)
        .execute(function(newWindow){
            window.open('http://www.twitter.com', null, "height=1024,width=768");
        }, [host])
        .pause(3000)
        .window_handles(function(result) {
            console.log("\n\nHANDLE: " + result.value + "\n");
            var temp = result.value[0];
            this.switchWindow(temp);
            console.log("\n\ntemp0: " + temp + "\n");
            temp = result.value[1];
            this.switchWindow(temp);
            console.log("\n\ntemp1: " + temp + "\n");
        })
        .pause(2000);
});

运行时,结果如下:

在此处输入图像描述

BEFORE 是原始窗口的句柄。

HANDLE 是两个窗口。

temp0 和 temp1 依次是两个不同的窗口。显然 temp1 是我想要的窗口,但 finalthis.switchWindow没有完成它的工作。

AFTER 是下一个测试步骤中的当前窗口句柄。

标签: seleniumnightwatch.jsnightwatch

解决方案


推荐阅读