|
| 1 | +package org.acme; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.*; |
| 4 | + |
| 5 | +import java.net.URL; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | + |
| 8 | +import jakarta.ws.rs.core.HttpHeaders; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import com.microsoft.playwright.BrowserContext; |
| 13 | +import com.microsoft.playwright.Response; |
| 14 | +import com.microsoft.playwright.TimeoutError; |
| 15 | +import com.microsoft.playwright.assertions.PlaywrightAssertions; |
| 16 | + |
| 17 | +import io.quarkiverse.playwright.BrowserContextConfig; |
| 18 | +import io.quarkiverse.playwright.InjectPlaywright; |
| 19 | +import io.quarkiverse.playwright.WithPlaywright; |
| 20 | +import io.quarkus.test.common.http.TestHTTPResource; |
| 21 | +import io.quarkus.test.junit.QuarkusTest; |
| 22 | + |
| 23 | +@QuarkusTest |
| 24 | +@WithPlaywright(browserContext = @BrowserContextConfig(userAgent = "playwright-browser", defaultNavigationTimeout = "PT10s")) |
| 25 | +class PlaywrightConfigTest { |
| 26 | + @InjectPlaywright |
| 27 | + BrowserContext context; |
| 28 | + |
| 29 | + @TestHTTPResource("/") |
| 30 | + URL index; |
| 31 | + |
| 32 | + @Test |
| 33 | + void timeoutWorks() { |
| 34 | + var page = context.newPage(); |
| 35 | + page.route( |
| 36 | + index.toString(), |
| 37 | + r -> { |
| 38 | + try { |
| 39 | + // Introduce intentional timeout |
| 40 | + TimeUnit.SECONDS.sleep(12); |
| 41 | + } catch (InterruptedException e) { |
| 42 | + throw new RuntimeException(e); |
| 43 | + } |
| 44 | + |
| 45 | + r.resume(); |
| 46 | + }); |
| 47 | + |
| 48 | + assertThatExceptionOfType(TimeoutError.class) |
| 49 | + .isThrownBy(() -> page.navigate(index.toString())); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testConfig() { |
| 54 | + var page = context.newPage(); |
| 55 | + |
| 56 | + page.onRequest(r -> assertThat(r.headerValue(HttpHeaders.USER_AGENT)).isEqualTo("playwright-browser")); |
| 57 | + |
| 58 | + var response = page.navigate(index.toString()); |
| 59 | + |
| 60 | + assertThat(response) |
| 61 | + .extracting(Response::status) |
| 62 | + .isEqualTo(200); |
| 63 | + |
| 64 | + page.waitForLoadState(); |
| 65 | + |
| 66 | + PlaywrightAssertions.assertThat(page) |
| 67 | + .hasTitle("My Awesome App"); |
| 68 | + |
| 69 | + // Make sure the web app is loaded and hits the backend |
| 70 | + var quinoaEl = page.waitForSelector(".toast-body.received"); |
| 71 | + var greeting = quinoaEl.innerText(); |
| 72 | + |
| 73 | + assertThat(greeting) |
| 74 | + .isEqualTo("Hello from RESTEasy Reactive"); |
| 75 | + } |
| 76 | +} |
0 commit comments