This is an automated email from the ASF dual-hosted git repository. papegaaij pushed a commit to branch islands in repository https://gitbox.apache.org/repos/asf/wicket.git
commit bd2391e7e4b3cf2d83ea924467b72b0db745db95 Author: Emond Papegaaij <[email protected]> AuthorDate: Thu Nov 5 22:27:17 2026 +0100 Test the Treasure Hunt in a browser and show how it is built The Treasure Hunt example was only checked by hand in a browser, so nothing in the build would notice the example breaking while the islands module changes. TreasureHuntSeleniumTest plays the game in a headless Chrome with either Ajax engine: a probe updating the board, the history and the Wicket labels; the hover shared between two islands; a new game keeping the islands mounted and a new board size mounting a new board; a probe from a stale tab answered as expired; and a score validated, saved with markup in the name and listed through the page-less endpoint. Each test also fails on browser errors, CSP violations included, and on an island that outlived its host. Like the other Selenium tests it only runs with -Dwicket.selenium=true, and it is skipped when the examples were built without -Pjs-test. The status line and the probe count get a class to find them by, because markup ids are generated in deployment mode. The page explains which parts are islands and what each demonstrates, and how to work on the React components with hot reload. The source viewer lists the island modules and vite.config.js next to the Java source, so both halves of the example can be read in one place. The Vite build writes THIRD-PARTY-LICENSES.md next to the manifest, listing the npm packages bundled into the war with their licences. It is not served. The release build does not use -Pjs-test, so the released war bundles none of them. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- wicket-examples/pom.xml | 10 + wicket-examples/src/main/frontend/vite.config.js | 4 +- .../islands/treasurehunt/TreasureHuntPage.css | 5 + .../islands/treasurehunt/TreasureHuntPage.html | 46 ++- .../treasurehunt/TreasureHuntSeleniumTest.java | 432 +++++++++++++++++++++ .../wicket/examples/source/SourcesPageTest.java | 21 +- 6 files changed, 514 insertions(+), 4 deletions(-) diff --git a/wicket-examples/pom.xml b/wicket-examples/pom.xml index 8f6f264fe1..eeb8254f99 100644 --- a/wicket-examples/pom.xml +++ b/wicket-examples/pom.xml @@ -238,6 +238,16 @@ <include>**/*.ttf</include> </includes> </resource> + <!-- the island modules of the Treasure Hunt, to view them next to its Java source --> + <resource> + <filtering>false</filtering> + <directory>${basedir}/src/main/frontend</directory> + <targetPath>org/apache/wicket/examples/islands/treasurehunt/frontend</targetPath> + <includes> + <include>vite.config.js</include> + <include>src/treasurehunt/*</include> + </includes> + </resource> </resources> <plugins> <plugin> diff --git a/wicket-examples/src/main/frontend/vite.config.js b/wicket-examples/src/main/frontend/vite.config.js index 25e0074146..245f4bb3be 100644 --- a/wicket-examples/src/main/frontend/vite.config.js +++ b/wicket-examples/src/main/frontend/vite.config.js @@ -52,6 +52,8 @@ export default defineConfig({ }, build: { outDir: '../../../target/generated-resources/wicket-islands/META-INF/wicket-islands', - emptyOutDir: true + emptyOutDir: true, + // lists the npm packages bundled into the war and their licenses + license: { fileName: 'THIRD-PARTY-LICENSES.md' } } }); diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.css b/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.css index 3669dc5d3e..0d8efe4664 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.css +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.css @@ -37,3 +37,8 @@ gap: 3em; align-items: flex-start; } + +.treasure-notes { + margin-top: 2em; + max-width: 50em; +} diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.html index d540d5008e..fc05297828 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.html +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntPage.html @@ -19,8 +19,8 @@ <div class="treasure-layout"> <div wicket:id="game" class="treasure-game"> <p> - <span wicket:id="status"></span> - Probes left: <span wicket:id="probesLeft"></span>. + <span wicket:id="status" class="treasure-status"></span> + Probes left: <span wicket:id="probesLeft" class="treasure-probes-left"></span>. <a wicket:id="newGame" href="#">New game</a> </p> <div class="treasure-playfield"> @@ -31,6 +31,48 @@ </div> <div wicket:id="highScores"></div> </div> + + <section class="treasure-notes"> + <h3>How this page is built</h3> + <p> + Four React components are mounted as islands. Each has a Java class that places it in + the page, and a module under <code>frontend/src/treasurehunt</code> that renders it; the + source viewer shows both. + </p> + <ul> + <li> + <strong>The board</strong> (<code>TreasureBoard</code>) is an <code>Island</code>. + Its props are a projection of the game without the treasure positions, so the browser + never sees them. A click is a page-bound call: <code>probe</code> runs on the server + under the page lock, changes the game and adds the status line and the history to + the Ajax response. + </li> + <li> + <strong>The probe history</strong> (<code>ProbeHistory</code>) shares the hovered cell + with the board through a client store. Hovering never reaches the server. + </li> + <li> + <strong>The name and colour field</strong> (<code>PlayerEntryField</code>) is a + <code>FormIsland</code> in a plain Wicket form. Its value is converted from JSON and + checked by Wicket validators; errors show in the island and in the feedback panel. + </li> + <li> + <strong>The high scores</strong> (<code>HighScoreList</code>) come from the + page-less endpoint <code>scores</code>, which runs without the page lock. Saving a + score invalidates <code>scores</code>, so the list fetches them again. + </li> + </ul> + <p> + "New game" re-renders the board with Wicket; React keeps the mounted component and + updates its props. A new board size replaces the board component, so React mounts a new + one. + </p> + <p> + To change the React components with hot reload, run <code>npm run dev</code> in + <code>wicket-examples/src/main/frontend</code> and start the examples in development + mode with <code>-Dwicket.islands.devServer=http://localhost:5173</code>. + </p> + </section> </wicket:extend> </body> </html> diff --git a/wicket-examples/src/test/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntSeleniumTest.java b/wicket-examples/src/test/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntSeleniumTest.java new file mode 100644 index 0000000000..12d8b5e45b --- /dev/null +++ b/wicket-examples/src/test/java/org/apache/wicket/examples/islands/treasurehunt/TreasureHuntSeleniumTest.java @@ -0,0 +1,432 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.islands.treasurehunt; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.List; +import java.util.logging.Level; +import java.util.stream.Collectors; + +import org.apache.wicket.examples.AjaxEngineSelector.Engine; +import org.apache.wicket.examples.JettyTestCaseDecorator; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.Keys; +import org.openqa.selenium.StaleElementReferenceException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.WindowType; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.logging.LogEntry; +import org.openqa.selenium.logging.LogType; +import org.openqa.selenium.logging.LoggingPreferences; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.Select; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Plays the Treasure Hunt in a headless Chrome, with either Ajax engine. + * <p> + * Needs Chrome, so it only runs with {@code -Dwicket.selenium=true}, and the examples' JavaScript, + * so it is skipped unless the build ran with {@code -Pjs-test}. Every test also checks that the + * browser logged no errors, which includes violations of the content security policy. + */ +@EnabledIfSystemProperty(named = "wicket.selenium", matches = "true") +class TreasureHuntSeleniumTest extends JettyTestCaseDecorator +{ + private static final String BOARD = "[data-wicket-island=treasure-board]"; + + private static final String BOARD_ROOT = BOARD + " [data-wicket-island-root]"; + + private static final String CELLS = BOARD + " button.cell"; + + private static final String HISTORY = ".probe-history"; + + private static final String PLAYER_ENTRY = ".player-entry"; + + private static final String HIGH_SCORES = ".highscore-list"; + + /** + * Marks the element so a later check can tell whether it is still the same one. + */ + private static final String MARK = + "document.querySelector(arguments[0]).__mark = arguments[1];"; + + private static final String READ_MARK = """ + var element = document.querySelector(arguments[0]); + return element && element.__mark ? element.__mark : null; + """; + + private boolean started; + + private WebDriver driver; + + private WebDriverWait wait; + + @Override + @BeforeEach + public void before() throws Exception + { + Assumptions.assumeTrue( + getClass().getResource("/META-INF/wicket-islands/.vite/manifest.json") != null, + "the island modules are built with -Pjs-test only"); + + super.before(); + started = true; + + LoggingPreferences logging = new LoggingPreferences(); + logging.enable(LogType.BROWSER, Level.ALL); + ChromeOptions options = new ChromeOptions(); + options.addArguments("--headless=new", "--window-size=1280,1024"); + options.setCapability(ChromeOptions.LOGGING_PREFS, logging); + driver = new ChromeDriver(options); + wait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofMillis(20)); + // Wicket replaces the labels on every probe, possibly between finding and reading one + wait.ignoring(StaleElementReferenceException.class); + } + + @Override + @AfterEach + public void after() throws Exception + { + if (driver != null) + { + driver.quit(); + } + if (started) + { + super.after(); + } + } + + @ParameterizedTest + @EnumSource(Engine.class) + void aProbeUpdatesTheBoardTheHistoryAndTheWicketComponents(Engine engine) + { + open(engine); + assertEquals(100, cells().size()); + assertEquals("20", probesLeft()); + mark(BOARD_ROOT, "first"); + + probe(0, 0); + + assertEquals("19", probesLeft()); + assertEquals(1, driver.findElements(By.cssSelector(HISTORY + " li")).size()); + assertEquals("first", readMark(BOARD_ROOT), "the board was mounted again"); + assertNoLeakedIslands(); + assertNoBrowserErrors(); + } + + @ParameterizedTest + @EnumSource(Engine.class) + void theBoardAndTheHistoryShareTheHoveredCell(Engine engine) + { + open(engine); + probe(3, 4); + + hover(HISTORY + " li[data-x='3'][data-y='4']"); + wait.until(ExpectedConditions.attributeContains(cell(3, 4), "class", "hover")); + + hover(BOARD + " button.cell[data-x='5'][data-y='5']"); + wait.until(ExpectedConditions.not( + ExpectedConditions.attributeContains(cell(3, 4), "class", "hover"))); + hover(BOARD + " button.cell[data-x='3'][data-y='4']"); + wait.until(ExpectedConditions.attributeContains( + By.cssSelector(HISTORY + " li[data-x='3'][data-y='4']"), "class", "hover")); + assertNoBrowserErrors(); + } + + @ParameterizedTest + @EnumSource(Engine.class) + void aNewGameKeepsTheIslandsMounted(Engine engine) + { + open(engine); + probe(0, 0); + String key = boardKey(); + mark(BOARD_ROOT, "first"); + + driver.findElement(By.linkText("New game")).click(); + wait.until(driver -> driver.findElements(By.cssSelector(CELLS + ".probed, " + CELLS + + ".found")).isEmpty()); + + assertEquals("20", probesLeft()); + assertEquals(key, boardKey()); + assertEquals("first", readMark(BOARD_ROOT), "the board was mounted again"); + assertNoLeakedIslands(); + assertNoBrowserErrors(); + } + + @ParameterizedTest + @EnumSource(Engine.class) + void aNewBoardSizeMountsANewBoard(Engine engine) + { + open(engine); + String key = boardKey(); + mark(BOARD_ROOT, "first"); + + selectSize("SMALL"); + + assertNotEquals(key, boardKey()); + assertEquals(null, readMark(BOARD_ROOT), "the old board was kept"); + probe(1, 1); + assertEquals("15", probesLeft()); + assertNoLeakedIslands(); + assertNoBrowserErrors(); + } + + @ParameterizedTest + @EnumSource(Engine.class) + void aProbeFromAStaleTabIsAnsweredAsExpired(Engine engine) + { + open(engine); + String staleTab = driver.getWindowHandle(); + String url = driver.getCurrentUrl(); + + // rendering the page in a second tab makes the first one stale + driver.switchTo().newWindow(WindowType.TAB); + driver.get(url); + awaitIslands(); + driver.switchTo().window(staleTab); + + cell(2, 2).click(); + + WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated( + By.cssSelector(BOARD + " .message"))); + assertTrue(message.getText().contains("Reload"), message.getText()); + assertEquals("20", probesLeft()); + assertTrue(cells().stream() + .allMatch(cell -> "true".equals(cell.getDomAttribute("aria-disabled"))), + "the stale board can still be played"); + + message.findElement(By.tagName("button")).click(); + wait.until(ExpectedConditions.stalenessOf(message)); + awaitIslands(); + probe(2, 2); + assertEquals("19", probesLeft()); + assertNoBrowserErrors(); + } + + @ParameterizedTest + @EnumSource(Engine.class) + void aScoreIsValidatedSavedAndListed(Engine engine) + { + open(engine); + selectSize("SMALL"); + wait.until(ExpectedConditions.textToBePresentInElementLocated( + By.cssSelector(HIGH_SCORES), "No scores yet")); + + playUntilTheGameIsOver(); + WebElement name = wait.until(ExpectedConditions.visibilityOfElementLocated( + By.cssSelector(PLAYER_ENTRY + " input"))); + + save(); + wait.until(ExpectedConditions.presenceOfElementLocated( + By.cssSelector(PLAYER_ENTRY + " .errors li"))); + List<String> islandErrors = texts(PLAYER_ENTRY + " .errors li"); + assertEquals(1, islandErrors.size(), islandErrors.toString()); + assertEquals(islandErrors, texts(".feedbackPanel li")); + + mark("[data-wicket-island=player-entry] [data-wicket-island-root]", "entry"); + name.sendKeys("A"); + save(); + wait.until(ExpectedConditions.textToBePresentInElementLocated( + By.cssSelector(PLAYER_ENTRY + " .errors"), "2 to 20")); + assertEquals("A", name.getDomProperty("value"), "the typed name was lost"); + assertEquals("entry", + readMark("[data-wicket-island=player-entry] [data-wicket-island-root]"), + "the field was mounted again"); + + name.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.BACK_SPACE, "</script><b>Ann</b>"); + WebElement colour = driver.findElement( + By.cssSelector(PLAYER_ENTRY + " .swatch:nth-child(4)")); + colour.click(); + wait.until(ExpectedConditions.attributeToBe(colour, "aria-checked", "true")); + String chosenColour = colour.getCssValue("background-color"); + save(); + wait.until(ExpectedConditions.textToBePresentInElementLocated( + By.cssSelector(".treasure-score"), "Saved for </script><b>Ann</b>.")); + wait.until(ExpectedConditions.numberOfElementsToBe( + By.cssSelector(HIGH_SCORES + " li"), 1)); + assertEquals("</script><b>Ann</b>", + driver.findElement(By.cssSelector(HIGH_SCORES + " li .name")).getText()); + assertEquals(chosenColour, driver.findElement(By.cssSelector(HIGH_SCORES + " li .dot")) + .getCssValue("background-color")); + assertTrue(driver.findElements(By.cssSelector(".treasure-score b, " + HIGH_SCORES + " b")) + .isEmpty(), "the name was rendered as markup"); + assertNoLeakedIslands(); + assertNoBrowserErrors(); + } + + private void open(Engine engine) + { + driver.get(String.format("http://localhost:%d/wicket-examples/treasurehunt", localPort)); + if (isJQueryLoaded() != (engine == Engine.JQUERY)) + { + WebElement toggle = driver.findElement(By.partialLinkText("switch to")); + toggle.click(); + wait.until(ExpectedConditions.stalenessOf(toggle)); + } + assertEquals(engine == Engine.JQUERY, isJQueryLoaded(), "the Ajax engine"); + awaitIslands(); + } + + private void awaitIslands() + { + wait.until(driver -> !cells().isEmpty() && + !driver.findElements(By.cssSelector(HISTORY)).isEmpty() && + !driver.findElements(By.cssSelector(HIGH_SCORES)).isEmpty()); + } + + private boolean isJQueryLoaded() + { + return (Boolean)js().executeScript("return typeof window.jQuery === 'function';"); + } + + private List<WebElement> cells() + { + return driver.findElements(By.cssSelector(CELLS)); + } + + private By cellSelector(int x, int y) + { + return By.cssSelector(CELLS + "[data-x='" + x + "'][data-y='" + y + "']"); + } + + private WebElement cell(int x, int y) + { + return driver.findElement(cellSelector(x, y)); + } + + /** + * Probes a cell and waits for the answer, which marks the cell as probed or found. + */ + private void probe(int x, int y) + { + String before = probesLeft(); + cell(x, y).click(); + wait.until(driver -> !before.equals(probesLeft())); + wait.until(driver -> { + String classes = driver.findElement(cellSelector(x, y)).getDomAttribute("class"); + return classes.contains("probed") || classes.contains("found"); + }); + } + + private void playUntilTheGameIsOver() + { + for (int i = 0; i < 64 && status().contains("left to find"); i++) + { + probe(i % 8, i / 8); + } + assertFalse(status().contains("left to find"), "the game did not end"); + } + + private void selectSize(String size) + { + String key = boardKey(); + new Select(driver.findElement(By.cssSelector("select"))).selectByValue(size); + wait.until(driver -> !key.equals(boardKey())); + awaitIslands(); + } + + private void save() + { + driver.findElement(By.cssSelector(".treasure-score button[name]")).click(); + } + + private void hover(String selector) + { + new Actions(driver).moveToElement(driver.findElement(By.cssSelector(selector))).perform(); + } + + private String status() + { + return driver.findElement(By.cssSelector(".treasure-status")).getText(); + } + + private String probesLeft() + { + return driver.findElement(By.cssSelector(".treasure-probes-left")).getText(); + } + + private String boardKey() + { + return driver.findElement(By.cssSelector(BOARD)).getDomAttribute("data-wicket-island-key"); + } + + private List<String> texts(String selector) + { + return driver.findElements(By.cssSelector(selector)) + .stream() + .map(WebElement::getText) + .collect(Collectors.toList()); + } + + private void mark(String selector, String mark) + { + js().executeScript(MARK, selector, mark); + } + + private Object readMark(String selector) + { + return js().executeScript(READ_MARK, selector); + } + + /** + * Every island the runtime has mounted still has its host in the document. + */ + private void assertNoLeakedIslands() + { + Object counts = js().executeScript(""" + return [globalThis.__wicketIslandsRuntime.mounted.size, + document.querySelectorAll('[data-wicket-island]').length]; + """); + List<?> mountedAndHosts = (List<?>)counts; + assertEquals(mountedAndHosts.get(1), mountedAndHosts.get(0), + "mounted islands versus hosts in the page"); + } + + private void assertNoBrowserErrors() + { + List<String> errors = driver.manage() + .logs() + .get(LogType.BROWSER) + .getAll() + .stream() + .filter(entry -> entry.getLevel().intValue() >= Level.WARNING.intValue()) + .map(LogEntry::getMessage) + .collect(Collectors.toList()); + assertTrue(errors.isEmpty(), errors.toString()); + } + + private JavascriptExecutor js() + { + return (JavascriptExecutor)driver; + } +} diff --git a/wicket-examples/src/test/java/org/apache/wicket/examples/source/SourcesPageTest.java b/wicket-examples/src/test/java/org/apache/wicket/examples/source/SourcesPageTest.java index bc505a5540..c2a44483ba 100644 --- a/wicket-examples/src/test/java/org/apache/wicket/examples/source/SourcesPageTest.java +++ b/wicket-examples/src/test/java/org/apache/wicket/examples/source/SourcesPageTest.java @@ -19,7 +19,9 @@ package org.apache.wicket.examples.source; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.apache.wicket.Page; import org.apache.wicket.examples.helloworld.HelloWorld; +import org.apache.wicket.examples.islands.treasurehunt.TreasureHuntPage; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.util.tester.WicketTestCase; import org.junit.jupiter.api.Test; @@ -31,7 +33,12 @@ public class SourcesPageTest extends WicketTestCase { private String render(String source) { - PageParameters parameters = SourcesPage.generatePageParameters(HelloWorld.class, source); + return render(HelloWorld.class, source); + } + + private String render(Class<? extends Page> page, String source) + { + PageParameters parameters = SourcesPage.generatePageParameters(page, source); tester.startPage(SourcesPage.class, parameters); return tester.getLastResponseAsString(); } @@ -45,6 +52,18 @@ public class SourcesPageTest extends WicketTestCase assertFalse(render("HelloWorld.html").contains("Unable to read the source for")); } + /** + * The island modules of the Treasure Hunt are listed and displayed with its Java source. + */ + @Test + public void islandModuleIsDisplayed() + { + String response = render(TreasureHuntPage.class, + "frontend/src/treasurehunt/TreasureBoard.tsx"); + assertTrue(response.contains("export default function TreasureBoard")); + assertTrue(response.contains("frontend/src/treasurehunt/HighScoreList.tsx")); + } + /** * An absolute class path resource outside of the package of the requested page is refused. */
