Skip to content

Commit

Permalink
fix(log): console log capture is now enabled by default again
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigg committed Mar 2, 2020
1 parent e4e527b commit 1e79353
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/MochaWebDriverRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export async function withWebDriver<T>(capabilities: Object | Capabilities, test

/**
* Run Mocha tests using `webDriver` (instance or `Capabilities` used to build instance).
*
* @param webDriver
* @param url
* @param options
*/
export async function runMochaWebDriverTest(
webDriver: WebDriver | Capabilities,
Expand All @@ -48,14 +44,17 @@ export async function runMochaWebDriverTest(
}

options = options || {};
if (options.captureConsoleLog === undefined) {
options.captureConsoleLog = true;
}
const passOptionsWithQueryString = true;

if (passOptionsWithQueryString) {
const queryStringParams: any = {
useMochaWebDriverRunner: 1
};
if (options.captureConsoleLog !== undefined) {
queryStringParams.captureConsoleLog = options.captureConsoleLog;
queryStringParams.captureConsoleLog = options.captureConsoleLog !== false;
}
if (options.delay !== undefined) {
queryStringParams.delay = options.delay;
Expand All @@ -64,7 +63,7 @@ export async function runMochaWebDriverTest(
queryStringParams.timeout = options.timeout;
}
if (options.globals !== undefined) {
queryStringParams.globals = options.globals.join(',');
queryStringParams.globals = options.globals.join(",");
}
if (options.checkLeaks !== undefined) {
queryStringParams.checkLeaks = options.checkLeaks;
Expand All @@ -73,9 +72,9 @@ export async function runMochaWebDriverTest(
queryStringParams.grep = options.grep;
}
if (options.globalsToSave !== undefined && options.globalsToSave.length > 0) {
queryStringParams.globalsToSave = options.globalsToSave.join(',');
queryStringParams.globalsToSave = options.globalsToSave.join(",");
}
const delimiter = url.indexOf('?') === -1 ? '?' : '&';
const delimiter = url.indexOf("?") === -1 ? "?" : "&";
url = url + delimiter + querystring.stringify(queryStringParams);
}

Expand Down
18 changes: 16 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface CliOptions {
}

let DEFAULT_CLI_OPTIONS: Readonly<CliOptions> = {
captureConsoleLog: true,
capabilities: {},
timeout: 2000,
reporter: "spec"
Expand Down Expand Up @@ -66,6 +67,15 @@ function stringOptionConsumer(name: string) {
};
}

function booleanOptionConsumer(name: string) {
return (value: string, current: any) => {
const normalized = value.toLocaleLowerCase();
const enabled = normalized === "yes" || normalized === "1" || normalized === "true";
set(programOptions, name, enabled);
return value;
};
}

function consumeReporterOptions(optionsString?: string) {
if (!optionsString) {
return;
Expand Down Expand Up @@ -132,7 +142,7 @@ function collectGlobals(val: string) {
if (!val) {
throw new Error("global name cannot be empty");
}
globals.push(...val.split(','));
globals.push(...val.split(","));
}

function looksLikeUrl(val: string) {
Expand Down Expand Up @@ -166,7 +176,11 @@ commander
.option("--check-leaks <boolean>", "Check for global variable leaks")
.option("--globals <name1,name2...>", "list of allowed global variables", collectGlobals, [])
.option("-S, --save <globalName[:fileName]>", "save global `name` as JSON file", collectGlobalsToSave)
.option("-L, --capture-console-log <boolean>", "whether to capture console.log in browser context", true)
.option(
"-L, --capture-console-log <boolean>",
"whether to capture console.log in browser context",
booleanOptionConsumer("captureConsoleLog")
)
.option("-g, --grep <pattern>", "only run tests/suites that match pattern", stringOptionConsumer("grep"))
.version(version);

Expand Down
52 changes: 51 additions & 1 deletion test/MochaWebdriverRunner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { assert } from "chai";
import * as sinon from "sinon";

import * as Mocha from "mocha";
import * as fs from "fs";
import * as xpath from "xpath";
// import * as xmldom from 'xmldom';
const xmldom = require("xmldom");

import { runMochaWebDriverTest } from "../src/MochaWebDriverRunner";
import { RemoteRunnerOptions } from "../src/RemoteRunnerProtocol";
import { Options } from "../src/MochaRemoteRunner";

class NullReporter extends Mocha.reporters.Base {
Expand Down Expand Up @@ -131,5 +132,54 @@ browserConfigurations.forEach(entry => {
assert.equal(testResult.success, true);
});
});

describe("capture console logs supports", function() {
let sandbox: sinon.SinonSandbox;
const logLevels: (keyof Console)[] = ["log", "error"];
beforeEach(function() {
sandbox = sinon.createSandbox();
});
afterEach(function() {
sandbox.restore();
});

it("captures logs by default", async function() {
const spies = logLevels.map(name => sandbox.spy(console, name));
this.timeout(20000);
const testResult = await runMochaWebDriverTest(
capabilities,
"file://" + __dirname + "/sample-suite/log-capture.html",
{
reporter: NullReporter
}
);
assert.equal(testResult.success, true);
logLevels.forEach((name, i) => {
assert.isTrue(
spies[i].calledWith(`[browser] ${name}:`, `sample console.${name}`),
`expected 'sample console.${name}' message to be captured`
);
});
});
it("doesn't capture logs by if disabled", async function() {
this.timeout(20000);
const spies = logLevels.map(name => sandbox.spy(console, name));
const testResult = await runMochaWebDriverTest(
capabilities,
"file://" + __dirname + "/sample-suite/log-capture.html",
{
reporter: NullReporter,
captureConsoleLog: false
}
);
assert.equal(testResult.success, true);
logLevels.forEach((name, i) => {
assert.isFalse(
spies[i].calledWith(`[browser] ${name}:`, `sample console.${name}`),
`expected 'sample console.${name}' message to be captured`
);
});
});
});
});
});
36 changes: 36 additions & 0 deletions test/sample-suite/log-capture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Mocha Webdriver Tests</title>
<link href="../../node_modules/mocha/mocha.css" rel="stylesheet">
</head>

<body>
<div id="mocha"></div>
<script src="../../node_modules/mocha/mocha.js"></script>
<script src="../../node_modules/chai/chai.js"></script>
<script src="../../dist/mocha-webdriver-client.js"></script>
<script>
mocha.setup({
ui: "bdd"
});
</script>
<script>
describe("logging", function () {
it("sample logs", function () {
console.log("sample console.log");
console.error("sample console.error");
console.info("sample console.info");
console.debug("sample console.debug");
});
})
</script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
</body>

</html>
2 changes: 2 additions & 0 deletions test/sample-suite/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe("sample module", function () {
globalThis.someResult = Object.assign(globalThis.someResult || {}, {
test1: "result1"
})
console.log("sample console.log");
console.error("sample console.error");
});
it.skip("pending test", function () {
assert(true);
Expand Down

0 comments on commit 1e79353

Please sign in to comment.