Skip to content

Instantly share code, notes, and snippets.

@mjpearson
Created April 30, 2012 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjpearson/2562717 to your computer and use it in GitHub Desktop.
Save mjpearson/2562717 to your computer and use it in GitHub Desktop.
selenium test result retriever for saucelabs
#!/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";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment