|
| 1 | +//- |
| 2 | +//- Usage |
| 3 | +//- lj ui-test [URL_PATH] |
| 4 | +//- |
| 5 | +//- Options |
| 6 | +//- --port Server port (default: 8091) |
| 7 | +//- --budget Virtual time budget in ms (default: 5000) |
| 8 | + |
| 9 | +var { command } = require("..") |
| 10 | +, consoleRe = /^[\s\S]*INFO:CONSOLE[^"]*"([\s\S]*)/ |
| 11 | + |
| 12 | +module.exports = function(opts) { |
| 13 | + var output = "" |
| 14 | + , buf = "" |
| 15 | + , urlPath = opts._[0] || "test/index.html" |
| 16 | + , browser = ["google-chrome-stable", "google-chrome", "chromium-browser", "chromium"].find(command) |
| 17 | + , retries = 10 |
| 18 | + opts._ = [] |
| 19 | + |
| 20 | + if (!browser) { |
| 21 | + console.error("No browser found") |
| 22 | + process.exitCode = 1 |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + var server = require("./serve")(opts) |
| 27 | + .on("error", function(e) { |
| 28 | + if (e.code === "EADDRINUSE" && retries--) return server.listen(++opts.port, "127.0.0.1") |
| 29 | + console.error(e.message) |
| 30 | + process.exitCode = 1 |
| 31 | + }) |
| 32 | + .on("listening", function() { |
| 33 | + var url = "http://127.0.0.1:" + opts.port + "/" + urlPath |
| 34 | + , proc = require("child_process").spawn(browser, [ |
| 35 | + "--headless", |
| 36 | + "--virtual-time-budget=" + opts.budget, |
| 37 | + "--enable-logging=stderr", |
| 38 | + "--no-sandbox", |
| 39 | + url |
| 40 | + ], { stdio: ["ignore", "ignore", "pipe"] }) |
| 41 | + |
| 42 | + proc.stderr.on("data", function(chunk) { |
| 43 | + buf += chunk |
| 44 | + var lines = buf.split(/", source: .*\n/m) |
| 45 | + buf = lines.pop() |
| 46 | + lines.forEach(parseLine) |
| 47 | + }) |
| 48 | + |
| 49 | + proc.on("close", function() { |
| 50 | + parseLine(buf) |
| 51 | + server.close() |
| 52 | + process.exitCode = /\u2714/.test(output) && !/FAIL|\u2718/.test(output) ? 0 : 1 |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + function parseLine(line) { |
| 57 | + var match = consoleRe.exec(line) |
| 58 | + if (match) { |
| 59 | + console.log(match[1]) |
| 60 | + output += match[1] |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
0 commit comments