因此,我认为可以使用AJAX 在同一页面上显示 casperjs 回显的结果。我用两个按钮制作了一个HTML页面:1) 运行casperjs
AJAX 以运行AJAX,以及2) 运行casperjs
以简单地加载执行casperjs脚本并将结果打印到新页面中的PHP页面。这是HTML(在“如何链接外部javascript文件onclick按钮以实现AJAX按钮单击的帮助下”):
<!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>该 casperjsajax.js 代码(带的帮助下JS基础培训):
// 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 backmyRequest.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 itmyRequest.open('GET', 'scriptresults.php', true);// any parameters?myRequest.send(null);该 scriptresults.php 代码:
<?phpecho "Here are your test results!";echo "<br />";echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js");?>以及非AJAX 的 runscript.php 链接
<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>


