Skip to content

Instantly share code, notes, and snippets.

@up1
Created June 28, 2024 16:21
Show Gist options
  • Save up1/37e5cb5a46ece32150616a636517f386 to your computer and use it in GitHub Desktop.
Save up1/37e5cb5a46ece32150616a636517f386 to your computer and use it in GitHub Desktop.
Playwright + Clock API
// Initialize clock and let the page load naturally.
await page.clock.install({ time: new Date('2024-02-02T08:00:00') });
await page.goto('http://localhost:3333');
// Pretend that the user closed the laptop lid and opened it again at 10am,
// Pause the time once reached that point.
await page.clock.pauseAt(new Date('2024-02-02T10:00:00'));
// Assert the page state.
await expect(page.getByTestId('current-time')).toHaveText('2/2/2024, 10:00:00 AM');
// Close the laptop lid again and open it at 10:30am.
await page.clock.fastForward('30:00');
await expect(page.getByTestId('current-time')).toHaveText('2/2/2024, 10:30:00 AM');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment