【问题标题】:Installing casperJS and phantomJS in WIndows Apache 2.4 to pass data to PHP在 WIndows Apache 2.4 中安装 casperJS 和 phantomJS 以将数据传递给 PHP
【发布时间】:2014-07-19 20:23:34
【问题描述】:

我正在尝试通过单击网站中的 运行 按钮来运行我的 casperjs 脚本。我的本地设置是 PHP 5.5.14 和 Windows7 上的 Apache 2.4 [正确运行;用php页面测试];我的问题是:如何正确安装 casperJS 和 phantomJS 以便在执行脚本时识别其 PATH 。我需要知道 Windows 中的路径才能使用它:putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");。我已经研究了以下可能的解决方案,但没有一个提到如何正确安装 casperJS 和 phantomJS 以供 Web 服务器识别它们:CasperJS passing data back to PHPPass parameter from php to casperjs/phantomjsHow to run casperJS script from php APIUsing casperjs and PHP to save dataphp execution phantom js works but casperjs does not work permission denied

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

    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

【问题讨论】:

  • 我认为这是一个特定于 Windows 的问题,所以我只是添加了该标签。 (对不起,无法回答我自己。)
  • 我决定切换到 Mac 环境,但仍然想知道正确的安装/路径是什么才能像我在这里所做的那样(但在 Windows 中):stackoverflow.com/questions/24856492/…

标签: php windows apache phantomjs casperjs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-19
    • 2017-04-02
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多