forked from MarcusFelling/demo.playwright
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.spec.ts
66 lines (61 loc) · 1.86 KB
/
example.spec.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {test, chromium} from '@playwright/test';
import {playAudit} from 'playwright-lighthouse';
test.describe.parallel('web performance tests', () => {
/**
* In this test we use request.timing()
* to to return timing information about the request
* @see https://playwright.dev/docs/api/class-request#request-timing
*/
test('Get resource timing of request', async ({page}) => {
const [request] = await Promise.all([
page.waitForEvent('requestfinished'),
page.goto(''),
]);
console.log(request.timing());
});
/**
* In this test we start CDPSession to talk to DevTools
* and a simulate a slow network connection
* @see https://playwright.dev/docs/api/class-cdpsession
*/
test('Simulate slow network connection', async ({page}) => {
const client = await page.context().newCDPSession(page);
await client.send('Network.enable');
await client.send('Network.emulateNetworkConditions', {
offline: false,
downloadThroughput: (2 * 1024 * 1024) / 4,
uploadThroughput: (3 * 1024 * 1024) / 4,
connectionType: 'cellular2g',
latency: 10,
});
await page.goto('');
});
/**
* In this test we use playwright-lighhouse package
* to audit performance of the page
* @see https://www.npmjs.com/package/playwright-lighthouse
*/
test('Run Lighthouse Audit', async () => {
const browser = await chromium.launch({
headless: true,
args: ['--remote-debugging-port=9222'],
});
const page = await browser.newPage();
await page.goto('');
await playAudit({
page: page,
port: 9222,
thresholds: {
performance: 90,
},
reports: {
formats: {
html: true,
},
name: `lighthouse-${new Date().getTime()}`,
directory: `${process.cwd()}/lighthouse`,
},
});
await browser.close();
});
});