点击一个按钮运行PHP casperJS和回声效果与按钮相同的PHP页面 [英] Clicking a button to run casperJS in PHP and echo results in same PHP page with button

查看:263
本文介绍了点击一个按钮运行PHP casperJS和回声效果与按钮相同的PHP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个casperJS脚本登录多个用户提供的凭据;它返回量的成功和失败的登录,以及其中成功和失败的用户名。我试图通过点击PHP页面中一个按钮来运行该脚本,casperJS运行后,我想要的结果被回显到按钮被点击的同一页(无需重新加载页面)。我已经看过,并审议了以下Q /作为但这些都不提供足够的回答我的问题(因为一旦脚本结束,结果不显示):<一href="http://stackoverflow.com/questions/16377985/click-on-the-button-to-run-php-$c$c-and-echo-result">click在按钮上运行PHP code和回声效果,<一个href="http://stackoverflow.com/questions/18327862/run-execute-casperjs-script-by-clicking-a-button-on-webpage">Run/Execute CasperJS脚本通过点击网页, CasperJS数据传递回PHP <按钮/ A>。

I have a casperJS script which logs in multiple users with provided credentials; 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). 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.

我目前的Web服务器设置:

My current web server set up:

  • PhantomJS版本:1.9.7
  • CasperJS版本:1.1.0-β3
  • 的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脚本:要点

My casperJS script: gist

予先称为右PHP页面上的脚本casperJS(但是这显然导致脚本首先运行,然后在页面加载),使用该(从如何运行casperJS从PHP API 脚本):

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):

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

在加载的页面,它没有回声回结果的一部分从运行脚本。我不知道为什么只选择呼应了最后的 this.echo(JSON.stringify(跟踪)); 。我显然希望它打印所有的回声在剧本,但可能是另外一个问题;我也想被包上色的回声。此外,截屏并没有发生任何。

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));. 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.

现在我其实有一个按钮,当我preSS它,它仍然pssed只要在casperJS脚本运行$ P $,而一旦做了它变得无法pressed,但我没有回应任何结果。我没有办法知道,如果脚本运行或不能,因为屏幕没有出现,虽然时间长短的按钮依然pressed使我相信这是执行。所以这是我的PHP页面:

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. 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:

<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>

这是的onclick激活PHP页面的 phpTest.php

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

也许我这样做是完全错误的,我不应该有另外一个PHP页面,我不知道。我试图通过学习previous Q /为创建一个解决方案,但不能拿出一个解决方案。谢谢!

Maybe I am doing this completely wrong and I should not have another PHP page, I'm not sure. I've tried to create a solution by studying the previous Q/As but cannot come up with a solution. Thanks!

推荐答案

所以我想,我可以使用AJAX来显示在同一页面中的 casperjs 呼应的效果。我做了一个HTML页面有两个按钮:1)生成casperjs AJAX 运行AJAX,以及2)生成casperjs ,以简单地装载一个PHP页面执行casperjs脚本和打印结果在一个新的页面。下面是HTML(从<帮助href="http://stackoverflow.com/questions/7789521/how-to-link-external-javascript-file-onclick-of-button">How链接外部JavaScript文件的onclick按钮为AJAX按钮点击)的:

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>

casperjsajax.js code(用的 JS基础培训):

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);

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");
?>

和在 runscript.php 链接对于非AJAX

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>

这篇关于点击一个按钮运行PHP casperJS和回声效果与按钮相同的PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆