Testing

Testing an Endpoint that is returning HTML

Let's assume a request to the URL http://localhost:8080/example will respond with the following markup:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>i'm the title</title>
    </head>
    <body>
        i'm the body
        <h1>i'm the headline</h1>
        <p>i'm a paragraph</p>
        <p>i'm a second paragraph</p>
    </body>
</html>

What we want to do is to call the URL with a HTTP-Request GET and verify certain assumptions in a defined html-structure. Because Skrape{it} has no direct bindings to a certain test-runner or library it works seamlesly with all different Test-Runners. Just to give you a clue how a typical test that is using the Skrape{it} DSL would look like you can find multiple examples of the same test scenario here:

class ExampleTest {

    @Test
    internal fun `can parse and verify response of example endpoint`() {
        skrape {
            url = "http://localhost:8080/example"
            expect {
                status { 
                    code toBe 200
                    message toBe "OK"
                }
                contentType toBe ContentTypes.TEXT_HTML_UTF8

                htmlDocument {
                    p {
                        findFirst {
                            text toBe "i'm a paragraph"
                        }
                        findAll {
                            size toBe 2
                            toBePresentExactlyTwice // shorthand
                        }
                    }
                }
            }
        }
    }
}

Last updated