最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - protractor switch to previous tab - Stack Overflow

programmeradmin3浏览0评论

After opening a new tab (second) I'm trying to switch to the first tab.

     common.clickOpenNewSession(); //it opens the new tab

 browser.getAllWindowHandles().then(function (handles) {
    var secondWindowHandle = handles[1];
    var firstWindowHandle = handles[0];
    browser.switchTo().window(secondWindowHandle).then(function () { //the focus moves on new tab
        browser.sleep(3000);
        expect(browser.driver.getCurrentUrl()).toContain("url");
//do some actions

    });
//below it doesn't work. I try to go back on previous tab without closing the second tab
    browser.actions().sendKeys(protractor.Key.CONTROL).sendKeys(protractor.Key.TAB).perform();
    browser.sleep(4000);
    browser.driver.switchTo().window(firstWindowHandle);
    browser.setLocation('');
});

After opening a new tab (second) I'm trying to switch to the first tab.

     common.clickOpenNewSession(); //it opens the new tab

 browser.getAllWindowHandles().then(function (handles) {
    var secondWindowHandle = handles[1];
    var firstWindowHandle = handles[0];
    browser.switchTo().window(secondWindowHandle).then(function () { //the focus moves on new tab
        browser.sleep(3000);
        expect(browser.driver.getCurrentUrl()).toContain("url");
//do some actions

    });
//below it doesn't work. I try to go back on previous tab without closing the second tab
    browser.actions().sendKeys(protractor.Key.CONTROL).sendKeys(protractor.Key.TAB).perform();
    browser.sleep(4000);
    browser.driver.switchTo().window(firstWindowHandle);
    browser.setLocation('http://google.com');
});
Share Improve this question asked Nov 7, 2014 at 9:27 AnaAna 1031 gold badge1 silver badge5 bronze badges 1
  • i have the same problem , i tried different approach as encapsulate this in a promise, used browser.switchTo().window(tab1) .. no success .. – Teodor Commented Nov 7, 2014 at 9:38
Add a comment  | 

2 Answers 2

Reset to default 9

The problem is that you're trying to go to the previous tab by using ctrl + tab, instead of using the window handles to switch back. This is not supported in WebDriver, because using ctrl + tab is a system operation, and can't be emulated on your browser for all OS/browsers. Just use browser.switchTo() again.

@Aaron This code get all the browser handles available and resolves the promise. Then opens the tab created by a <a href='newlink' target='_blank'>Link</a>:

POFile.buttonWithABlankTarget.click().then(function() {
    browser.getAllWindowHandles().then(function(handles) {
        var newTabHandle = handles[1];
        browser.switchTo().window(newTabHandle).then(function () {
            // Expect the newly opened tab URL to equal the href of the invitation
            expect(browser.driver.getCurrentUrl()).toContain("http://...");
        });
    });
});

In the same fashion, you could switch between tabs:

it("should open the first tab", function() {
    browser.getAllWindowHandles().then(function (handles) {
        browser.switchTo().window(handles[0]);
    });
});

And, of course, close a tab:

it("should close the second tab and return to the first tab", function() {
    browser.getAllWindowHandles().then(function (handles) {
        // We currently are on the second tab...
        browser.driver.close();
        browser.switchTo().window(handles[0]);
    });
});

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论