Webdriverio - 如何检测正在关闭的浏览器窗口

时间:2016-09-01 17:38:38

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

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

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

1 个答案:

答案 0 :(得分: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对象的代码中的正确位置,您可以选择结束该过程。

相关问题