-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Playwright: get console logs without trace (#28972)
* Playwright: get console logs without trace Signed-off-by: Michael Telatynski <[email protected]> * Add page url to log Signed-off-by: Michael Telatynski <[email protected]> * Skip empty logs Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> * Reset page counter Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
- Loading branch information
Showing
3 changed files
with
68 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import { BrowserContext, Page, TestInfo } from "@playwright/test"; | ||
import { Readable } from "stream"; | ||
import stripAnsi from "strip-ansi"; | ||
|
||
export class Logger { | ||
private pages: Page[] = []; | ||
private logs: Record<string, string> = {}; | ||
|
||
public getConsumer(container: string) { | ||
this.logs[container] = ""; | ||
return (stream: Readable) => { | ||
stream.on("data", (chunk) => { | ||
this.logs[container] += chunk.toString(); | ||
}); | ||
stream.on("err", (chunk) => { | ||
this.logs[container] += "ERR " + chunk.toString(); | ||
}); | ||
}; | ||
} | ||
|
||
public async onTestStarted(context: BrowserContext) { | ||
this.pages = []; | ||
for (const id in this.logs) { | ||
if (id.startsWith("page-")) { | ||
delete this.logs[id]; | ||
} else { | ||
this.logs[id] = ""; | ||
} | ||
} | ||
|
||
context.on("console", (msg) => { | ||
const page = msg.page(); | ||
let pageIdx = this.pages.indexOf(page); | ||
if (pageIdx === -1) { | ||
this.pages.push(page); | ||
pageIdx = this.pages.length - 1; | ||
this.logs[`page-${pageIdx}`] = `Console logs for page with URL: ${page.url()}\n\n`; | ||
} | ||
const type = msg.type(); | ||
const text = msg.text(); | ||
this.logs[`page-${pageIdx}`] += `${type}: ${text}\n`; | ||
}); | ||
} | ||
|
||
public async onTestFinished(testInfo: TestInfo) { | ||
if (testInfo.status !== "passed") { | ||
for (const id in this.logs) { | ||
if (!this.logs[id]) continue; | ||
await testInfo.attach(id, { | ||
body: stripAnsi(this.logs[id]), | ||
contentType: "text/plain", | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.