I have the following test with the current versions of webdriver/protractor (see title).
it('checks tabs', () => {
const url1 = 'https://stackoverflow.com/';
const url2 = 'http://programmers.stackexchange.com/';
let windowHandles = {
oldTab: '',
newTab: ''
};
await browser.get(url1);
await browser.getWindowHandle().then(handle => {
windowHandles.oldTab = handle;
});
await browser.executeScript('window.open("' + url2 + '", "whatever")');
await browser.getAllWindowHandles()
.then(handles => {
expect(handles[0]).toEqual(windowHandles.oldTab);
windowHandles.newTab = handles[1];
return browser.driver.switchTo().window(windowHandles.oldTab);
})
.then(() => {
let handle = browser.driver.getWindowHandle();
expect(handle).toEqual(windowHandles.oldTab);
})
.then(() => browser.sleep(6000));
});
The interesting thing is that the assertations work well; they are all green. But it does not switch back to the first tab.
Am I missing something or it is indeed a bug?
Update
In my FireFox window.open
opens a window, not a tab, and switching actually works between the windows.
I can accept the workaround of opening windows instead tabs in Chrome, though I really think that if the current window handle tells you that you have switched while you are still in the same is a bug.
Update 2
Even with opening windows Chrome
does not switch while FireFox
does. I reported a bug.
This is my new test:
it('checks tabs', async () => {
const url1 = '/login';
const url2 = config.chatLaunchUrl;
let windowHandles = {
oldTab: '',
newTab: ''
};
await browser.get(url1);
await browser.getWindowHandle().then(handle => {
windowHandles.oldTab = handle;
});
// opening new window sending CTRL+N
await browser.actions()
.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "n"))
.perform();
await browser.getAllWindowHandles()
.then(handles => {
expect(handles[0]).toEqual(windowHandles.oldTab);
windowHandles.newTab = handles[1];
return browser.driver.switchTo().window(windowHandles.newTab);
})
.then(() => {
// this works
return browser.get(url2);
})
.then(() => {
return browser.driver.switchTo()
.window(windowHandles.oldTab)
.then(() => browser.driver.executeScript('window.focus();'));
})
.then(() => {
let handle = browser.driver.getWindowHandle();
expect(handle).toEqual(windowHandles.oldTab);
})
.then(() => browser.sleep(6000));
});
Update 3
The difference between Chrome and Firefox is that if I switch to Firefox the browser window comes into focus while with Chrome it does not. The test can continue without problems in Chrome as well. So it's a lesser bug.
(Related links:
- Protractor - switch tabs error - different case
- https://github.com/angular/protractor/issues/55 - closed many years ago
- https://github.com/angular/protractor/issues/3124 - My new bug report)