Skip to content
This repository has been archived by the owner on May 10, 2019. It is now read-only.

Commit

Permalink
add script to convert selenium html reports to json
Browse files Browse the repository at this point in the history
  • Loading branch information
zaach committed Aug 15, 2012
1 parent 23c0820 commit 4979536
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -42,7 +42,8 @@
"irc": "0.3.3",
"jshint": "0.7.1",
"minimatch": "0.2.6",
"which": "1.0.5"
"which": "1.0.5",
"htmlparser": "1.7.6"
},
"scripts": {
"postinstall": "node ./scripts/generate_ephemeral_keys.js",
Expand Down
77 changes: 77 additions & 0 deletions scripts/automation/convert_results.js
@@ -0,0 +1,77 @@
#!/usr/bin/env node
/*
* Converts html reports into nice, machine readable JSON
* Run: $ ./convert_result.js result/index.html
*/

const fs = require('fs'),
path = require('path'),
jsonselect = require('jsonselect'),
htmlparser = require('htmlparser');


function main (args) {
var file = fs.readFile(path.resolve(args[2]), "utf8", function (err, html) {
if (err) throw err;
parseReport(html);
});
}

function parseReport (html) {

var handler = new htmlparser.DefaultHandler(function(err, dom) {
if (err) {
console.error("Error: " + err);
} else {
var results = jsonselect.match(':has(:root > .attribs > .id:val("results")) .children :has(:root > .name:val("tr"))', dom);
var report = {};

// remove header row
results.shift();

results.forEach(function (node, i, array) {
var url;
var result = node.children[1].attribs.class;

// skip traceback rows
if (!result) return;

try {
url = result === 'error' ?
findJobUrl(array[i+1].children[1].children[1].children) :
node.children[9].children[0].attribs.href;
} catch (e) {
url = '';
}

var name = node.children[5].children[0].data;

report[name] = {
success: result === 'passed',
class: node.children[3].children[0].data,
duration: node.children[7].children[0].data,
url: url
};
});

console.log(JSON.stringify(report, null, ' '));
}
});

var parser = new htmlparser.Parser(handler);
parser.parseComplete(html);
}

// extract saucelab url from error report
function findJobUrl (children) {
var result;
children.forEach(function (node) {
var match = node.raw.match(/https:\/\/saucelabs.com\/jobs\/[a-f0-9]+/);
if (match) result = match[0];
});
return result;
}

exports.parseReport = parseReport;

if (process.argv[1] === __filename) main(process.argv);

0 comments on commit 4979536

Please sign in to comment.