This repository has been archived by the owner on May 10, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script to convert selenium html reports to json
- Loading branch information
Showing
2 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |