有没有办法关闭 WebDriver 或 Protractor 中的选项卡? [英] Is there a way to close a tab in WebDriver or Protractor?

查看:33
本文介绍了有没有办法关闭 WebDriver 或 Protractor 中的选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过 Protractor 或 WebDriver 物理关闭选项卡?

Is there a way to physically close a tab via Protractor or WebDriver?

我问是因为虽然我知道如何以编程方式切换标签,但确实如此不要将活动选项卡置于前台.我不能总是知道在 SauceLabs 上运行的 E2E 测试中发生了什么,因为当我查看屏幕投射时,它显示的是我导航离开的选项卡,而不是活动选项卡.

I ask because while I know how to switch tabs programmatically, but it does not bring the active tab to the foreground. I can't always tell what is going on in my E2E tests that run on SauceLabs because when I view the screen casts it is showing the tab that I navigated away from, not the active one.

我的做法是否错误?

it('should do something in the previous tab', function(done) {
    browser.getAllWindowHandles().then(function (handles) {
        browser.switchTo().window(handles[0]);
        // do something
        expect(something).toEqual(thisThing);
        done();
    });
});

推荐答案

您可以尝试以下操作:

  1. 切换到新打开的标签页.
  2. 关闭当前窗口(在本例中为新选项卡).
  3. 切换回第一个窗口.

  1. Switch to the new opened tab.
  2. Close the current windows (in this case, the new tab).
  3. Switch back to the first window.

browser.getAllWindowHandles().then(function (handles) {
browser.driver.switchTo().window(handles[1]);
browser.driver.close();
browser.driver.switchTo().window(handles[0]);
});

这篇关于有没有办法关闭 WebDriver 或 Protractor 中的选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆