coverage for browser tests

This commit is contained in:
Florian Dold 2016-11-13 18:44:50 +01:00
parent f5a1a26368
commit 095b19aeb4
7 changed files with 12721 additions and 6 deletions

2
testlib/selenium/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
coverage/
coveage-*.json

File diff suppressed because one or more lines are too long

6605
testlib/selenium/esprima.js Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -35,12 +35,23 @@ var httpPort = 8080;
var p = `http://localhost:${httpPort}/testlib/selenium/testhost.html`;
var argv = require('minimist')(process.argv.slice(2), {"boolean": ["keep-open"]});
var argv = require('minimist')(process.argv.slice(2), {"boolean": ["keep-open", "coverage"]});
function printUsage() {
console.log(`Usage: [--keep-open] TESTSCRIPT`);
}
function randId(n) {
let s = "";
var choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < n; i++) {
s += choices.charAt(Math.floor(Math.random() * choices.length));
}
return s;
}
if (argv._.length != 1) {
console.log("exactly one test script must be given");
printUsage();
@ -48,6 +59,7 @@ if (argv._.length != 1) {
}
var testScriptName = path.resolve(argv._[0]);
var testName = path.basename(testScriptName, ".js");
var projectRoot = path.resolve(__dirname, "../../") + "/";
if (!testScriptName.startsWith(projectRoot)) {
console.log("test file must be inside wallet project root");
@ -119,6 +131,9 @@ var driver = new webdriver.Builder()
.build();
driver.get(p);
if (argv["coverage"]) {
driver.executeScript("window.requestCoverage = true;");
}
driver.executeScript(script);
driver.wait(untilTestOver);
@ -136,9 +151,23 @@ driver.manage().logs().get("browser").then((logs) => {
console.log(l.message.substring(s2));
}
if (!argv["keep-open"]) {
driver.quit();
l.close();
}
let coverage = driver.executeScript("return JSON.stringify(window.__coverage__);");
coverage.then((covStr) => {
let cov = JSON.parse(covStr);
if (cov) {
let covTranslated = {};
for (let f in cov) {
let p = path.resolve(projectRoot, f);
let c = covTranslated[p] = cov[f];
c.path = p;
}
fs.writeFileSync(`coverage-${testName}-${randId(5)}.json`, JSON.stringify(covTranslated));
}
if (!argv["keep-open"]) {
driver.quit();
l.close();
}
})
});

File diff suppressed because one or more lines are too long

View File

@ -2,8 +2,37 @@
<html>
<head>
<title>Browser Test Host</title>
<script src="/lib/vendor/system-csp-production.src.js"></script>
<script src="/testlib/selenium/esprima.js"></script>
<script src="/testlib/selenium/escodegen.browser.js"></script>
<script src="/testlib/selenium/instrumenter.js"></script>
<!-- for instrumentation to work, we have to use the non-csp version -->
<script src="/testlib/selenium/system.js"></script>
<script>
var requestCoverage = false;
let parser = document.createElement('a');
let oldTranslate = System.translate.bind(System);
System.translate = (load) => {
if (!requestCoverage) {
return oldTranslate(load);
}
let inst = new Instrumenter();
let srcP = oldTranslate(load);
console.dir(load);
return Promise.resolve(srcP).then((src) => {
parser.href = load.name;
let modName = parser.pathname.substring(1);
return inst.instrumentSync(src, modName);
});
}
System.config({
baseURL: "/",
defaultJSExtensions: true,