单击按钮以在PHP中运行casperJS,并在带有按钮的相同PHP页面中返回结果


Clicking a button to run casperJS in PHP and echo results in same PHP page with button

我有一个casperJS脚本,登录多个用户提供凭据;它返回成功和失败的登录数量,以及成功和失败的用户名。我试图通过单击PHP页面中的按钮来运行此脚本,并且在casperJS运行后,我希望结果显示在单击按钮的同一页面上(无需重新加载页面)。我已经查看并审查了以下问题,但这些问题都没有提供足够的答案(因为脚本结束后结果不会显示):单击按钮运行php代码并返回结果,通过单击网页上的按钮运行/执行CasperJS脚本,CasperJS将数据传递回php。

我当前的web服务器设置:

  • PhantomJS版本:1.9.7
  • CasperJS版本:1.1.0-beta3
  • Apache/2.2.27 (Unix) mod_ssl/2.2.27 OpenSSL/1.0.1h DAV/2 PHP/5.5.14在OS X 10.9.3
我的casperJS脚本:gist

我首先在PHP页面上调用casperJS脚本(但这显然导致脚本首先运行,然后页面加载),使用这个(从如何从PHP API运行casperJS脚本):

echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js");

加载页面后,它确实回显运行脚本的一部分结果。我不知道为什么它只选择echo最后的this.echo(JSON.stringify(tracker));。我显然希望它打印脚本中的所有echo,但这可能是另一个问题;我也想给回声上色。此外,屏幕截图也没有发生。

现在我实际上有一个按钮,当我按下它时,只要casperJS脚本正在运行,它就会保持按下状态,一旦完成,它就会变得未按下,但我没有任何结果。我没有办法知道脚本是否运行,因为屏幕截图没有发生,尽管按钮保持按下的时间长度让我相信它被执行。这是我的PHP页面:

<html>
<head>
<title>casperJS testing</title>
</head>
<body>
    <center>
<p>Welcome to the casperJS Automated Testing Utility</p>
<br>
<input type="button" name="runcasperjs"  onclick="casperjs()"><div id="results">Waiting for echoes</>
<br>
<?php
    ## This Function Below Sets PhantomJs So CasperJS Can Use It
    putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
    echo "Running PhantomJS version: ";
    echo exec('/usr/local/bin/phantomjs --version 2>&1');
    echo "<br />";
    echo "Running CasperJS version: ";
    echo exec('/usr/local/bin/casperjs --version 2>&1');
    echo "<br />";
    echo 'Current PHP version: ' . phpversion();
    echo "<br />";
    $version = apache_get_version();
    echo "$version'n";
?>
    </center>
    <script>  
     function casperjs(){
       var xhReq = new XMLHttpRequest();
       var request = "http://pathToLocalHost/phpTest.php" // this will prepare a request to your server
       xhReq.open("GET", request, false);  // send a request
       xhReq.send(null);
       document.getElementsByID("results").innerHTML=xhReq.responseText  /// this will display results
       }
    </script>
</body>
</html>
这是激活的PHP页面onclick phptest。PHP :
<?php
echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js"); 
?>

也许我这样做完全错了,我不应该有另一个PHP页面,我不确定。我试图通过研究之前的Q/As来创造一个解决方案,但无法想出一个解决方案。谢谢!

所以我认为可以使用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 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);
脚本结果。php代码:
<?php
echo "Here are your test results!";
echo "<br />";
echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js");
?>

runscript.php链接用于非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>