我正在使用 Mocha 和WebDriverJS测试一个 Web 应用程序,或多或少如此处所述。当测试通过时,一切都很好。但是,如果一个测试失败,套件中的其余测试将超时,并且运行器将在套件结束时退出,而不关闭 Webdriver 实例。示例测试用例:
var assert = require('assert'),
client = require("webdriverjs").remote({
logLevel: 'silent'
});
describe('Self-test', function() {
before(function(done) {
client
.init()
.url('http://www.wikipedia.org/', function() {
done();
});
});
after(function(done) {
client.end(function() {
done();
});
});
// tests
it('should fail properly', function(done) {
client.getTitle(function(result) {
assert(false, 'This should fail');
done();
});
});
it('should pass afterwards', function(done) {
client.getTitle(function(result) {
assert(true, 'This should still pass');
done();
});
});
});
输出:
~> mocha test/self-test.js
Self-test
1) should fail properly
2) should pass afterwards
3) "after all" hook
✖ 3 of 2 tests failed:
1) Self-test should fail properly:
AssertionError: This should fail
at null.<anonymous> (./test/self-test.js:24:17)
at QueueItem (./node_modules/webdriverjs/lib/webdriverjs.js:242:15)
at null.<anonymous> (./node_modules/webdriverjs/lib/commands/getTitle.js:12:6)
at QueueItem (./node_modules/webdriverjs/lib/webdriverjs.js:242:15)
at IncomingMessage.WebdriverJs.proxyResponse (./node_modules/webdriverjs/lib/webdriverjs.js:782:6)
at IncomingMessage.EventEmitter.emit (events.js:115:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:402:27)
2) Self-test should pass afterwards:
Error: timeout of 10000ms exceeded
at Object.<anonymous> (./node_modules/mocha/lib/runnable.js:158:14)
at Timer.list.ontimeout (timers.js:101:19)
3) Self-test "after all" hook:
Error: timeout of 10000ms exceeded
at Object.<anonymous> (./node_modules/mocha/lib/runnable.js:158:14)
at Timer.list.ontimeout (timers.js:101:19)
据我所知,这是因为 WebDriverJS 队列在测试失败时会停止。有没有什么办法解决这一问题?即使对于本地命令行测试,它也不是最理想的,并且它使自动运行测试和/或在后台运行测试变得难以实现。
更新:我认为我可以通过为每个测试实例化一个新客户端来修复队列故障,但这会使事情变得更慢(因为 WebDriver 实例每次都需要从头开始启动)并且会使 WebDriver 进程在未杀死的情况下挂起测试失败。理想情况下,我想要类似Soda提供的结构,其中队列中某处的失败会跳到队列的末尾,然后抛出错误以供测试框架捕获。