【问题标题】:protractor switch to previous tab量角器切换到上一个选项卡
【发布时间】:2014-11-07 09:27:19
【问题描述】:

打开一个新标签(第二个)后,我试图切换到第一个标签。

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

【问题讨论】:

  • 我有同样的问题,我尝试了不同的方法将其封装在一个承诺中,使用 browser.switchTo().window(tab1) .. 没有成功..

标签: javascript angularjs selenium-webdriver protractor


【解决方案1】:

问题是您尝试使用 ctrl + tab 转到上一个选项卡,而不是使用窗口句柄切换回来。这在 WebDriver 中不受支持,因为使用 ctrl + tab 是一项系统操作,并且不能在您的浏览器上为所有操作系统/浏览器模拟。只需再次使用browser.switchTo()

【讨论】:

  • 是否可以通过量角器关闭标签页?
【解决方案2】:

@Aaron 此代码获取所有可用的浏览器句柄并解析承诺。然后打开<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://...");
        });
    });
});

以同样的方式,您可以在标签之间切换:

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

当然,关闭一个标签页:

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多