← Back to Discord Forum

Configuring Playwright to use a specific datetime

I'm using Playwright via the JavaScript library and using it to do screenshot based testing of my UI.

The UI has graphs involved, and the data for those graphs comes from a mock backend started with the webServer option that serves a bunch of fake static content for the UI testing. Included in that data is a timestamps for the graph data. However, the data is queried from the backend using the browser's time, and given that the data is captured way in the past, this messes with what ends up being shown in the browser.

Is there a way to configure playwright to make it think that the time is set to a specific timestamp so that the data for the graphs renders properly all the time?

This thread is trying to answer question "Is there a way to configure playwright to make it think that the time is set to a specific timestamp so that the data for the graphs renders properly all the time?"

4 replies

I was thinking I could get the backend data by normalizing the timestamps sent it to be based on a hardcoded timestamp, and then use masks in the screenshots to exclude the actual timestamps from the graphs' legends, but that wouldn't be possible since the timestamps are rendered in a canvas and I can't locate them. I also don't want to mask the whole canvas because I need to do some assertions based on screenshots of it. I could potentially include maxPixelDiffs to account for different timestamps, but that seems imprecise and I worry might lead to missing real issues.

Ahh, I see that there's a way to mock browser APIs. Maybe I can use that to mock the Date API to return a hardcoded current time...

Looks I should be able to use https://www.npmjs.com/package/mockdate to mock the date for the Date API to use something hardcoded. I'll give that a shot


Sure, you can make Playwright think it's a specific time. This is handy when you want your graph data to render consistently during tests.

Here's how you do it:

Use the --timezone option when you run your Playwright codegen or tests. This lets you set a specific timezone, which affects how Playwright handles timestamps.

For instance, if you want to set the time to "2022-01-01T12:00:00" in the "Europe/Rome" timezone, you'd use this command:

npx playwright codegen --timezone="Europe/Rome"

With the timezone set to "Europe/Rome", Playwright treats any timestamps in your tests or generated code as if they were in that timezone. This means graphs or other timestamp-dependent components will render based on the specified timestamp.

Remember, this only affects how Playwright interprets time in your tests or generated code. It doesn't change your machine's system time or affect other applications running outside of Playwright.

By controlling and manipulating timestamps in your tests, you can ensure consistent rendering of graph data. This leads to more reliable and predictable testing scenarios when time-sensitive components are involved.

In short, using the --timezone option to set a specific timezone in Playwright lets you control how timestamps are interpreted in your tests or generated code. This helps ensure your graph data renders properly during testing.

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.