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
Comments
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; |
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 Thank you for your quick response btw! |
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 |
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). |
All is working fine! Why do you use vo exactly? I don't understand what it does. Thank you for your help! Really appreciated |
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: |
Thank you! |
It looks like this issue is resolved. Closing. |
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?
The text was updated successfully, but these errors were encountered: