以下代码无效。它在catch子句中的nightmare.screenshot('./ screenshots / error_inner.png')上失败了。错误消息表明它正在尝试读取而不是写入文件:error_inner.png
是否有人知道如何在发生错误时获取屏幕截图。 帮助非常丰富 / Tomas Hesse
var Nightmare = require('nightmare'),
nightmare = Nightmare({
show: true,
height: 1080,
width: 1920
});
var myVar = 'Non init';
nightmare
.goto('http://localhost:8082/myPage1')
.wait('.recloc')
.screenshot('./screenshots/tada.png')
.evaluate(() => { return document.querySelector('span.myClass').innerHTML;})
// .end()
.then((textFound) => {
myVar = textFound;
console.log('Outer nightmare Sucess:', textFound);
nightmare.goto('http://localhost:8082/myPage2')
.wait('#nav > ul.pull-left.navigation.hidden-xs > li:nth-child(3) > a')
.click('Non existing Element ie Error is thrown')
.end()
.then(()=>{
console.log('Outer nightmare Sucess:', myVar )
})
.catch((error) => {
nightmare.screenshot('./screenshots/error_inner.png')
console.error('Inner nightmare failed:', error);
return nightmare.end();
})
})
.catch((error) => {
console.error('Outer nightmare failed:', error);
nightmare.screenshot('./screenshots/error_outer.png')
return nightmare.end();
});
答案 0 :(得分:0)
如果您 .end 该过程,则无法截屏或执行任何操作。分离两个模块然后正确链接它们怎么样?
const Nightmare = require("nightmare");
const nightmare = Nightmare({
show: true,
height: 1080,
width: 1920
});
const myVar = "Non init";
function innerCircle() {
return new Promise((resolve, reject) => {
nightmare
.goto("http://localhost:8082/myPage1")
.wait(".recloc")
.screenshot("./screenshots/tada.png")
.evaluate(() => {
return document.querySelector("span.myClass").innerHTML;
})
.then(textFound => {
resolve(textFound);
})
.catch(error => {
console.error("Outer nightmare failed:", error);
nightmare.screenshot("./screenshots/error_outer.png");
reject(error);
});
});
}
function outerCircle(textFound) {
return new Promise((resolve, reject) => {
nightmare
.goto("http://localhost:8082/myPage2")
.wait("#nav > ul.pull-left.navigation.hidden-xs > li:nth-child(3) > a")
.click("Non existing Element ie Error is thrown")
.then(() => {
console.log("Outer nightmare Sucess:", myVar);
resolve(myVar);
})
.catch(error => {
nightmare.screenshot("./screenshots/error_inner.png");
console.error("Inner nightmare failed:", error);
reject(error);
});
});
}
// run them
innerCircle()
.then(outerCircle)
.then(()=>{
nightmare.end()
})
.catch(error => {
nightmare.end();
});
请勿复制粘贴上面的代码,但请尝试了解其工作原理。