1

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:

  1. Protractor - switch tabs error - different case
  2. https://github.com/angular/protractor/issues/55 - closed many years ago
  3. https://github.com/angular/protractor/issues/3124 - My new bug report)
Community
  • 1
  • 1
atoth
  • 838
  • 1
  • 9
  • 23
  • Even i had the issues with Protractor 3.0+, switching between tabs and navigating through iframes. In my case i could not found any other solution other than using sleep :( – Nick Apr 12 '16 at 22:38

1 Answers1

0

Visually, you will see it switch to the second tab, but you won't see it switch back to the first tab (maybe that's a bug in chrome or chromedriver?). The test commands are in fact sent to the correct tab though, and your test will pass or fail appropriately.

martin770
  • 1,271
  • 11
  • 15