【问题标题】:Take screenshot in catch clause nightmarejs javascript在 catch 子句 nightmarejs javascript 中截屏
【发布时间】:2018-01-30 10:41:04
【问题描述】:

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

【问题讨论】:

    标签: javascript nightmare regression-testing


    【解决方案1】:

    如果您.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();
    });
    

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

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 2014-12-22
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      相关资源
      最近更新 更多