Skip to content

Commit ae6bd2f

Browse files
committed
Complete docs, fix tests
1 parent 4f6a02e commit ae6bd2f

16 files changed

Lines changed: 556 additions & 495 deletions

File tree

framework-docs/modules/ROOT/nav.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@
382382
**** xref:testing/mockmvc/htmlunit/webdriver.adoc[]
383383
**** xref:testing/mockmvc/htmlunit/geb.adoc[]
384384
*** xref:testing/mockmvc/playwright.adoc[]
385+
**** xref:testing/mockmvc/playwright/setup.adoc[]
386+
**** xref:testing/mockmvc/playwright/testing.adoc[]
385387
*** xref:testing/mockmvc/vs-end-to-end-integration-tests.adoc[]
386388
*** xref:testing/mockmvc/resources.adoc[]
387389
** xref:testing/spring-mvc-test-client.adoc[]
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[[mockmvc-server-playwright]]
1+
[[playwright]]
22
= Playwright Integration
33
:page-section-summary-toc: 1
44

@@ -7,19 +7,3 @@ https://playwright.dev/[Playwright] via `MockMvcPlaywrightHandler`.
77
This allows browser-style end-to-end testing against your MVC controllers without
88
starting a server.
99

10-
== Setup
11-
12-
First, make sure that you have included a test dependency on `com.microsoft.playwright:playwright`.
13-
To use it, create a`MockMvcPlaywrightHandler` instance and register it as a
14-
route https://playwright.dev/java/docs/network#handle-requests[handler] for http requests.
15-
16-
include-code::./MockMvcPlaywrightHandlerTests[tag=init,indent=0]
17-
18-
include-code::./MockMvcPlaywrightHandlerTests[tag=setup,indent=0]
19-
20-
Browser http requests, such as form submission and file uploads, are then translated
21-
to MockMvc requests.
22-
23-
Don't forget to close playwright resources after your tests
24-
25-
include-code::./MockMvcPlaywrightHandlerTests[tag=close,indent=0]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[[mockmvc-playwright-setup]]
2+
= Playwright Setup
3+
4+
To integrate playwright with MockMvc, first make sure that you have included a test dependency on `com.microsoft.playwright:playwright`.
5+
To use it, create a `MockMvcPlaywrightHandler` instance and register it as a
6+
route https://playwright.dev/java/docs/network#handle-requests[handler] for http requests.
7+
8+
include-code::./MockMvcPlaywrightHandlerTests[tag=init,indent=0]
9+
10+
include-code::./MockMvcPlaywrightHandlerTests[tag=setup,indent=0]
11+
12+
Browser http requests, such as form submission and file uploads, are then translated
13+
to MockMvc requests.
14+
15+
Remember to close playwright resources after your tests
16+
17+
include-code::./MockMvcPlaywrightHandlerTests[tag=close,indent=0]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[[mockmvc-playwright-testing]]
2+
= Testing Fullstack Web Applications with Playwright and Mockmvc
3+
4+
With Playwright and Mockmvc in place, you can simulate user interactions with the browser
5+
and assert the expected changes without mocking the API calls. The following examples cover simple form
6+
submission and file upload.
7+
8+
NOTE: We use `com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat` for assertions.
9+
10+
[[mockmvc-playwright-testing-get]]
11+
== Simple GET form
12+
13+
Having an HTML page with a GET form
14+
15+
[source,html]
16+
----
17+
<form id="getForm" data-testid="getForm" method="get" action="/search">
18+
<label for="query">Search Query:</label>
19+
<input type="text" id="query" name="query" required>
20+
<button type="submit">Submit GET</button>
21+
</form>
22+
<div id="getResult"></div>
23+
<script>
24+
...
25+
// Event listener to submit the form and display results in `getResult`
26+
</script>
27+
----
28+
29+
And the following controller to handle the `/search` request
30+
31+
include-code::./MockMvcPlaywrightHandlerTests[tag=controller,indent=0]
32+
33+
Playwright can be used to submit the form and assert results are displayed
34+
35+
include-code::./MockMvcPlaywrightHandlerTests[tag=test,indent=0]
36+
37+
[[mockmvc-playwright-testing-file-upload]]
38+
== File Upload
39+
40+
You can test a file upload with playwright and assert that it is properly handled by your controllers.
41+
For example, the following form uploads a file.
42+
43+
[source,html]
44+
----
45+
<form id="singleFileForm" data-testid="singleFileForm" method="post" action="/upload-single" enctype="multipart/form-data">
46+
<label for="singleFile">Choose File:</label>
47+
<input type="file" id="singleFile" name="file" required>
48+
<button type="submit">Upload Single File</button>
49+
</form>
50+
----
51+
52+
Assuming that the following controller handles the POST `/upload-single` request
53+
54+
include-code::./MockMvcPlaywrightHandlerTests[tag=controller,indent=0]
55+
56+
Playwright can be used to send the file and assert results are displayed
57+
58+
include-code::./MockMvcPlaywrightHandlerTests[tag=test,indent=0]

framework-docs/src/main/java/org/springframework/docs/testing/mockmvc/mockmvcserverplaywright/MockMvcPlaywrightHandlerTests.java

Lines changed: 0 additions & 201 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.testing.mockmvc.playwright.mockmvcplaywrightsetup;
18+
19+
import com.microsoft.playwright.Browser;
20+
import com.microsoft.playwright.Page;
21+
import com.microsoft.playwright.Playwright;
22+
import org.junit.jupiter.api.*;
23+
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
24+
import org.springframework.test.web.servlet.playwright.MockMvcPlaywrightHandler;
25+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
26+
import org.springframework.web.context.WebApplicationContext;
27+
28+
@SpringJUnitWebConfig
29+
public class MockMvcPlaywrightHandlerTests {
30+
31+
private static Playwright playwright;
32+
private static Browser browser;
33+
private Page page;
34+
35+
private final MockMvcPlaywrightHandler handler;
36+
// tag::init[]
37+
public MockMvcPlaywrightHandlerTests(WebApplicationContext wac) {
38+
var mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
39+
this.handler = MockMvcPlaywrightHandler.builder(mockMvc).build();
40+
}
41+
// end::init[]
42+
43+
// tag::setup[]
44+
@BeforeAll
45+
static void initPlaywright() {
46+
playwright = Playwright.create();
47+
browser = playwright.chromium().launch();
48+
}
49+
@BeforeEach
50+
public void initPage() {
51+
page = browser.newPage();
52+
page.setDefaultTimeout(500);
53+
page.route(url -> url.startsWith("http://localhost"), handler);
54+
page.navigate("http://localhost/index.html");
55+
}
56+
57+
// end::setup[]
58+
// tag::close[]
59+
60+
@AfterEach
61+
public void closePage() {
62+
page.close();
63+
}
64+
@AfterAll
65+
static void closePlaywright() {
66+
browser.close();
67+
playwright.close();
68+
}
69+
70+
// end::close[]
71+
72+
}

0 commit comments

Comments
 (0)