|
| 1 | +package ng.testapp; |
| 2 | + |
| 3 | +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.AfterAll; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import com.microsoft.playwright.Browser; |
| 12 | +import com.microsoft.playwright.BrowserContext; |
| 13 | +import com.microsoft.playwright.Page; |
| 14 | +import com.microsoft.playwright.Playwright; |
| 15 | + |
| 16 | +import ng.appserver.NGApplication; |
| 17 | + |
| 18 | +/** |
| 19 | + * Playwright-based integration tests for the framework's client-side Ajax support. |
| 20 | + * |
| 21 | + * Boots the ng-testapp application on port 1200, then uses a headless browser |
| 22 | + * to exercise AjaxUpdateLink, AjaxUpdateContainer, AjaxObserveField and friends. |
| 23 | + */ |
| 24 | +public class TestAjax { |
| 25 | + |
| 26 | + private static final String BASE_URL = "http://localhost:1200"; |
| 27 | + |
| 28 | + // Shared across all tests — expensive to create |
| 29 | + static Playwright playwright; |
| 30 | + static Browser browser; |
| 31 | + |
| 32 | + // Fresh per test — full isolation (cookies, storage, etc.) |
| 33 | + BrowserContext context; |
| 34 | + Page page; |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + static void startAppAndBrowser() { |
| 38 | + // Boot the ng-testapp server (blocks until Jetty is listening) |
| 39 | + NGApplication.runAndReturn( new String[0], Application.class ); |
| 40 | + |
| 41 | + playwright = Playwright.create(); |
| 42 | + browser = playwright.chromium().launch(); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterAll |
| 46 | + static void closeBrowser() { |
| 47 | + if( playwright != null ) { |
| 48 | + playwright.close(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void createPage() { |
| 54 | + context = browser.newContext(); |
| 55 | + page = context.newPage(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + void closePage() { |
| 60 | + context.close(); |
| 61 | + } |
| 62 | + |
| 63 | + // --- Basic page load ----------------------------------------------------- |
| 64 | + |
| 65 | + @Test |
| 66 | + void ajaxPageLoads() { |
| 67 | + page.navigate( BASE_URL + "/ajax" ); |
| 68 | + assertThat( page ).hasTitle( "Welcome to ng" ); |
| 69 | + assertThat( page.getByText( "Partial page updates (Ajax)" ) ).isVisible(); |
| 70 | + } |
| 71 | + |
| 72 | + // --- AjaxUpdateLink + AjaxUpdateContainer -------------------------------- |
| 73 | + |
| 74 | + @Test |
| 75 | + void ajaxUpdateLinkUpdatesContainer() { |
| 76 | + page.navigate( BASE_URL + "/ajax" ); |
| 77 | + |
| 78 | + // Click the "Show current time" link |
| 79 | + page.getByText( "Show current time" ).click(); |
| 80 | + |
| 81 | + // The container should now contain a time string |
| 82 | + assertThat( page.locator( "#timeUC" ) ).containsText( "The current time is" ); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void ajaxUpdateLinkUpdatesMultipleContainers() { |
| 87 | + page.navigate( BASE_URL + "/ajax" ); |
| 88 | + |
| 89 | + // Click the "Update both" link |
| 90 | + page.getByText( "Update both" ).click(); |
| 91 | + |
| 92 | + // Both containers should now contain time strings |
| 93 | + assertThat( page.locator( "#uc1" ) ).containsText( "The current time is" ); |
| 94 | + assertThat( page.locator( "#uc2" ) ).containsText( "The current time is" ); |
| 95 | + } |
| 96 | + |
| 97 | + // --- AjaxSubmitButton ---------------------------------------------------- |
| 98 | + |
| 99 | + @Test |
| 100 | + void ajaxSubmitButtonSubmitsFormAndUpdatesContainer() { |
| 101 | + page.navigate( BASE_URL + "/ajax" ); |
| 102 | + |
| 103 | + // NGTextField renders as a plain <input type="text"> without an id. |
| 104 | + // The "Say hello" button is in the same form, so locate the text field relative to it. |
| 105 | + final var submitButton = page.locator( "input[value='Say hello']" ); |
| 106 | + submitButton.locator( "xpath=preceding-sibling::input[@type='text']" ).fill( "Playwright" ); |
| 107 | + submitButton.click(); |
| 108 | + |
| 109 | + // The container should greet us |
| 110 | + assertThat( page.locator( "#helloUC" ) ).containsText( "Hello Playwright" ); |
| 111 | + } |
| 112 | + |
| 113 | + // --- AjaxObserveField (single-field mode) -------------------------------- |
| 114 | + |
| 115 | + @Test |
| 116 | + void observeFieldSingleModeEchosInput() { |
| 117 | + page.navigate( BASE_URL + "/ajax" ); |
| 118 | + |
| 119 | + // Type into the observed input field |
| 120 | + page.locator( "#observeInput" ).fill( "hello world" ); |
| 121 | + |
| 122 | + // The echo container should update after debounce (default 300ms) |
| 123 | + assertThat( page.locator( "#observeEcho" ) ).containsText( "Echo: hello world" ); |
| 124 | + } |
| 125 | + |
| 126 | + // --- AjaxObserveField (container mode) ----------------------------------- |
| 127 | + |
| 128 | + @Test |
| 129 | + void observeFieldContainerModeObservesDescendants() { |
| 130 | + page.navigate( BASE_URL + "/ajax" ); |
| 131 | + |
| 132 | + // Type into Field A |
| 133 | + page.locator( "#containerFieldA" ).fill( "alpha" ); |
| 134 | + |
| 135 | + // The container echo should reflect Field A's value |
| 136 | + assertThat( page.locator( "#containerEcho" ) ).containsText( "A=alpha" ); |
| 137 | + |
| 138 | + // Now type into Field B |
| 139 | + page.locator( "#containerFieldB" ).fill( "beta" ); |
| 140 | + |
| 141 | + // The container echo should reflect both values |
| 142 | + assertThat( page.locator( "#containerEcho" ) ).containsText( "A=alpha, B=beta" ); |
| 143 | + } |
| 144 | +} |
0 commit comments