Skip to content

Commit

Permalink
Added a helper to check for JS exceptions
Browse files Browse the repository at this point in the history
Essential for add-on testing
  • Loading branch information
mstahv committed Nov 17, 2023
1 parent a2842fe commit 0e8716a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/in/virit/mopo/Mopo.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
}
}
}
69 changes: 69 additions & 0 deletions src/test/java/firitin/pw/AddonHelpersIT.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
18 changes: 18 additions & 0 deletions src/test/java/firitin/ui/AddOnHelpersView.java
Original file line number Diff line number Diff line change
@@ -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();");
}));
}
}

0 comments on commit 0e8716a

Please sign in to comment.