在Apache 2.4中安装casperJS和phantomJS将数据传递给PHP

我试图通过单击网站中的运行button来运行我的casperjs脚本。 我的本地设置是PHP 5.5.14与Windows7上的Apache 2.4 [正常运行; 用php页面testing]; 我的问题是:如何正确安装casperJS和phantomJS,以便在执行脚本时识别PATH。 我需要知道Windows中的path是什么: putenv(“PHANTOMJS_EXECUTABLE = / usr / local / bin / phantomjs”); 。 我已经解决了以下可能的解决scheme,但是没有一个提到如何正确安装casperJS和phantomJS以便Web服务器识别它们: CasperJS将数据传回PHP ,将参数从php传递给casperjs / phantomjs , 如何运行casperJS脚本从PHP API , 使用casperjs和PHP来保存数据 , php执行幻像js的作品,但casperjs不工作的权限被拒绝

这是我当前的脚本,让多个用户login到一个页面,注销,然后返回多less成功/失败。

var casper = require('casper').create() var colorizer = require('colorizer').create('Colorizer'); var userNames = ['username1','username2','username3','username4', 'username5']; var passWords = ['password1','password2','password3','password4', 'password5']; var url = 'http://mywebsitenet.com'; var tracker = {Success: [], Fail: []}; function login(username, password) { casper.then(function () { this.sendKeys('#log', username); this.sendKeys('#pwd', password); this.click('#wpmem_login > form > fieldset > div.button_div > input.buttons'); // console.log(username + " has clicked the Log In button!") }); casper.waitFor(function check() { return this.evaluate(function() { return document.getElementById('wp-admin-bar-logout'); }); }, function then() { // step to execute when check() is ok this.click('#wp-admin-bar-logout > a'); tracker.Success.push(username); this.echo(this.fetchText('#wp-admin-bar-my-account > a') + " you logged in."); this.capture('Success_'+username+'.png'); }, function timeout() { // step to execute if check has failed tracker.Fail.push(username); this.echo("Warning: " + username + " could not be logged in.", "WARNING"); this.capture('Fail_'+username+'.png'); }); }; casper.start(); // empty page casper.viewport(1024, 768); userNames.forEach(function(username, index){ casper.thenOpen(url); // open the start page login(username, passWords[index]); // schedule the steps }); casper.then(function () { this.echo("Success: " + tracker.Success.length, "INFO"); this.echo("Fail: " + tracker.Fail.length, "WARNING"); this.echo(JSON.stringify(tracker)); }); casper.run(); // begin the execution 

所以我最终找出了casperJS和phantomJS的正确路径。 我把两个.exe放在C:\ casperjs \ bin中,甚至不需要将它们添加到我的PATH中,它运行得非常好。 这是我的index.php,它发送一个php页面的AJAX执行我的casperJ脚本(我已经为每个成功/失败的测试添加了一个计数器,以及允许用户选择测试运行的频率):

 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection"/> <link rel="shortcut icon" type="image/ico" href="favicon_jdoe.png" /> <script src="jquery-2.1.1.min.js" type="text/javascript"></script> <script type="text/javascript" language="javascript" src="jquery.dropdownPlain.js"></script> <title>CasperJS Automated Testing Unit</title> </head> <center> <body> <div id="mainContent"> <p>Welcome to the CasperJS Automated Testing Unit</p> <br> <button id="button_AJAX">Run CasperJS</button> <button id="button_STOP" onclick="myStopFunction()" style="display: none">Stop CasperJS</button> </div> <p> <select id="multi"> <option value="1">1 min</option> <option value="2">2 min</option> <option value="5">5 min</option> <option value="10">10 min</option> <option value="30" selected="selected">30 min</option> <option value="60">1 hour</option> <option value="360">6 hours</option> <option value="720">12 hours</option> <option value="1440">1 day</option> </select> </p> <p> <div class="centered"> <div style="float:left; margin-right:20px"> <div style="float:left">Success count:</div> <div id="succcount" style="float:left">0</div> </div> <div style="float:left"> <div style="float:left">Fail count:</div> <div id="failcount" style="float:left">0</div> </div> </div> </p> <br> <br> <div id="loading"></div> <script type="text/javascript"> var succcount = 0; var failcount = 0; $('#button_AJAX').click(function executecasperJS() { $('#loading').html('<img src="rays.gif"><br><i>Web harvesting in progress; please wait for test results.</i>'); // Loading image $.ajax({ // Run ajax request type: "GET", dataType: "text", url: "casperJS.php", success: function (data) { $('#loading').html(data); if( data.indexOf('Fail: 0') !== -1 ) { succcount++; } else { failcount++; } $('#succcount').html(succcount); $('#failcount').html(failcount); } }); multi = $( "#multi option:selected" ).val(); console.log("multi="+multi); timeout = setTimeout(executecasperJS,multi*60000); //1 min == 60000 }); $("#button_AJAX").click(function() {$("#button_AJAX").text("CasperJS Executed");}); $("#button_STOP").click(function() {$("#button_AJAX").text("Run CasperJS");}); function myStopFunction() { clearTimeout(timeout); } $("#button_AJAX").click(function(){ $("#button_STOP").show(); }); $("#button_STOP").click(function(){ $("#button_STOP").hide(); }); </script> </div> <div id="page-wrap"> <ul class="dropdown"> <li><a href="#">CasperJS Logs</a> <ul class="sub_menu"> <li><a href="casperjs_log.txt" target="_blank">Testing Log</a></li> <li><a href="casperjs_error.txt" target="_blank">Error Log</a></li> </ul> </div> </center> </body> </html> 

这里是casperJS.php ; 如果发生故障,则发送电子邮件:

 <?php set_time_limit(3600); date_default_timezone_set('America/New_York'); $date = date('m/d/Y h:i:s a', time()); $time_start = microtime(true); $output = exec("C:\casperjs\bin\casperjs casperJScript.js"); if (strpos($output, 'Fail: 0') === FALSE) { require_once('PHPMailer_5.2.4/class.phpmailer.php'); $mail = new PHPMailer(); $mail->IsSMTP(); $mail->SMTPDebug = 1; $mail->SMTPAuth = true; $mail->SMTPSecure = "ssl"; $mail->Host = "smtp.gmail.com"; $mail->Port = 465; $mail->IsHTML(true); $mail->Username = "email@host.com"; $mail->Password = "password"; $mail->SetFrom('email@host.co'); $mail->AddReplyTo("email@host.co"); $mail->Subject = "casperJS: server failure occured on $date"; $mail->Body = "The casperJS testing unit has picked up a server fault: $output $mail->AddAddress("email@host.co"); if(!$mail->Send()) { //echo "Mailer Error: " . $mail->ErrorInfo; } else { // echo "An error has occured and an email with the fault has been sent."; echo '<span style="color:#FF0000">An error has occured; it was logged and an email notification has been sent.</span>'; $userip = $_SERVER['REMOTE_ADDR']; $file = 'casperjs_error.txt'; $oldContents = file_get_contents($file); $fr = fopen($file, 'w'); $txt = "ERROR log: $output. Requested by: $userip on $date." . PHP_EOL . PHP_EOL ; fwrite($fr, $txt); fwrite($fr, $oldContents); fclose($fr); echo "<br />"; echo "<br />"; } } echo "Test Results: $output"; $userip = $_SERVER['REMOTE_ADDR']; $time_end = microtime(true); $time = $time_end - $time_start; echo "<br />"; echo "<br />"; echo "Last test completed in $time seconds\n on $date"; $file = 'casperjs_log.txt'; $oldContents = file_get_contents($file); $fr = fopen($file, 'w'); $txt = "Log: $output. Test completed in $time seconds\n on $date. Requested by: $userip" . PHP_EOL . PHP_EOL ; fwrite($fr, $txt); fwrite($fr, $oldContents); fclose($fr); ?>