Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.evaluate doesn't provide current open page's document variable #333

Closed
DWboutin opened this issue Nov 4, 2015 · 8 comments
Closed

.evaluate doesn't provide current open page's document variable #333

DWboutin opened this issue Nov 4, 2015 · 8 comments

Comments

@DWboutin
Copy link

DWboutin commented Nov 4, 2015

Hi, i'm trying to get the document's scrollWidth and scrollHeight with evaluate, but it do nothing. I want to pass my pageWidth and pageHeight to the screenshot method. How can i do this?

var pageWidth;
var pageHeight;
var nightmare = new Nightmare();
var nightmare = new Nightmare();
var NRdashboard = nightmare
    .viewport(1920, 1080)
    .useragent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36')
    .goto('https://login.newrelic.com/login')
    .wait()
    .type('#login_email', '***')
    .type('#login_password', '***')
    .click('#login_submit')
    .wait('body')
    .evaluate(function() {
        console.log('ASS');  // doesn't log
        console.log(document); // doesn't log
        pageWidth = document.body.scrollWidth;
        pageHeight = document.body.scrollHeight;
        return document.body.scrollWidth; // contains nothing
    })
    .screenshot(content_dir + '/dashboard.png')
    .goto('https://insights.newrelic.com/accounts/123456/dashboards/12345?kiosk=true')
    .wait(2000)
    .screenshot(content_dir + '/dashboard2.png')
    .run(function (err, nightmare) {
        if (err) return console.log(err);
        console.log('Done!');
        console.log(pageWidth, pageHeight); // undefined, undefined
    })
    .end();
@DWboutin DWboutin changed the title .evaluate doesn't work at all... .evaluate doesn't work at all Nov 4, 2015
@fmaruki-zz
Copy link

Variables inside the evaluate are in a different context than the functions from outside, so a variable won't be passed automatically. You need to pass variables from outside as the extra arguments of evaluate, and pass values back with return.

 var dimensions = yield nightmare.viewport(x, y) ... .evaluate(function() {
     ... ;
     return {pageWidth: pageWidth, pageHeight: pageHeight}
 }, arg1, arg2);
 var pageWidth = dimensions.pageWidth;

@DWboutin
Copy link
Author

DWboutin commented Nov 4, 2015

But how can i access do the document object of the page i'm currently in? I don't want to pass variables in, i want to get scrollHeight and scrollWidth of the page who are open.

I need to get document.body.scrollWidth, document.body.scrollHeight and change a variable in the localstorage.

Thank you for your quick response btw!

@DWboutin
Copy link
Author

DWboutin commented Nov 4, 2015

I tried to set things like you and the yield keyword. But it won't work. I have no arguments to pass to the evaluate methods.

function* newRelicDashboard(){

  var pageWidth;
  var pageHeight;

  var nightmare = Nightmare();

  yield nightmare
    .viewport(1920, 1080)
    .useragent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36')
    .goto('https://login.newrelic.com/login')
    .wait()
    .type('#login_email', '***')
    .type('#login_password', '***')
    .click('#login_submit')
    .wait()
    .goto('https://insights.newrelic.com/accounts/123456/dashboards/12345?kiosk=true')
    .wait(2000);

  var pageWidth = yield nightmare
    .evaluate(function (selector) {
      // now we're executing inside the browser scope.
      return document.body.scrollWidth;
    });

  var pageHeight = yield nightmare
    .evaluate(function (selector) {
      // now we're executing inside the browser scope.
      console.log(document.body.scrollHeight); // nothing
      return document.body.scrollHeight;
    });

  yield nightmare
    .evaluate(function(){
      localStorage.setItem('kioskNoir', 'true');
    })
    .screenshot(content_dir + '/dashboard2.png')
    .run(function (err, nightmare) {
      if (err) return console.log(err);
      console.log('Done!');
      console.log(pageWidth, pageHeight);  // undefined, undefined
    })
    .end();
}

The .evaluate(function(){ localStorage.setItem('kioskNoir', 'true'); }) works some times, but not always.

@DWboutin DWboutin changed the title .evaluate doesn't work at all .evaluate doesn't provide current open page's document variable Nov 4, 2015
@fmaruki-zz
Copy link

You need to wrap the generator function in a call with the libraries/functions vo or co, and start node with the flag --harmony (don't need flags for io.js).
Sorry for using yield, maybe it is not mandatory, I just never used nightmare without it.

@DWboutin
Copy link
Author

DWboutin commented Nov 4, 2015

All is working fine!

Why do you use vo exactly? I don't understand what it does.

Thank you for your help! Really appreciated

@fmaruki-zz
Copy link

I'm happy that you made it work!

Yield is like a return that can continue after the asynchronous operation is done. There must be someone wrapping the function to call it again as a callback once the result is set, so it can continue execution.

I usually use Co for this job, a library that I learned while programming with Koa.js. Vo looks like an sophisticated alternative to Co, I still don't know what it can do better for me.

Asynchronous callbacks, promisses, thunks and generators are all intimately related. It is a complex and fun topic, that I am still trying to fully understand.

Interesting links, I was trying to find another one, but can remember where it is:
https://www.promisejs.org/generators/
http://colintoh.com/blog/staying-sane-with-asynchronous-programming-promises-and-generators
https://medium.com/@rdsubhas/es6-from-callbacks-to-promises-to-generators-87f1c0cd8f2e

@DWboutin
Copy link
Author

DWboutin commented Nov 5, 2015

Thank you!

@rosshinkley
Copy link
Contributor

It looks like this issue is resolved. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants