This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcheck_selenium
executable file
·113 lines (93 loc) · 3.89 KB
/
check_selenium
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/php
<?php
# ------------------------------------------------------------------
# We offer no warantee or guarantee - use this code at your own risk!
# All code is Copyright (C) 2011, Applied Trust Engineering, Inc.
# ------------------------------------------------------------------
#
# Depends on PHPunit and the Testing_Selenium library:
# sudo pear config-set auto_discover 1
# sudo pear install pear.phpunit.de/PHPUnit
# sudo pear install channel://pear.php.net/Testing_Selenium-0.4.4
# see: https://github.com/sebastianbergmann/phpunit/issues/367
/*
define command{
command_name check_selenium
command_line $USER1$/check_selenium -d $ARG1$ -u $ARG2$
}
*/
// handle options
$options = getopt("d:u:h:");
if (!isset($options["d"])) { usage(); }
if (!isset($options["u"])) { usage(); }
if (!isset($options["h"])) { usage(); }
if (!preg_match('/^http/', $options["u"])) { usage(); }
$test_directory = $options["d"];
$base_url = $options["u"];
$selenium_host = $options["h"];
// run test cases
$mytest = create_phpunit_test_file($test_directory, $base_url, $selenium_host);
$test_results = run_phpunit_test($mytest);
#unlink($mytest);
// all done
echo $test_results['output']."\n";
exit ($test_results['status']);
########################
function usage() {
echo "usage: check_selenium -d <test_directory> -u <base_url> -h <selenium_host>\n";
exit(1);
}
########################
function run_phpunit_test($testfile) {
$phpunit_cmd = '/usr/bin/phpunit '; # --log-junit phunit-bnw.log ';
$cmd = $phpunit_cmd.$testfile;
$name = 'selenium';
#echo "running: $cmd\n";
$result = `$cmd`;
if (preg_match('/Time: (\d+) seconds/', $result, $matches_first) and preg_match('/OK \((\d+) tests{0,1}, (\d+) assertions\)/', $result, $matches_second)) {
$output = "Selenium OK: ";
$output .= $matches_second[2]." assertions, zero fails";
$output .= " |".$name."_time=".$matches_first[1].";";
$output .= $name."_tests=".$matches_second[1].";";
$output .= $name."_assertions=".$matches_second[2].";";
$output .= $name."_failures=0";
$status = 0;
} else if (preg_match('/Time: (\d+) seconds/', $result, $matches_first) and preg_match('/FAILURES!\nTests: (\d+), Assertions: (\d+), Failures: (\d+)\./', $result, $matches_second)) {
$output = "Selenium ERROR: ";
$output .= $matches_second[2]." assertions, ".$matches_second[3]." fails";
$output .= " |".$name."_time=".$matches_first[1].";";
$output .= $name."_tests=".$matches_second[1].";";
$output .= $name."_assertions=".$matches_second[2].";";
$output .= $name."_failures=".$matches_second[3];
$status = 2;
} else {
$output = "Selenium Error: parse error: ".$result;
$status = 2;
}
return array( 'output' => $output, 'status' => $status );
}
########################
function create_phpunit_test_file($selenese_test_dir, $browser_url, $selenium_host) {
$tmpfname = tempnam("/tmp", "check_selenium_temp_file");
$filecontents = "";
$filecontents .= "<?php \n";
$filecontents .= "require_once 'PHPUnit/Extensions/SeleniumTestCase.php'; \n";
$filecontents .= "class SeleniumTests extends PHPUnit_Extensions_SeleniumTestCase { \n";
$filecontents .= " public static \$seleneseDirectory = '".$selenese_test_dir."'; \n";
$filecontents .= " public static \$browsers = array( \n";
$filecontents .= " array( \n";
$filecontents .= " 'name' => 'Firefox on Windows Server 2008', \n";
$filecontents .= " 'browser' => '*firefox', \n";
$filecontents .= " 'host' => '".$selenium_host."', \n";
$filecontents .= " 'port' => 4444, \n";
$filecontents .= " 'timeout' => 10000, \n";
$filecontents .= " ), \n";
$filecontents .= " ); \n";
$filecontents .= " protected function setUp() { \n";
$filecontents .= " \$this->setBrowserUrl('".$browser_url."'); \n";
$filecontents .= " } \n";
$filecontents .= "} \n\n";
file_put_contents($tmpfname, $filecontents);
return $tmpfname;
}
########################