-
Notifications
You must be signed in to change notification settings - Fork 0
/
playwright.extend.ts
49 lines (43 loc) · 1.15 KB
/
playwright.extend.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { test as base, expect } from '@playwright/test'
interface Fixtures {
useFakeTimers(date: Date): void
consoleMessages: Map<string, Array<string>>
}
const test = base.extend<Fixtures>({
async useFakeTimers({ page }, use) {
await use(async (date) => {
const fakeNow = date.valueOf()
await page.addInitScript(`{
// Extend Date constructor to default to fakeNow
Date = class extends Date {
constructor(...args) {
if (args.length === 0) {
super(${fakeNow});
} else {
super(...args);
}
}
}
const __DateNowOffset = ${fakeNow} - Date.now();
const __DateNow = Date.now;
Date.now = () => __DateNow() + __DateNowOffset;
}`)
})
},
async consoleMessages({ page }, use) {
const messages = new Map()
page.on('console', (message) => {
const prevMessages = messages.get(message.type()) || []
const text = message
.text()
.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
''
)
messages.set(message.type(), prevMessages.concat(text))
})
await use(messages)
messages.clear()
},
})
export { test, expect }