johntitus/node-horseman

Multiple evaluations

Closed this issue · 1 comments

I want to do multiple evaluations on a page. Specifically plainText and screenshotBase64.

For 1 evaluation the code is fairly easy:

var horseman = new Horseman(); 
horseman 
  .userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0') 
  .open(url) 
  .plainText() 
  .then(plainText => horseTest(plainText)) 
  .close(); 

function horseTest(plainText) { 
   console.log(plainText); 
}

But how do I go about passing both the plainText and the screenshotBase64 results to the horseTest function?

Here is one way to work with the results of multiple Promises in a chain.

var horseman = new Horseman();

horseman 
  .userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0') 
  .open(url) 
  .plainText() 
  .then(plainText => {
    // The Promises returned by Horseman instances all have `this` bound to the horseman instance
    return this.screenshotBase64('PNG').then(screenshot => {
      return horseTest(plainText, screenshot);
    });
  })
  .close(); 

function horseTest(plainText, screenshot) { 
  console.log(plainText);
  console.log(screenshot);
}