javascript 量角器切换到上一个选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26798020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:30:24  来源:igfitidea点击:

protractor switch to previous tab

javascriptangularjsselenium-webdriverprotractor

提问by Ana

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

采纳答案by Jmr

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.

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

回答by Tim Visser

@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>:

@Aaron 此代码获取所有可用的浏览器句柄并解决承诺。然后打开由 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!

希望这可以帮助!