Skip to content

Commit

Permalink
Playwright v1.45 makes you a Time Wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Jun 25, 2024
1 parent 7785d2a commit 9037a48
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
47 changes: 47 additions & 0 deletions bits/playwright-v145-makes-you-a-time-wizard/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: Playwright v1.45 makes you a Time Wizard
slug: playwright-v145-makes-you-a-time-wizard
date: 2024-06-25
tags: Playwright, testing
---

# Playwright v1.45 makes you a Time Wizard

The latest release of Playwright, version 1.45, introduces a new feature called `Clock`. This feature allows you to manipulate the time in your tests, making it easier to test time-sensitive scenarios.

```ts
import { test, expect } from '@playwright/test';

test('🧙‍♂️ Time wizard plays with time 🪄', async ({ page }) => {
// Initialize clock and let the page load naturally.
await page.clock.install({ time: new Date('2024-06-25, 08:00') });
await page.goto('http://localhost:4200');

await expect(page.getByTestId('current-time')).toHaveText('25/06/2024, 08:00');

// Set a fixed time (Date.now and new Date() return fixed fake time at all times)
await page.clock.setSystemTime('2024-06-25T09:45:00');
await expect(page.getByTestId('current-time')).toHaveText('25/06/2024, 09:45');

// 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-06-25T10:00:00'));
await expect(page.getByTestId('current-time')).toHaveText('25/06/2024, 10:00');

// Close the laptop lid again and open it at 10:30am.
await page.clock.fastForward('30:00');
await expect(page.getByTestId('current-time')).toHaveText('25/06/2024, 10:30');

// Resume the time to normal flow
await page.clock.resume();

// Fake Date value while keeping the timers going
await page.clock.setFixedTime('2024-06-25T16:00:00');
await expect(page.getByTestId('current-time')).toHaveText('25/06/2024, 16:00');
});
```

## Additional Resources

- [`Clock` Documentation](https://playwright.dev/docs/clock)
- [v1.45.0 Release Notes](https://github.com/microsoft/playwright/releases/tag/v1.45.0)
42 changes: 36 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"devDependencies": {
"@azure/static-web-apps-cli": "1.1.9",
"@playwright/test": "1.41.2",
"@playwright/test": "1.45.0",
"@squoosh/lib": "0.5.3",
"@sveltejs/adapter-auto": "3.2.2",
"@sveltejs/adapter-node": "4.0.1",
Expand Down

0 comments on commit 9037a48

Please sign in to comment.