skrape{it}
Search…
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:

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<title>i'm the title</title>
5
</head>
6
<body>
7
i'm the body
8
<h1>i'm the headline</h1>
9
<p>i'm a paragraph</p>
10
<p>i'm a second paragraph</p>
11
</body>
12
</html>
Copied!

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:

jUnit 5
jUnit 5 & assertJ
KotlinTest
Spek
Spek & assertJ
1
class ExampleTest {
2
3
@Test
4
internal fun `can parse and verify response of example endpoint`() {
5
skrape {
6
url = "http://localhost:8080/example"
7
expect {
8
status {
9
code toBe 200
10
message toBe "OK"
11
}
12
contentType toBe ContentTypes.TEXT_HTML_UTF8
13
14
htmlDocument {
15
p {
16
findFirst {
17
text toBe "i'm a paragraph"
18
}
19
findAll {
20
size toBe 2
21
toBePresentExactlyTwice // shorthand
22
}
23
}
24
}
25
}
26
}
27
}
28
}
Copied!
1
class ExampleTest {
2
3
@Test
4
internal fun `can parse and verify response of example endpoint`() {
5
skrape {
6
url = "http://localhost:8080/example"
7
expect {
8
status {
9
assertThat(code).isEqualTo(200)
10
assertThat(message).isEqualTo("OK")
11
}
12
assertThat(contentType).isEqualTo("text/html; charset=UTF-8")
13
14
htmlDocument {
15
p {
16
findFirst {
17
assertThat(text()).isEqualTo("i'm a paragraph")
18
}
19
findAll {
20
assertThat(size).isEqualTo(2)
21
}
22
}
23
}
24
}
25
}
26
}
27
}
Copied!
1
class ExampleTest : StringSpec({
2
3
"can parse and verify response of example endpoint" {
4
skrape {
5
url = "http://localhost:8080/example"
6
expect {
7
status {
8
code shouldBe 200
9
message shouldBe "OK"
10
}
11
contentType shouldBe TEXT_HTML_UTF8
12
13
htmlDocument {
14
p {
15
findFirst {
16
text toBe "i'm a paragraph"
17
}
18
findAll {
19
size toBe 2
20
}
21
}
22
}
23
}
24
}
25
}
26
})
Copied!
1
object ExampleSpec: Spek({
2
describe("example endpoint") {
3
it("returns expected markup") {
4
skrape {
5
url = "http://localhost:8080/example"
6
expect {
7
status {
8
code toBe 200
9
message toBe "OK"
10
}
11
contentType toBe TEXT_HTML_UTF8
12
13
htmlDocument {
14
p {
15
findFirst {
16
text toBe "i'm a paragraph"
17
}
18
findAll {
19
size toBe 2
20
}
21
}
22
}
23
}
24
}
25
}
26
}
27
})
Copied!
1
object ExampleSpec: Spek({
2
describe("example endpoint") {
3
it("returns expected markup") {
4
skrape {
5
url = "http://localhost:8080/example"
6
expect {
7
status {
8
assertThat(code).isEqualTo(200)
9
assertThat(message).isEqualTo("OK")
10
}
11
assertThat(contentType).isEqualTo("text/html; charset=UTF-8")
12
13
htmlDocument {
14
p {
15
findFirst {
16
text toBe "i'm a paragraph"
17
}
18
findAll {
19
size toBe 2
20
}
21
}
22
}
23
}
24
}
25
}
26
}
27
})
Copied!
Last modified 1yr ago
Copy link