-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndexBehaviourTest.java
102 lines (88 loc) · 3.92 KB
/
IndexBehaviourTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.example;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.assertj.core.api.Assertions.assertThat;
import static org.example.IndexTemplateTest.parseHtml;
import static org.example.IndexTemplateTest.renderTemplate;
import static org.junit.jupiter.api.Assertions.fail;
public class IndexBehaviourTest {
static Playwright playwright;
static Browser browser;
@BeforeAll
static void launchBrowser() {
playwright = Playwright.create();
browser = playwright.chromium().launch();
}
@AfterAll
static void closeBrowser() {
playwright.close();
}
private void logActivity(Page page) {
page.onRequest(request -> System.out.printf(">> %s %s%n", request.method(), request.url()));
page.onResponse(response -> System.out.printf("<< %s %s%n", response.status(), response.url()));
page.onLoad(page1 -> System.out.println("Loaded: " + page1.url()));
page.onConsoleMessage(consoleMessage -> System.out.println("! " + consoleMessage.text()));
}
@Test
void toggleTodoItem() {
// Render our initial html
TodoList model = new TodoList()
.add(101, "One")
.add(102, "Two");
String initialHtml = renderTemplate("/index.tmpl", model, "/");
try (Page page = browser.newPage()) {
logActivity(page);
// stub network calls
page.route("**", route -> {
if (route.request().url().equals("http://localhost:4567/index.html")) {
// serve the initial HTML
route.fulfill(new Route.FulfillOptions()
.setContentType("text/html")
.setBody(initialHtml));
} else if (route.request().url().equals("http://localhost:4567/toggle/101") && route.request().method().equals("POST")) {
// we expect that a POST /toggle/101 request is made when we click on the "One" checkbox
String stubbedHtml = """
<section class="todoapp">
<p>Stubbed html</p>
</section>
""";
route.fulfill(new Route.FulfillOptions()
.setContentType("text/html")
.setBody(stubbedHtml));
} else if (route.request().url().equals("https://unpkg.com/htmx.org@1.9.12")) {
// serve the htmx library
route.fulfill(new Route.FulfillOptions()
.setContentType("text/html")
.setBody(readFile("/htmx.min.js")));
} else {
// we don't want unexpected calls
fail(String.format("Unexpected request: %s %s", route.request().method(), route.request().url()));
}
});
// load initial html
page.navigate("http://localhost:4567/index.html");
// click on the "One" checkbox
var checkbox = page.getByRole(AriaRole.CHECKBOX, new Page.GetByRoleOptions().setName("One"));
checkbox.click();
// check that the page has been updated
var document = parseHtml(page.content());
var elements = document.select("body > section.todoapp > p");
assertThat(elements.text())
.describedAs(page.content())
.isEqualTo("Stubbed html");
}
}
private String readFile(String fileName) {
try {
return Files.readString(Paths.get("src/test/resources", fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}