spectron
spectron copied to clipboard
How to get console logs of Electron main process when testing with Spectron ?
Hello,
I'm trying to get the console logs of Electron's main process when testing with Spectron via console.log(app.client.getMainProcessLogs());
, but that does not return the correct console logs.
How can I retrieve them ?
Thank you
Running into the same problem, getMainProcessLogs
sometimes has renderer
logs in it. And when it does, those log lines are not in getRendererProcessLogs
.
Also, there isn't a documented way to get the logs without clearing them (which nullifies integration tests that want to wait for specific log lines to occur).
A work around that I have found (though the data is still needs a bit of parsing to get more usable info out of it). It the following:
const app: Application; // spectron app instance
const source: "main" | "renderer";
const rendererLogPrefixMatcher = /^\[[0-9]{5}:[0-9]{4}\/[0-9]{6}\.[0-9]{6}:[A-Z]+:CONSOLE\([0-9)]+\)\]/;
const logs = (app as any).chromeDriver.getLogs() as string[]; // access a private field
const filteredLogs = logs.filter(logLine => (source === "main") !== Boolean(logLine.match(rendererLogPrefixMatcher)));