<https://youtu.be/VJh-xj225XM> Extending Playwrigh...
# http4k
d

https://youtu.be/VJh-xj225XM

Extending Playwright with Kotlin, with quite a nice runPlaywriteAgainstAnHttp4kRoute function if I say so myself
http4k 5
d
@dmcg your (very neat) function inspired me to add an extension module to http4k which will make sparking up a playwright much easier using a JUnit extension. The general idea is that the extension will start and stop the browser, and also to provide a custom Browser which automatically sets the base URL for testing:
Copy code
class RunWithPlaywrightTest {

    private val app = routes(
        "/foo" bind GET to { _: Request -> Response(OK).body("foo") },
        "/" bind GET to { _: Request -> Response(OK).body("helloworld") }
    )

    @RegisterExtension
    val playwright = RunWithPlaywright(app)

    @Test
    fun `provides http4k browser`(browser: Http4kBrowser) {
        with(browser.newPage()) {
            assertThat(String(navigateHome().body()), equalTo("helloworld"))
            assertThat(String(navigate("/foo").body()), equalTo("foo"))
            assertThat(String(navigate("<http://google.com>").body()), containsSubstring("google"))
        }
    }
}
Would love to incorporate any feedback or things you think are useful into it (or even if it has some massive flaw and is a terrible idea!) before we pull the trigger. 🙂
d
The base URL is a neat trick. Is the playwright property just there to hang the extension on? It could just be an @ExtendWith otherwise.
Despite your flattery, I hesitate to put my stamp of approval on the lifecycle aspects just yet, as I’m really just finding my feet with what shape the tests should be. Is there some way to configure the browser injected - it’s brand, visibility, page injection? It would be really nice for example to have the htmx javascript extensions injected into new page on navigation by click, not just initial page load. I think I’m going to be writing some Playwright tests for another project soon. Will try the extensions and let you know how it goes.
d
@dmcg If you check the source of the PR you can see the options are passed to the constructor of the extension. So yes that's possible. It's also the reason that it's a field - so we can inject the app and create/start the server automatically. I'll merge the PR and release it today and we can go from there. 🙃
👯‍♂️ 1