Javascript 量角器切换到上一个选项卡
打开一个新选项卡(第二个)后,我尝试切换到第一个选项卡Javascript 量角器切换到上一个选项卡,javascript,angularjs,selenium-webdriver,protractor,Javascript,Angularjs,Selenium Webdriver,Protractor,打开一个新选项卡(第二个)后,我尝试切换到第一个选项卡 common.clickOpenNewSession(); //it opens the new tab browser.getAllWindowHandles().then(function (handles) { var secondWindowHandle = handles[1]; var firstWindowHandle = handles[0]; browser.switchTo().wi
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');
});
问题是,您试图使用ctrl+tab转到上一个选项卡,而不是使用窗口句柄切换回上一个选项卡。这在WebDriver中不受支持,因为使用ctrl+tab是一种系统操作,不能在浏览器上对所有操作系统/浏览器进行模拟。只需再次使用
browser.switchTo()
。@Aaron此代码将获得所有可用的浏览器句柄,并解析承诺。然后打开由
创建的选项卡:
同样,您可以在选项卡之间切换:
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]);
});
});
希望这有帮助 我也有同样的问题,我尝试了不同的方法,用browser.switchTo().window(tab1)将其封装在promise中。。不成功..是否可以通过量角器关闭选项卡?
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]);
});
});