Rayrun
← Back to Discord Forum

Pattern for waiting for an element with a reload in python?

I have an element the doesn't appear until after a bit of time and requires a reload to display. Is there a pattern in playwright/python for waiting for a locator while including a reload or do I need to create my own retry loop?

This thread is trying to answer question "Is there a pattern in playwright/python for waiting for a locator while including a reload or do I need to create my own retry loop?"

7 replies
refactoreric

Hi, I think Locators will wait automatically for the reload. Are you experiencing some issue?

The auto-waits built in to Playwright don't include reloads. I'm looking for an element in a list, but when the page loads initially, the element I'm looking isn't present. I have to give time for data created by a pytest fixture to appear in a DynamoDB and then be served up by the API. I need to reload the page until the element is actually displayed in the list.

refactoreric

Is it a manual reload then? From my experience, if you're waiting for an element (Locator), and you haven't arrived at the target page with that element yet, Playwright will wait across those navigations.

If the reload needs to be done manually, then building your own loop with a reload is necessary.

What you can also do is perhaps make an API call with page.request (in a loop), polling until the API returns the expected item. Then reload the page. In the JS version we have expect.poll for such things.

If I understand what you mean by a manual reload, yes it is: If I add data outside of the automation, the page doesn't update and display it without a reload.

Because I have two fixtures, one that creates the data and one that interacts with a Playwright page, there's a race condition between the data creation and the page requesting it. I've ended up using a rety loop (using https://pypi.org/project/retry/) and getting it to work, but I was hoping there was a more concise way of doing it.

you could chain the fixtures and make the playwright one expect the api response first. this is cheaper than reloading the frontend

That makes sense. Unfortunately I'm on vacation and can't try that approach until I return. I'll give it a shot when I get back and see, but if I recall correctly, I run into the same issue when I automate the UI to create the record in other tests - it's a shortcoming (arguably a bug) with the webpage, but the new item doesn't appear without a refresh and there's no other element I can check that would indicate the list is not up to date.

What makes the page reactive, apart from automating the test how would the page update? It sound like you have a static page that will never update until it is reloaded?

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 [email protected].