Skip to content

Instantly share code, notes, and snippets.

@frozeman
Created November 11, 2014 20:27
Show Gist options
  • Select an option

  • Save frozeman/b3f016de58b04c99cfc3 to your computer and use it in GitHub Desktop.

Select an option

Save frozeman/b3f016de58b04c99cfc3 to your computer and use it in GitHub Desktop.

Revisions

  1. frozeman created this gist Nov 11, 2014.
    73 changes: 73 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    var page = new WebPage(), 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");
    };

    var steps = [
    function() {
    //Load Login Page
    page.open("https://website.com/theformpage/");
    },
    function() {
    //Enter Credentials
    page.evaluate(function() {

    var arr = document.getElementsByClassName("login-form");
    var i;

    for (i=0; i < arr.length; i++) {
    if (arr[i].getAttribute('method') == "POST") {

    arr[i].elements["email"].value="mylogin";
    arr[i].elements["password"].value="mypassword";
    return;
    }
    }
    });
    },
    function() {
    //Login
    page.evaluate(function() {
    var arr = document.getElementsByClassName("login-form");
    var i;

    for (i=0; i < arr.length; i++) {
    if (arr[i].getAttribute('method') == "POST") {
    arr[i].submit();
    return;
    }
    }

    });
    },
    function() {
    // Output content of page to stdout after form has been submitted
    page.evaluate(function() {
    console.log(document.querySelectorAll('html')[0].outerHTML);
    });
    }
    ];


    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);