playwrightplaywright-java

How can I check if a response has already been received for Playwright?


Using Java, I'm trying to wait for a response to one of the Javascript scripts I'm waiting for.

I've already discovered that I can use waitForResponse, but this leads to issues if the script has already completed loading before I reach the waitForResponse statement.

Is there a way I could wait for the script to be completed but only if it hasn't been received yet?

        page.waitForResponse("**/*<<Script reference>>*.js", () -> { });

Any help would be greatly appreciated!


Solution

  • What you need to do is audit all traffic. Here you have some example:

    import com.microsoft.playwright.Browser;
    import com.microsoft.playwright.BrowserType;
    import com.microsoft.playwright.Page;
    import com.microsoft.playwright.Playwright;
    
    public class Pw {
            public static void main(String[] args) {
                try (Playwright playwright = Playwright.create()) {
                    BrowserType chromium = playwright.chromium();
                    Browser browser = chromium.launch();
                    Page page = browser.newPage();
                    page.onResponse(response -> System.out.println(response.status() + " " + response.url()));
                    page.navigate("https://playwright.dev/java/docs/network");
                    browser.close();
                }
        }
    }
    

    OUTPUT:

    200 https://playwright.dev/java/docs/network
    200 https://playwright.dev/java/assets/css/styles.57a5681f.css
    200 https://playwright.dev/java/js/redirection.js
    200 https://playwright.dev/java/assets/js/main.bdc3f5b7.js
    200 https://playwright.dev/java/assets/js/runtime~main.f37ee5cd.js
    200 https://playwright.dev/java/img/playwright-logo.svg
    200 https://playwright.dev/java/assets/js/04005ac5.4bff4e91.js
    

    This way you will not miss your request.
    I hope I have been able to help you.