Phantomjs login, redirect and render page after pageLoad finishes -
i have website login form. if user not logged , tries access internal page redirected default page. instance if try access http://siteurl.phantomprint.aspx
redirected http://siteurl/default.aspx?returnurl=phantomprint.aspx.
, after login automatic redirect take place page.
after redirect want render page phantomjs , save pdf. problem rendering takes place before page load finished , can render page if use timeouts. in case, if page loading takes longer normal resulted pdf not proper one.
below can find java script code:
var page = require('webpage').create(); var index = 0, page.onconsolemessage = function (msg) { console.log(msg); }; var steps = [ function () { //load login page page.open("http://siteurl.phantomprint.aspx", function () { //enter credentials page.evaluate(function () { console.log("filling inputs"); var usernameinput = document.getelementbyid("txtusername"); usernameinput.value = "user"; var passwordinput = document.getelementbyid("txtpassword"); passwordinput.value = "password"; var loginbutton = document.getelementbyid("btnlogin"); loginbutton.click(); console.log("login button submitted"); }); }); }, function () { // page.onloadfinished = function () { // render page pdf page.render('example.png'); phantom.exit(); console.log("rendering finished"); //}); } ]; interval = setinterval(function () { if (!loadinprogress && typeof steps[testindex] == "function") { console.log("step " + (testindex + 1)); steps[testindex](); testindex++; } if (typeof steps[testindex] != "function") { console.log("test complete!"); phantom.exit(); } }, 1000);
any suggestions on how can assure rendering done after redirected page finishing loading welcomed.
it looks want process navigation steps. need use page.onnavigationrequested
pick if page load/redirect issued. hard maintain. have discard idea of using step array setinterval
.
another possibility wait selector present in target page using waitfor
, again, make use of setinterval
impossible.
casperjs built on top of phantomjs , uses steps navigate site. when use of then*
functions automatically pick page load , wait page load finish until executing callback.
var casper = require('casper').create(); casper.on("remote.message", function (msg) { console.log(msg); }); casper.start("http://siteurl/phantomprint.aspx", function () { //enter credentials this.evaluate(function () { console.log("filling inputs"); var usernameinput = document.getelementbyid("txtusername"); usernameinput.value = "user"; var passwordinput = document.getelementbyid("txtpassword"); passwordinput.value = "password"; }); this.click("#btnlogin"); this.echo("login button submitted"); }); casper.then(function () { this.capture('example.png'); }); casper.run();
this can made smaller using casper.fillselectors
.
Comments
Post a Comment