使用异步NightwatchAfterHook与客户端交互是不起作用的



据我所知,在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();
})

最新更新