Skip to content

Commit fde5d4c

Browse files
Merge pull request #165 from devoxa/console-messages
Add captureConsoleMessages helper
2 parents 6609052 + 79ba62a commit fde5d4c

17 files changed

Lines changed: 139 additions & 0 deletions

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ await removeLocalStorageItem(page, key)
120120
121121
Remove a local storage item.
122122
123+
### Console Messages
124+
125+
```ts
126+
const getConsoleMessages = captureConsoleMessages(page)
127+
// ... do things here ...
128+
expect(getConsoleMessages()).toEqual([])
129+
expect(getConsoleMessages(['warning', 'error'])).toEqual([])
130+
```
131+
132+
Capture the console messages of the page for later assertions.
133+
123134
### Matchers
124135
125136
> **Note:** These functions escape special regular expression characters, so they are safe to use
@@ -213,6 +224,9 @@ WaitForNetworkIdleDefaultOptions.minIdleTime = 500
213224
214225
Set the global default options for all `*waitForNetworkIdle` functions.
215226
227+
Keep in mind that test files run independently from the setup, so you have to call this in a helper,
228+
that is imported into all test files.
229+
216230
## Contributors
217231
218232
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

src/captureConsoleMessages.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ConsoleMessage, Page } from '@playwright/test'
2+
3+
const CAPTURED_TYPES = ['debug', 'log', 'info', 'warning', 'error'] as const
4+
type CapturedConsoleMessageType = (typeof CAPTURED_TYPES)[number]
5+
6+
export interface CapturedConsoleMessage {
7+
/** The type of the captured console message (e.g. "info" or "warning"). */
8+
type: CapturedConsoleMessageType
9+
10+
/** The list of arguments passed to the console function call. */
11+
args: Array<unknown>
12+
}
13+
14+
/** Capture the console messages of the page for later assertions. */
15+
export function captureConsoleMessages(page: Page) {
16+
const messages: Array<CapturedConsoleMessage> = []
17+
18+
async function onConsole(message: ConsoleMessage) {
19+
const type = message.type() as CapturedConsoleMessageType
20+
if (!CAPTURED_TYPES.includes(type)) return
21+
22+
const args = []
23+
for (const arg of message.args()) {
24+
args.push(await arg.jsonValue())
25+
}
26+
27+
messages.push({ type, args })
28+
}
29+
30+
page.on('console', onConsole)
31+
32+
return (types?: Array<CapturedConsoleMessageType>) => {
33+
if (!types) return messages
34+
return messages.filter((message) => types.includes(message.type))
35+
}
36+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './matchers'
77
export * from './performanceMetrics'
88
export * from './retryOnError'
99
export * from './waitForNetworkIdle'
10+
export * from './captureConsoleMessages'

test-examples/static/accessible.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!doctype html>
12
<html lang="en">
23
<head>
34
<title>Hello world</title>

test-examples/static/async-render.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!doctype html>
12
<html>
23
<body>
34
<div id="foo">Bar</div>

test-examples/static/copy-to-clipboard.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!doctype html>
12
<html>
23
<body>
34
<button onclick="copyToClipboard('Hello World')">Copy</button>

test-examples/static/de54bb36-b1c1-4028-8cbe-57fa24f99622/edit.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!doctype html>
12
<html>
23
<body>
34
<h1>This is an example for testing.</h1>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<script>
5+
function sleep(ms) {
6+
return new Promise((resolve) => setTimeout(resolve, ms))
7+
}
8+
9+
async function main() {
10+
console.log('0')
11+
12+
await sleep(500)
13+
14+
console.log('1')
15+
console.warn('2')
16+
console.error('3')
17+
console.debug('4', { foo: { bar: 'baz', boz: 12 } })
18+
19+
document.body.innerHTML = '<h1>Done</h1>'
20+
}
21+
22+
main()
23+
</script>
24+
</body>
25+
</html>

test-examples/static/delayed-fast-requests.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!doctype html>
12
<html>
23
<body>
34
<script>

test-examples/static/delayed-slow-requests.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!doctype html>
12
<html>
23
<body>
34
<script>

0 commit comments

Comments
 (0)