如何使用nightmare.js wait()来检查是否加载了javascript对象/变量?
我正在测试一个严重依赖javascript来呈现内容的网页。我正在使用Nightmare.js,在注入数据和运行测试之前,我需要等待加载javascript。但是,我还没有找到一种使用nightmare.js wait()来处理javascript的方法。
我试过这样的方法:
nightmare
.goto('file:\\\\' + __dirname + '\\index.html');
.wait("#ext-quicktips-tip") //make sure index.html is loaded
.wait(function() {
return App.app //should return true when App.app is available
}
.end()
.then();我试过使用调试控制台,页面加载得很好。App.app在控制台中是可用的,但是nightmare.js永远不会看到App.app
发布于 2017-05-08 03:14:43
正如在Nightmare.js的API文档中解释的那样,如果您将一个函数传递给wait方法,它应该返回true来告诉Nightmare.js它可以恢复测试。
因此,如果您希望测试等到App.app可用时,可以让您的函数返回一个布尔表达式,如果它存在,即不是undefined,如下所示:
nightmare
.goto('file:\\\\' + __dirname + '\\index.html');
.wait("#ext-quicktips-tip") //make sure index.html is loaded
.wait(function() {
// return true when App.app is available
return typeof App.app !== undefined;
}
.end();请注意,我忽略了原始代码片段中的最后一个then --这是多余的。
https://stackoverflow.com/questions/43839074
复制相似问题