Selenium 当mocha测试超时时停止webdriver
我在运行摩卡和webdriverio时遇到问题。当测试超时时,webdriver客户端将继续运行Selenium 当mocha测试超时时停止webdriver,selenium,webdriver,mocha.js,Selenium,Webdriver,Mocha.js,我在运行摩卡和webdriverio时遇到问题。当测试超时时,webdriver客户端将继续运行 var assert = require('assert'); var webdriverio = require('webdriverio'); describe('suite', function() {
var assert = require('assert');
var webdriverio = require('webdriverio');
describe('suite', function() {
beforeEach(function(callback) {
this.client = webdriverio.remote().init(callback);
});
it('test1', function(callback) {
this.timeout(500);
this.client
.pause(550)
.call(function() {
assert(false, 'You shouldn\'t be here!');
})
.call(callback);
});
afterEach(function(callback) {
this.client.end(callback);
});
});
当我执行mocha test.js时,我得到:
我不希望也不期望第二个错误发生。如何实现这一点?我最终编写了自己的摩卡包装,扩展了普通的摩卡跑步者,然后能够捕获第一个错误并阻止第二个错误的执行。不幸的是,没有简单的解决方案…Mocha为异步函数提供的回调可以使用包含所发生错误的参数来调用。捕捉断言抛出的异常并将其传递给回调,允许Mocha继续执行其工作。摩卡只是忽略了回调的调用 必须删除第二个.call调用,因为在代码没有超时的情况下,回调将被调用两次 如果您不想添加,请尝试。。。接住您可以使用的任何地方:
function handle_errors(test_code, callback) {
return function () {
try {
test_code();
callback();
}
catch (e) {
callback(e);
}
};
}
并将呼叫更改为:
this.client
.pause(550)
.call(handle_errors(function() {
assert(false, 'You shouldn\'t be here!');
}, callback));
好的,很酷。我喜欢这个解决方案,似乎是合法的,但是没有办法确认它,因为我再也无法访问该项目了,但我会继续,并将其标记为正确的,因为它比我的答案更好:很高兴帮助!这不是不可能的,我错过了一些东西,但我在发布之前测试了代码。
function handle_errors(test_code, callback) {
return function () {
try {
test_code();
callback();
}
catch (e) {
callback(e);
}
};
}
this.client
.pause(550)
.call(handle_errors(function() {
assert(false, 'You shouldn\'t be here!');
}, callback));