From 0e8716a4651aaf72fdc205344434db2eec34b904 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Fri, 17 Nov 2023 15:22:33 +0200 Subject: [PATCH] Added a helper to check for JS exceptions Essential for add-on testing --- src/main/java/in/virit/mopo/Mopo.java | 31 +++++++++ src/test/java/firitin/pw/AddonHelpersIT.java | 69 +++++++++++++++++++ .../java/firitin/ui/AddOnHelpersView.java | 18 +++++ 3 files changed, 118 insertions(+) create mode 100644 src/test/java/firitin/pw/AddonHelpersIT.java create mode 100644 src/test/java/firitin/ui/AddOnHelpersView.java diff --git a/src/main/java/in/virit/mopo/Mopo.java b/src/main/java/in/virit/mopo/Mopo.java index 2940e60..48f0d9b 100644 --- a/src/main/java/in/virit/mopo/Mopo.java +++ b/src/main/java/in/virit/mopo/Mopo.java @@ -1,8 +1,12 @@ package in.virit.mopo; +import com.microsoft.playwright.ElementHandle; +import com.microsoft.playwright.Locator; import com.microsoft.playwright.Page; +import com.microsoft.playwright.assertions.PlaywrightAssertions; import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; +import static com.microsoft.playwright.assertions.PlaywrightAssertions.setDefaultAssertionTimeout; /** * General utilities for Playwright & Vaadin. @@ -38,4 +42,31 @@ public static void waitForConnectionToSettle(Page page, int minWait) { // System.out.println("Waited for" + (System.currentTimeMillis() - start) + "ms"); } + + /** + * Asserts that there are no JS errors in the dev console. + */ + public void assertNoJsErrors() { + assertNoJsErrors(page); + } + + /** + * Asserts that there are no JS errors in the dev console. + */ + public static void assertNoJsErrors(Page page) { + + try { + ElementHandle elementHandle = page.waitForSelector("vaadin-dev-tools>div.error", + //wait for it just a tiny moment + new Page.WaitForSelectorOptions().setTimeout(100.0) + ); + if (elementHandle != null) { + String msg = page.locator("vaadin-dev-tools div.message.error").last().textContent(); + throw new AssertionError("JS error in dev console: " + msg); + } + } catch (com.microsoft.playwright.TimeoutError e) { + // expected + return; + } + } } diff --git a/src/test/java/firitin/pw/AddonHelpersIT.java b/src/test/java/firitin/pw/AddonHelpersIT.java new file mode 100644 index 0000000..8287e31 --- /dev/null +++ b/src/test/java/firitin/pw/AddonHelpersIT.java @@ -0,0 +1,69 @@ +package firitin.pw; + +import com.microsoft.playwright.Browser; +import com.microsoft.playwright.BrowserType; +import com.microsoft.playwright.Page; +import com.microsoft.playwright.Playwright; +import in.virit.mopo.Mopo; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Tag("playwright") +public class AddonHelpersIT { + private final int port = 9998; + + static Playwright playwright = Playwright.create(); + + static { + } + + private Browser browser; + private Page page; + private Mopo mopo; + + @BeforeEach + public void setup() { + browser = playwright.chromium() + .launch(new BrowserType.LaunchOptions() + .setHeadless(false) + .setDevtools(true) + ); + + page = browser.newPage(); + page.setDefaultTimeout(5000); // die faster if needed + mopo = new Mopo(page); + } + + @AfterEach + public void closePlaywright() { + page.close(); + browser.close(); + } + + @Test + public void doRandomStuffAndChangeFirstRow() throws InterruptedException { + page.navigate("http://localhost:" + port + "/addonhelpers"); + + // There should be a JS error in the dev console, assert that + mopo.assertNoJsErrors(); + + page.getByText("Throw JS exception").click(); + + boolean exceptionThrown = false; + try { + mopo.assertNoJsErrors(); + } catch (java.lang.AssertionError e) { + // Expected + System.out.println(e.getMessage()); + exceptionThrown = true; + } + assertTrue(exceptionThrown); + assertThat(page.locator("vaadin-dev-tools>div.error")).isAttached(); + } +} diff --git a/src/test/java/firitin/ui/AddOnHelpersView.java b/src/test/java/firitin/ui/AddOnHelpersView.java new file mode 100644 index 0000000..1a72f70 --- /dev/null +++ b/src/test/java/firitin/ui/AddOnHelpersView.java @@ -0,0 +1,18 @@ +package firitin.ui; + +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.router.Route; + +@Route +public class AddOnHelpersView extends VerticalLayout { + + public AddOnHelpersView() { + add("Test view"); + + add(new Button("Throw JS exception", e -> { + // deliberately cause a JS exception + getElement().executeJs("window.foo();"); + })); + } +}