What are some common problems when trying to authenticate phantom.js against an asp.net site?
Here is my specific issue; I am able to navigate to the asp.net site and fill in the login form appropriately, I verify this by rendering a simple test.png file. Everything up to this point works as expected.
Once I submit the form (either by form.submit()
or element.click();
), the page reloads but is not redirected to the authenticated side of the site. It's simply the same page as if authentication failed. The difference, though, is that the new rendered page has the password removed from the password field.
I know the credentials are correct, as I can log in from any browser.
I am currently using the following test script which I received from another post and made some minor changes. Can someone please help me or point me in the right direction?
var page = require('webpage').create(),
testindex = 0,
loadInProgress = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
/*
page.onNavigationRequested = function(url, type, willNavigate, main) {
console.log('Trying to navigate to: ' + url);
console.log('Caused by: ' + type);
console.log('Will actually navigate: ' + willNavigate);
console.log('Sent from the page\'s main frame: ' + main);
};
*/
console.log('The default user agent is ' + page.settings.userAgent);
//changing the user agent to IE just in case!
page.settings.userAgent = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/4.0; InfoPath.2; SV1; .NET CLR 2.0.50727; WOW64)';
console.log('The new user agent is ' + page.settings.userAgent);
/*
The steps array represents a finite set of steps in order to perform the unit test
*/
var steps = [
function() {
//Load Login Page
page.open("https://www.aspxpage/signin.aspx");
},
function() {
//Enter Credentials
page.evaluate(function() {
//Fill the form with the correct credintals
//var loginForm = document.getElementById("aspnetForm");
document.querySelector('input[name=id]').value = 'blah';
document.querySelector('input[name=user]').value = 'blah';
document.querySelector('input[name=password]').value = 'blah';
});
},
function() {
//Login
page.evaluate(function() {
//Submit the form
//form submit
//1.
//var loginForm = document.getElementById("formid");
//loginForm.submit();
//2.
//button click
var button = document.querySelector('input[id=buttonid]');
button.click();
//console.log(button);
});
},
function() {
// Output content of page to stdout after form has been submitted
page.evaluate(function() {
//console.log(document.querySelectorAll('html')[0].outerHTML);
});
//render a test image to see if login passed
page.render('test.png');
}
];
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();
}
}, 50);