简体   繁体   English

单击一个按钮在PHP中运行casperJS并使用按钮在相同的PHP页面中回显结果

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

I have a casperJS script which logs in multiple users with provided credentials; 我有一个casperJS脚本,它使用提供的凭据登录多个用户; which returns the amount successful and failed logins as well as the usernames which succeeded and failed. 返回成功和失败登录的数量以及成功和失败的用户名。 I am attempting to run this script by clicking a button within a PHP page, and after casperJS runs, I want the results to be echoed to the same page where the button was clicked (without reloading the page). 我试图通过单击PHP页面中的按钮来运行此脚本,并且在运行casperJS之后,我希望将结果回显到单击按钮的同一页面(不重新加载页面)。 I have looked at and reviewed the following Q/As but none of these provide an adequate answer to my issue (since the results are not displayed once the script ends): click on the button to run php code and echo result , Run/Execute CasperJS script by clicking a button on webpage , CasperJS passing data back to PHP . 我查看并查看了以下Q / As,但这些都没有为我的问题提供足够的答案(因为一旦脚本结束就不会显示结果): 点击按钮运行php代码并回显结果运行/执行通过单击网页上的按钮CasperJS脚本CasperJS将数据传回PHP

My current web server set up: 我当前的Web服务器设置:

  • PhantomJS version: 1.9.7 PhantomJS版本:1.9.7
  • CasperJS version: 1.1.0-beta3 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 Apache / 2.2.27(Unix)mod_ssl / 2.2.27 OpenSSL / 1.0.1h DAV / 2 PHP / 5.5.14
  • On OS X 10.9.3 在OS X 10.9.3上

My casperJS script: gist 我的casperJS脚本: 要点

I first called the casperJS script right on the PHP page (but this obviously caused the script to run first, and then the page loaded), using this (from How to run casperJS script from php API ): 我首先在PHP页面上调用了casperJS脚本(但这显然导致脚本首先运行,然后加载页面),使用它(来自如何从php API运行casperJS脚本 ):

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

Once the page loaded, it did echo back a portion of the results from running the script. 加载页面后,它确实回显了运行脚本的部分结果。 I am not sure why it only chose to echo the last this.echo(JSON.stringify(tracker)); 我不确定为什么它只选择回应最后的this.echo(JSON.stringify(tracker)); . I would obviously like for it to print all the echos in the script, but that may be another question; 我显然希望它能够在脚本中打印所有的回声,但这可能是另一个问题; I would also like to be bale to colorize the echoes. 我还希望能够将回声着色。 Also, the screen captures did not take place either. 此外,屏幕捕获也没有发生。

Now I actually have a button, and when I press it, it remains pressed for as long as the casperJS script is running, and once it is done it becomes unpressed, but I do not have any result echoed. 现在我实际上有一个按钮,当我按下它时,只要casperJS脚本正在运行它就会保持按下状态,一旦完成它就会变为未按下,但我没有任何结果回显。 I have no way of knowing if the script ran or not since the screenshots are not occurring, although the length of time the button remained pressed makes me believe it was executed. 我无法知道脚本是否运行,因为截图没有出现,虽然按钮保持按下的时间长度让我相信它已被执行。 So this is my PHP page: 这是我的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>

And this is the PHP page activated onclick phpTest.php : 这是激活的PHP页面,点击phpTest.php

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

Maybe I am doing this completely wrong and I should not have another PHP page, I'm not sure. 也许我这样做完全错了,我不应该有另一个PHP页面,我不确定。 I've tried to create a solution by studying the previous Q/As but cannot come up with a solution. 我试图通过研究之前的Q / As来创建一个解决方案,但无法提出解决方案。 Thanks! 谢谢!

So I figured that I could use AJAX to display the casperjs echoed results in the same page. 所以我想我可以使用AJAX在同一页面中显示casperjs回显结果。 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. 我创建了一个带有两个按钮的HTML页面:1) 运行casperjs AJAX以运行AJAX,以及2) 运行casperjs以简单地加载执行casperjs脚本的PHP页面并将结果打印在新页面中。 Here is the HTML (with help from How to link external javascript file onclick of button for AJAX button click): 这是HTML(在如何链接外部javascript文件的帮助下点击 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>

The casperjsajax.js code (with the aid of JS Essential Training ): 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);

The scriptresults.php code: scriptresults.php代码:

<?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 和非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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM