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?"
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.
"""
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
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
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].