Installing casperJS and phantomJS in WIndows Apache 2.4 to pass data to PHP

孤街醉人 提交于 2019-12-06 04:41:53

So I ended up figuring out the proper path to casperJS and phantomJS. I placed both of the .exe in C:\casperjs\bin and did not even need to add them to my PATH, and it worked very well. This is my index.php, which sends AJAX for a php page executing my casperJ-script (I have added a counter for each successful/failed test, as well as allowed the user to select how frequently the test should run):

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

and here is the casperJS.php; which sends out an email if a failure occurs:

<?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);        
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!