gpt4 book ai didi

javascript - 在 catch 子句 nightmarejs javascript 中截屏

转载 作者:行者123 更新时间:2023-12-03 13:23:32 25 4
gpt4 key购买 nike

下面的代码不起作用。它在 catch 子句中的 nightmare.screenshot('./screenshots/error_inner.png') 上失败。错误消息表明它实际上是在尝试读取而不是写入文件:error_inner.png

有没有人知道发生错误时如何获取屏幕截图。
帮助很重要
/托马斯·黑塞

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();
});

最佳答案

如果您 ,则无法截取任何屏幕截图或执行任何操作.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();
});

不要复制粘贴上面的代码,而是尝试了解它是如何工作的。

关于javascript - 在 catch 子句 nightmarejs javascript 中截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com