【问题标题】:Webdriverio - How to detect a browser window being closedWebdriverio - 如何检测正在关闭的浏览器窗口
【发布时间】:2017-01-09 16:54:42
【问题描述】:

我正在使用 webdriverio 和 mocha 进行自动化测试。如果在执行我的自动化测试时关闭了浏览器窗口,我想结束该过程而不是继续执行我的测试。有没有办法检测浏览器关闭?我在 selenium 日志中看到了信息,但我不知道如何在我的实际节点脚本中检测到这一点。这是 selenium 服务器日志中的错误:

12:33:17.122 WARN - 抛出异常 org.openqa.selenium.NoSuchWindowException:找不到窗口。浏览器窗口可能已关闭。

【问题讨论】:

    标签: node.js selenium automated-tests mocha.js webdriver-io


    【解决方案1】:

    您可以使用 client.sessions() 检查会话是否仍然存在。你可以用这个

    describe('check if session still exist', function() {
    
        it('should check if session still exist', function() {
        return browser.sessions().then(function(sessionid){
        var id1 = sessionid
        console.log(id1);
          });
       });
    });
    

    如果没有会话,上面将返回

    { state: 'success',
      sessionId: null,
      hCode: 1944289324,
      value: [],
      class: 'org.openqa.selenium.remote.Response',
      status: 0 }
    

    否则它会返回类似的东西

     state: 'success',
      sessionId: null,
      hCode: 381733075,
      value:
    
       [ { capabilities: [Object],
           id: '8ec2e2e8-6833-4105-8b84-1a6ce74a29ff',
           hCode: 1337873045,
           class: 'org.openqa.selenium.remote.server.handler.GetAllSessions$SessionInfo' } ],
      class: 'org.openqa.selenium.remote.Response',
      status: 0 }
    

    然后在基于上述value json对象的代码中的正确位置,您可以选择结束该过程。

    【讨论】:

      猜你喜欢
      • 2014-02-09
      • 2017-12-17
      • 2010-12-13
      • 2010-10-22
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      相关资源
      最近更新 更多