Skip to content

Instantly share code, notes, and snippets.

@abbood
Last active December 16, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abbood/5347252 to your computer and use it in GitHub Desktop.
Save abbood/5347252 to your computer and use it in GitHub Desktop.
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://site/login.html");
},
function() {
//Enter Credentials
page.evaluate(function() {
var inputElements = document.getElementsByTagName("input");
inputElements["login"].value = "login";
inputElements["passwd"].value = "pwd";
});
},
function() {
//Login
page.evaluate(function() {
//assuming there is only one form in the page
//if not, just iterate through them and pick
//the one matching a specific attribute
var loginForm = document.getElementsByTagName("form")[0];
loginForm.submit();
});
},
function() {
// enter search criteria
page.evaluate(function() {
var inputElements = document.getElementsByTagName("input");
inputElements['field'].value = "software engineer"; // search value
});
},
function() {
// perform search
page.evaluate(function() {
//assuming there is only one form in the page
//if not, just iterate through them and pick
//the one matching a specific attribute
var searchForm = document.getElementsByTagName("form")[0];
searchForm.submit();
});
},
function() {
// Output content of page to stdout after form has been submitted
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
console.log("this is results table: "+jQuery('#resultsTable').length);
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);
@abbood
Copy link
Author

abbood commented Apr 9, 2013

this demonstrates how the session persists within the same phantomjs page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment