0

Hey I am trying to take a screenshot of an element with nightmare and than I want to extract text from this screenshot with a function and use the result of this in nightmare.type(). The Problem is when I use nightmare.run() I get an error that the screenshot does not exist. How can I fix this? This is my code:

var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');

var nightmare = new Nightmare();
nightmare.goto('website');
//Takes screenshot of element
nightmare.use(Screenshot.screenshotSelector('screenshot' + i + '.png', 'img[id="yw0"]'));
nightmare.type('input[id=AuthForm_login]', username);
nightmare.type('input[id=AuthForm_password]', password);

nightmare.type('input[id=upload]', image.decodeFile('screenshot' + i + '.png', function(err, result){
    //Returns Text from the Screenshot taken above!
    return result.text;
}));
nightmare.run();

When I remove the line:

nightmare.type('input[id=upload]', image.decodeFile('screenshot' + i + '.png', function(err, result){
        //Returns Text from the Screenshot taken above!
        return result.text;
    }));

Everything works fine and I get a screenshot.

NateW
  • 2,101
  • 3
  • 28
  • 37
Alesfatalis
  • 769
  • 2
  • 13
  • 32

1 Answers1

0

As nightmare uses Phantomjs you can do it that way, although I found this below that should help.

Credit to @fernandopasik who said:

I found a way to do this, with the "use" method for executing plugins.

nightmare = new Nightmare(); urls.forEach(function (url) {
    nightmare.goto(url).screenshot(path).use(function () {
        console.log('finished one');
    }); });

nightmare.run(function () {
    console.log('finished all'); });

NightmareJS screenshot callback

Community
  • 1
  • 1
MrPickles
  • 810
  • 1
  • 9
  • 27
  • I can't use this because when `nightmare.run()` it imediatly throws out the error and i need to process the image first and reuse it later in the same nightmare que – Alesfatalis Feb 18 '15 at 16:10