I have a website with a login form. If the user is not registered and tries to access the internal page, he will be redirected to the default page. For example, if I try to access http://siteURL.PhantomPrint.aspx I will be redirected to http://siteURL/Default.aspx?ReturnUrl=PhantomPrint.aspx. And after entering the page, a redirect will be automatically sent.
After redirecting, I want to display the page using Phantomjs and save it in pdf format. The problem is that the rendering is done before the page loading is completed, and I can display the page correctly only if I use timeouts. In this case, if the page loading takes longer than usual, the resulting pdf file is not correct.
Below you can find the 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 was submitted"); }); }); }, function () { // page.onLoadFinished = function () { // Render the page to 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 I can assure you that rendering is only done after the redirected page finishes loading is welcome.
source share