public
Created

selenium test result retriever for saucelabs

  • Download Gist
saucyparser.php
PHP
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#!/usr/bin/env php
<?php
// Retrieves all selenium screenshot/video test results from saucelabs in a useful way
// quick and dirty, deal with it.
//
// cache is in /cache/{job_id} : remove any job and rerun to reprocess the job
// usable output is in /data/{domain name}/[s|v]_[html_file_name]_[browser][version][job_id][operating_system][_screenshot.png|_video.flv].
//
// @author michael pearson michael@cloudspark.com.au
 
// -u {username} -p {password}
$opts = getopt("u:p:");
 
$slUsername = isset($opts['u']) ? $opts['u'] : NULL;
$slPassword = isset($opts['p']) ? $opts['p'] : NULL;
 
if (empty($slUsername) || empty($slPassword)) {
echo "Usage: saucyparser.php -u {username} -p {password}\n\n";
exit;
}
 
$cacheBaseDir = getcwd() . '/cache';
 
if (!file_exists($cacheBaseDir)) {
mkdir($cacheBaseDir);
}
 
$dataBaseDir = getcwd() . '/data';
 
if (!file_exists($dataBaseDir)) {
mkdir($dataBaseDir);
}
 
$auth = stream_context_create(array('http' => array('header' => 'Authorization: Basic ' . base64_encode($slUsername . ":" . $slPassword))));
 
$endPoint = "https://saucelabs.com/rest/v1/$slUsername/";
$endPointNoVersion = "https://saucelabs.com/rest/$slUsername/";
 
$jobList = file_get_contents($endPoint . 'jobs', FALSE, $auth);
 
$jobs = json_decode($jobList, TRUE);
 
// now iterate and get the crap
foreach ($jobs as $job) {
$jobid = $job['id'];
 
// -------------------------------------------------------------------------
$myCache = $cacheBaseDir . '/' . $jobid;
if (!file_exists($myCache)) {
mkdir($myCache);
}
 
// -------------------------------------------------------------------------
// store meta
$outFile = $myCache . '/meta.txt';
if (!file_exists($outFile)) {
$meta = file_get_contents($endPoint . 'jobs/' . $jobid, FALSE, $auth);
file_put_contents($myCache . '/meta.txt', $meta);
} else {
$meta = file_get_contents($outFile);
echo "skipping $outFile \n";
}
 
$jobInfo = json_decode($meta, TRUE);
 
// -------------------------------------------------------------------------
// store log
$outFile = $myCache . '/log.txt';
if (!file_exists($outFile)) {
echo "processing " . $endPointNoVersion . 'jobs/' . $jobid . '/results/selenium-server.log' . "\n";
$log = file_get_contents($endPointNoVersion . 'jobs/' . $jobid . '/results/selenium-server.log', FALSE, $auth);
file_put_contents($outFile, $log);
} else {
$log = file_get_contents($outFile);
echo "skipping $outFile \n";
}
 
// -------------------------------------------------------------------------
// Extract last screenshot from log
$logLines = explode("\n", $log);
$ssNum = NULL;
foreach ($logLines as $line) {
$matches = array();
if (preg_match("!Command request: captureScreenshot\[c:\\\job_assets\\\shot_.*.png!", $line, $matches)) {
foreach ($matches as $match) {
$nMatch = array();
preg_match("!\d!", $match, $nMatch);
$n = array_shift($nMatch);
$ssNum = str_repeat('0', 4 - sizeof($n)) . $n;
}
}
}
 
if (NULL !== $ssNum) {
$outFile = $myCache . '/screenshot.png';
if (!file_exists($outFile)) {
$fName = $endPointNoVersion . 'jobs/' . $jobid . "/results/${ssNum}screenshot.png";
$imgData = file_get_contents($fName, FALSE, $auth);
echo "processing $fName\n";
file_put_contents($outFile, $imgData);
} else {
echo "skipping $outFile \n";
}
}
 
// -------------------------------------------------------------------------
// store video
$outFile = $myCache . '/video.flv';
if (!file_exists($outFile)) {
echo "processing " . $endPointNoVersion . 'jobs/' . $jobid . '/results/video.flv' . "\n";
$video = file_get_contents($endPointNoVersion . 'jobs/' . $jobid . '/results/video.flv', FALSE, $auth);
file_put_contents($outFile, $video);
} else {
echo "skipping $outFile \n";
}
}
 
$files = scandir($cacheBaseDir);
 
foreach ($files as $jobid) {
if ($jobid == '.' || $jobid == '..') {
continue;
}
$basePath = $cacheBaseDir . '/' . $jobid;
 
$meta = $basePath . '/meta.txt';
$video = $basePath . '/video.flv';
$screenshot = $basePath . '/screenshot.png';
$log = $basePath . '/log.txt';
 
$logData = file_get_contents($log);
 
// extract remote open request
$matches = array();
preg_match("/Command request: open\[.*, True\]/", $logData, $matches);
 
$host = '';
 
foreach ($matches as $match) {
$host = str_replace('Command request: open[', '', $match);
$host = str_replace(', True]', '', $host);
}
 
$hostInfo = parse_url($host);
if (!empty($hostInfo)) {
if (!isset($hostInfo['host'])) {
continue;
}
$hostActual = $hostInfo['host'];
$pathActual = str_replace('/', '', $hostInfo['path']);
$outDir = $dataBaseDir . '/' . $hostActual;
if (!file_exists($outDir)) {
mkdir($outDir);
}
 
// build the shnizzle
// get browser type + version, prepend this to video filename and copy it over
$fileMeta = json_decode(file_get_contents($meta), TRUE);
 
$fname = preg_replace('/\s*/', '', $pathActual . '_' . $fileMeta['browser'] . $fileMeta['browser_version'] . $jobid . $fileMeta['os']);
 
// prefixing filenames with 'v' or 's' so they're ordered correctly on the filesystem
$vfname = 'v_' . $fname . '_video.flv';
$cmd = "cp $video $outDir/$vfname";
exec($cmd);
 
$sfname = 's_' . $fname . '_screenshot.png';
$cmd = "cp $screenshot $outDir/$sfname";
exec($cmd);
 
echo "Processed $fname\n";
}
}
 
echo "\n done \n";

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.