0

The example from github doesn't work for me.

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

This is my code.

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();
});

Both console.log before and after .switchWindow print out the same string.

Does anyone have any ideas please...?

EDIT

I've changed the code a bit taking into account what pcalkins said.

This is the code now:

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);
});

When run, this is the result:

enter image description here

BEFORE is the handle for the original window.

HANDLE is both windows.

temp0 and temp1 are the two different windows in sequence. Clearly temp1 is the window I want, and yet the final this.switchWindow is not doing its job.

AFTER is the current window handle at the next test step.

Alichino
  • 1,668
  • 2
  • 16
  • 25
  • the order of the window handles array is not guaranteed. (it'll vary by driver/browser) Check current handle by using getCurrentWindowHandle method of driver. The new window will be the handle in the getWindowHandles array that is not that one. – pcalkins Jun 12 '20 at 17:20
  • I've edited my question with new code. Why does it still not work? – Alichino Jun 15 '20 at 07:20
  • Interesting, even if you put a random string in switchWindow, no error is thrown. It's like it completely ignores switchWindow. – Alichino Jun 15 '20 at 10:48

0 Answers0