I’m trying to wait until text appears on certain element. My page is fully loaded but on one screen there is something like a "buffering", informations on the element appears later, I don’t want to use page.waitForTimeout because it is "luck" depending, what if my timeout is too small etc. I want to wait for text to appear
My current state is
await expect(page.locator('my-locator')).toContainText('a')
It is working pretty well because letter "a" is in basically every single world and I want to find a solution to just wait for text to appears, nothing else, I just want my page to wait until that one element which is "buffering" shows fully, but I know there is a solution for it which I don’t know about, looking for help!
2
Answers
I think you want
page.waitForFunction()
(docs). It’s great for nuanced criteria like this one.If I understand correctly, you need to wait for the downloading/buffering panel to complete before you action you next click, so we can do this a few ways.
This will find the buffering element, whatever that is for your site, and then wait until it disappears, i.e. loading complete. You can easily extend that to look for specific text such as ‘loading’ or whatever you need. As I don’t know your HTML, I am keeping it generic here, but the logic is the same.
If you wanted to conditionally check for it, its very similar. Something like:
This code block creates a promise (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then you should replace the ‘getByRole’ line with the action that performs the load, which is presumably just opening the page under test. Then, we resolve the promise, at which point the response is returned and buffering complete, then you can move on.