据我所知,在After
钩子中使用promise或回调可以防止在使用promise/回调时执行命令队列。我正在努力找出原因,任何帮助或建议都将不胜感激。我能在github上找到的最接近的问题是:https://github.com/nightwatchjs/nightwatch/issues/341
声明:CCD_ 2(正是我的问题(。但没有提供解决方案。我需要在场景运行后运行清理步骤,并且这些清理步骤需要能够与浏览器交互。
https://github.com/nightwatchjs/nightwatch/wiki/Understanding-the-Command-Queue
在下面的代码段中,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
,因为它们在不同的执行上下文中。
基本上,让客户端命令在hook之后执行的唯一方法是我的第三个例子,但它阻止了我使用async。
如果命令队列没有冻结并阻止执行,那么第1个和第2个示例将非常好。
edit:我在github上发现了更多问题,这些问题表明浏览器在挂钩之前/之后不可用:https://github.com/nightwatchjs/nightwatch/issues/575
如果你想在所有功能运行后使用浏览器进行清理,你该怎么办?
尝试以下
After(async () => {
await client.perform(() => {
...
});
await moreWork();
})