Skip to content

WebDriver

Installation (Gradle)

compile group: "org.http4k", name: "http4k-testing-webdriver", version: "3.251.0"

About

A basic Selenium WebDriver API implementation for http4k HttpHandlers, which runs completely out of container (no network) for ultra fast tests.

Feature Supported Notes
Navigation yes simple back/forward/refresh history
CSS selectors yes
Link navigation yes
Form field entry and submission yes
Cookie storage yes manual expiry management
JavaScript no
Alerts no
Screenshots no
Frames no
Multiple windows no

Use the API like any other WebDriver implementation, by simply passing your app HttpHandler to construct it:

Code

package guide.modules.webdriver

import org.http4k.core.Method.GET
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.webdriver.Http4kWebDriver
import org.openqa.selenium.By

fun main() {
    val app = routes(
        "/hello" bind GET to {
            Response(OK).body("<html><title>hello</title></html>")
        }
    )

    val driver = Http4kWebDriver(app)

    driver.navigate().to("http://localhost:10000/hello")

    println(driver.title)
    println(driver.findElement(By.tagName("title")))

// prints:
//
// hello
// JSoupWebElement(element=<title>hello</title>)
}