1

The code below is not working. It fails on nightmare.screenshot('./screenshots/error_inner.png') in the catch clauses. The error messages suggests it's actualy trying to read rather than write the file: error_inner.png

Is there anyone who knows how to get the screenshot when the error occurs. Help is much appricated /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();
  });
Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73
Tomas Hesse
  • 385
  • 3
  • 10

1 Answers1

2

You cannot take any screenshot or do anything if you .end the process. How about seperating two modules and then chaining them properly?

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

Don't copy paste the code above, but try to understand how it works.

Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73