Rayrun
← Back to Discord Forum

Waiting for certain network calls to finish before taking action on the page

Hi I am using the playwright-python sync API for my test suite. The suite loads a URL and does screenshot matching. This matching has turned out to be really flaky, and I have the issue narrowed down to some background network calls not finishing sometimes. When I implemented this using page.expect_response, it was not able to loop through a set of network calls and wait for them. Also this part of the code was being called after page.goto(). Has anyone implemented something like this and want to share?

This thread is trying to answer question "How can I ensure that all network calls finish before taking a screenshot in playwright-python?"

8 replies
dirvinautomation_16636

Hey Ethen! This mission is not impossible. I'm not as familiar with the python flavor of P-wright, but I do know the JS version well.

What I would (and have) done is to make a web-first assertion before taking the screenshot. That is, wait until the last item that loads into the page is loaded by asserting its visibility before taking the screenshot.

Modern JS frameworks are inherently asynchronous, and using networkidle is not recommended. The issue is this - if the network call succeeds, it shifts the layout of the entire page down enough to trigger screenshot mismatch failure. There is no web first thing to assert on.

@ethenhunt: """ When I implemented this using page.expect_response, it was not able to loop through a set of network calls and wait for them. """ How does that implementation using `expect_response` look?

""" When I implemented this using page.expect_response, it was not able to loop through a set of network calls and wait for them. """ How does that implementation using expect_response look?

Whenever I have to do screenshot comparisons, I'm using routeFromHar and updating the har file when updating the screenshot, you can give it a try. https://playwright.dev/python/docs/api/class-browsercontext#browser-context-route-from-har

@k00kl33: if wait_for_response: for network_call in wait_for_response: try: url = network_call + ".*" pattern = re.compile(rf"{url}") with page.expect_response(pattern, timeout=40000) as response_info: print(f"INFO: {network_call}: has received response " + str(response_info.value.status) + " for " + str(response_info.value.url)) except Exception as e: print(f"FAIL: {network_call}: has NOT received a response.") raise e

if wait_for_response: for network_call in wait_for_response: try: url = network_call + ".*" pattern = re.compile(rf"{url}") with page.expect_response(pattern, timeout=40000) as response_info: print(f"INFO: {network_call}: has received response " + str(response_info.value.status) + " for " + str(response_info.value.url)) except Exception as e: print(f"FAIL: {network_call}: has NOT received a response.") raise e

What do you mean by "is not able to loop?" Does the loop throw an error, or does the loop complete without waiting?

dirvinautomation_16636
@ethenhunt: So the content shifts but there is no element that is added or removed that causes the content shift?

So the content shifts but there is no element that is added or removed that causes the content shift?

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.