1

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); 
Kara
  • 6,115
  • 16
  • 50
  • 57
xMythicx
  • 827
  • 1
  • 11
  • 27

1 Answers1

1

I think 50 ms is not enough time so you can get to the post-login page. You should chain that last call so that you don't interfere with the login process. And, for solidity/sanity, I would suggest that you don't render between the password fill and the button click (that might trigger some refresh of the page and erase your password from the text box). Keep the rendering code for debugging purposes, for the time being - but not for testing the whole chain of calls.

scrat.squirrel
  • 3,607
  • 26
  • 31
  • My apologies if I'm not completely understanding your solution. I do understand the first part meaning that the 50ms could indeed be the culprit here. I can change this to whatever say 5 seconds or in terms of javascript 5000 ms. What exactly do you mean by "chain the last call" I apologize but I am unfamiliar with this term. Would you kindly provide an example of what you mean. I am a bit new to javascript. Again sorry for the ignorance! – xMythicx Feb 11 '14 at 20:56
  • @xMythicx: here is a link to a post here on SO where it shows how to chain several calls to page.open: http://stackoverflow.com/questions/16996732/phantomjs-using-multiple-page-open-in-single-script/17000930#17000930 – scrat.squirrel Feb 12 '14 at 15:19
  • @xMythicx: That answer in the link I provided above shows how to chain all the page.open calls with a setTimeout so that it leaves enough time for each one to complete (you must test/choose a proper timeout for your own case). – scrat.squirrel Feb 12 '14 at 15:25
  • I looked at the link. I feel this method, which you refer to as chaining logically is the same as my script above. My script sets a timeout between function calls which was 50 ms and I agree with you that 50 ms is indeed not enough time to allow the first page to load completely. Even when setting it to say 5 or even 10 seconds I received the same error. I don't think chaining with settimeout of 100 ms compared to stepping through an array of functions with settimeout of 100 ms logically is any different. Either way we are simply waiting 100 ms to call a function to do work. – xMythicx Feb 12 '14 at 16:22
  • The page render call may be the culprit! I must do more testing to find out. Thank you for the advice. – xMythicx Feb 12 '14 at 16:23
  • Yeah. You're using `setInterval` which is not the same as `setTimeout`; similar code, but the one I suggested is, in my opinion, better (also because I used the same approach in my projects). I suggested to not do that rendering in my answer on Feb. 7, yet the bounty has expired since. – scrat.squirrel Feb 12 '14 at 16:48
  • My apologies on the bounty. It was the first one that I offered. I did not realize that after it expired I could not award the points. Again sorry!!! – xMythicx Feb 13 '14 at 16:34