Permalink
Browse files

add script to convert selenium html reports to json

  • Loading branch information...
1 parent 23c0820 commit 4979536088b7dd95893f51ce89db2aea1443621a @zaach zaach committed Aug 15, 2012
Showing with 79 additions and 1 deletion.
  1. +2 −1 package.json
  2. +77 −0 scripts/automation/convert_results.js
View
@@ -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",
@@ -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.