spectron icon indicating copy to clipboard operation
spectron copied to clipboard

How to get console logs of Electron main process when testing with Spectron ?

Open petef19 opened this issue 3 years ago • 2 comments

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

petef19 avatar Dec 06 '20 07:12 petef19

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).

Nokel81 avatar Jan 25 '21 20:01 Nokel81

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)));

Nokel81 avatar Jan 25 '21 21:01 Nokel81