Skip to content

Instantly share code, notes, and snippets.

@MarcusFelling
Created April 27, 2023 13:45
Show Gist options
  • Save MarcusFelling/88f8ddde9941ec1cef19667892dbe2d0 to your computer and use it in GitHub Desktop.
Save MarcusFelling/88f8ddde9941ec1cef19667892dbe2d0 to your computer and use it in GitHub Desktop.
import { test, expect, Page, TestInfo } from '@playwright/test';
test('performance test example', async ({ page }, TestInfo) => {
// Perform actions
await page.goto(`/`);
// Measure performance
await measurePerformance(page, TestInfo);
});
async function measurePerformance(page: Page, TestInfo: TestInfo) {
// Use Performance API to measure performance
// https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntriesByType
const [performanceTiming] = await page.evaluate(() => {
const [timing] = performance.getEntriesByType('navigation');
return [timing];
});
// Get the start to load event end time
const startToLoadEventEnd = performanceTiming.loadEventEnd - performanceTiming.startTime;
// Add the performance annotation to the HTML report
test.info().annotations.push({ type: 'Performance', description: `"${TestInfo.project.name}" - Navigation start to load event end: ${startToLoadEventEnd}ms` });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment