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?"
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.
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
use as fixture: https://gist.github.com/Tallyb/22188ea6dfa980ce3c03262e80294ffc
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:
--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.
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 firstname.lastname@example.org.