So I figured that I could use AJAX to display the casperjs echoed results in the same page. I made an HTML page with two buttons: 1) Run casperjs AJAX to run the AJAX, and 2) Run casperjs to simply load a PHP page executing the casperjs script and printing the results in a new page. Here is the HTML (with help from How to link external javascript file onclick of button for AJAX button click):
<!DOCTYPE html>
<html>
<head>
<title>casperJS testing</title>
</head>
<center>
<body>
<div id="mainContent">
<p>Welcome to the Automated Testing Utility</p>
<table>
<tr>
<td><button id="button_AJAX">Run casperjs AJAX</button></td>
<td><form action="runscript.php">
<input type="submit" value="Run casperJS">
</form></td>
</tr>
</table>
</div>
<script type="text/javascript">
var button = document.getElementById('button_AJAX');
button.onclick = function(){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "casperjsajax.js";
document.getElementsByTagName("head")[0].appendChild(script);
return false;
}
</script>
</center>
</body>
</html>
The casperjsajax.js code (with the aid of JS Essential Training):
// 1: Create the request
var myRequest;
// feature check!
if (window.XMLHttpRequest) { // does it exist? we're in Firefox, Safari etc.
myRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // if not, we're in IE
myRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
// 2: Create an event handler for request to call back
myRequest.onreadystatechange = function(){
if (myRequest.readyState === 4) {
var p = document.createElement("p");
var t = document.createTextNode(myRequest.responseText);
p.appendChild(t);
document.getElementById("mainContent").appendChild(p);
}
};
// Open and send it
myRequest.open('GET', 'scriptresults.php', true);
// any parameters?
myRequest.send(null);
The scriptresults.php code:
<?php
echo "Here are your test results!";
echo "<br />";
echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js");
?>
And the runscript.php link for non-AJAX
<html>
<head>
<title>casperJS Test Results</title>
</head>
<body>
<center>
<p>Here are your results!</p>
<br>
<?php
echo exec("/usr/local/bin/casperjs /full/path/to/your_script.js");
?>
</center>
</body>
</html>