【发布时间】:2018-07-30 21:32:12
【问题描述】:
据我所知,在 After 钩子中使用承诺或回调会阻止命令队列在使用承诺/回调时执行。我试图找出原因,感谢任何帮助或建议。我在 github 上能找到的最接近的问题是:https://github.com/nightwatchjs/nightwatch/issues/341
其中指出:finding that trying to make browser calls in the after hook is too late; it appears that the session is closed before after is run.(正是我的问题)。但是没有提供解决方案。我需要在我的场景运行后运行清理步骤,并且这些清理步骤需要能够与浏览器交互。
https://github.com/nightwatchjs/nightwatch/wiki/Understanding-the-Command-Queue
在下面的 sn-p 中,bar 永远不会输出。只需foo。
const { After } = require('cucumber');
const { client } = require('nightwatch-cucumber');
After(() => new Promise((resolve) => {
console.log('foo')
client.perform(() => {
console.log('bar')
});
}));
我也尝试过使用回调方法
After((browser, done) => {
console.log('foo');
client.perform(() => {
console.log('bar');
done();
});
});
但与第一个示例类似,bar 永远不会输出,只是foo
你可以改用类似的东西:
const moreWork = async () => {
console.log('bar');
await new Promise((resolve) => {
setTimeout(resolve, 10000);
})
}
After(() => client.perform(async () => {
console.log('foo');
moreWork();
}));
但是moreWork 的异步特性意味着客户端在我的工作完成之前终止,所以这对我来说并不适用。您不能在 perform 中使用 await,因为它们处于不同的执行上下文中。
基本上,让客户端命令在钩子后执行的唯一方法是我的第三个示例,但它阻止我使用异步。
如果命令队列没有冻结并阻止执行,第一个和第二个示例会很棒。
编辑:我在 github 上发现更多问题表明浏览器在钩子之前/之后不可用:https://github.com/nightwatchjs/nightwatch/issues/575
如果您想在所有功能运行后使用浏览器进行清理,您应该怎么做?
【问题讨论】: