[英]Clicking a button to run casperJS in PHP and echo results in same PHP page with button
我有一個casperJS腳本,它使用提供的憑據登錄多個用戶; 返回成功和失敗登錄的數量以及成功和失敗的用戶名。 我試圖通過單擊PHP頁面中的按鈕來運行此腳本,並且在運行casperJS之后,我希望將結果回顯到單擊按鈕的同一頁面(不重新加載頁面)。 我查看並查看了以下Q / As,但這些都沒有為我的問題提供足夠的答案(因為一旦腳本結束就不會顯示結果): 點擊按鈕運行php代碼並回顯結果 , 運行/執行通過單擊網頁上的按鈕CasperJS腳本 , CasperJS將數據傳回PHP 。
我當前的Web服務器設置:
我的casperJS腳本: 要點
我首先在PHP頁面上調用了casperJS腳本(但這顯然導致腳本首先運行,然后加載頁面),使用它(來自如何從php API運行casperJS腳本 ):
echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js");
加載頁面后,它確實回顯了運行腳本的部分結果。 我不確定為什么它只選擇回應最后的this.echo(JSON.stringify(tracker)); 。 我顯然希望它能夠在腳本中打印所有的回聲,但這可能是另一個問題; 我還希望能夠將回聲着色。 此外,屏幕捕獲也沒有發生。
現在我實際上有一個按鈕,當我按下它時,只要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頁面,點擊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文件的幫助下點擊 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);
scriptresults.php代碼:
<?php
echo "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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.