Rayrun
← Back to Discord Forum

Is there a way to wait for `data-hydrated` flag on every page change globally?

I have this piece of code, to wait for my SSR page to be hydrated:

await page.locator("[data-hydrated]").waitFor()

It works well, but I don't want to repeat it on every single page change I have. So is there maybe a global hook after calling page.reload or page.goto etc.?

This thread is trying to answer question "Is there a way to wait for 'data-hydrated' flag on every page change globally?"

19 replies
insane_rookstar
insane_rookstar

You can put it in the POM

insane_rookstar
insane_rookstar

For example in the 'goto' method

if you're using fixtures then you can override page fixture and perhaps add page.on domcontentloaded listener there and then check for that element

@insane_rookstar: What do you mean with POM?

What do you mean with POM?

@skorp32: You mean like this: ```ts this.page.on("domcontentloaded", async () => { await this.page.locator("[data-hydrated]").waitFor() }) ``` Will it wait for the async promise?

You mean like this:

this.page.on("domcontentloaded", async () => {
      await this.page.locator("[data-hydrated]").waitFor()
    })

Will it wait for the async promise?

it should

but the error will be weird if it will fail to find that element

I think if it's not present, the app will just not work at all

insane_rookstar
insane_rookstar
@_oemer_: As far as I know it is impossible to put async/await in the event listener.

As far as I know it is impossible to put async/await in the event listener.

insane_rookstar
insane_rookstar
@_oemer_: Page Object Model

Page Object Model

@insane_rookstar: I have `page.on('response', async response => {}` listener, works well, so I think async/await should with other listeners as well

I have page.on('response', async response => {} listener, works well, so I think async/await should with other listeners as well

insane_rookstar
insane_rookstar

But if your approach works in this case, then it's good 😄

Putting it in a fixture with on("domcontentloaded" didn't work.

@insane_rookstar: I already did that actually and it works very well. However, right now we call the `page.reload` only on the playwright page (not a self written POM), since it's global operation and has nothing to do with a specific page. we only do `goto` on actual pages like, `profilePage.goto()`.

I already did that actually and it works very well. However, right now we call the page.reload only on the playwright page (not a self written POM), since it's global operation and has nothing to do with a specific page. we only do goto on actual pages like, profilePage.goto().

So for goto I have it fixed, but not for reload cases. I think have to somewaht create and ExtendedPage POM and wrap the playwright page in there, overriding the reload function.

insane_rookstar
insane_rookstar
@_oemer_: Yeah, this is a way to go. Like `profilePage.reload()`.

Yeah, this is a way to go. Like profilePage.reload().

@_oemer_: works for me 🤷‍♂️ also stops execution if element not found

works for me 🤷‍♂️ also stops execution if element not found

Related Discord Threads

TwitterGitHubLinkedIn
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.