javascript - 在 catch 子句 nightmarejs javascript 中截屏

标签 javascript nightmare regression-testing

下面的代码不起作用。它在 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/

相关文章:

javascript - 使用 Javascript 创建可重复的 HTML 表格

testing - 回归测试和突变测试有什么区别?

javascript - 即使在 JavaScript 中设置焦点后,Tab 事件也会更改焦点

javascript - 如何使用 Nightmare js 获取 JavaScript 触发下载?

javascript - Nightmare 条件等待()

javascript - 带有 Nightmare 的 Codeceptjs 不输出测试结果

.net - 性能测试

c++ - 在 C++ 环境中优化回归测试

javascript - 多次显示警报错误消息

javascript - 在 react 中按换行符拆分字符串