javaplaywrightplaywright-java

onResponse handler not getting called


I've been evaluating playwright-java for a while and I've encountered an issue with regard to onResponse handler. It's simply not getting called. Documentation doesn't mention any special setup for it, that is besides registering handler.

Here is my sample code.

import com.microsoft.playwright.*
import java.nio.file.Paths
import java.util.function.Consumer
fun main() {
    Playwright.create().use { playwright ->
        val browser: Browser = playwright.firefox().launch( )
        val page: Page = browser.newPage()
        page.onRequest(Consumer { request: Request? ->
            println(">> " + request!!.method() + " " + request.url())
        })
        page.onResponse(Consumer { response: Response? -> {
            println("<< RESPONSE " + response!!.status() + " " + response.url())
        }})
        page.onRequestFinished(Consumer { request: Request? ->
            println(">> FINISHED " + request!!.method() + " " + request.url())
        })
        page.navigate("https://www.york.ac.uk/teaching/cws/wws/webpage1.html")
        page.screenshot(Page.ScreenshotOptions().setPath(Paths.get("example.png")))
    }
}

onRequest and onRequestFinished handlers are being called, but onResponse does not.

GET https://www.york.ac.uk/teaching/cws/wws/webpage1.html
FINISHED GET https://www.york.ac.uk/teaching/cws/wws/webpage1.html

I've tried all supported browsers & a few different versions of playwright with same result, onResponse handler still not getting called.


Solution

  • Remove extra brackets

    page.onResponse(Consumer { response: Response? ->
        println("<< RESPONSE " + response!!.status() + " " + response.url())
    })