diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java b/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java index 9564c23d..b0cce97d 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java @@ -17,6 +17,7 @@ package com.microsoft.playwright.impl; import com.google.gson.JsonObject; +import com.microsoft.playwright.PlaywrightException; import java.util.*; import java.util.function.Consumer; @@ -46,6 +47,9 @@ void notify(EventType eventType, T param) { } void add(EventType type, Consumer listener) { + if (listener == null) { + throw new PlaywrightException("Can't add a null listener"); + } List> list = listeners.get(type); if (list == null) { list = new ArrayList<>(); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java b/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java index 95463179..8977c748 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java @@ -347,4 +347,13 @@ void shouldPropagateCloseReasonToPendingActions() { })); assertTrue(e.getMessage().contains("The reason."), e.getMessage()); } + + @Test + void shouldProhibitNullListeners() { + Page newPage = context.newPage(); + + PlaywrightException e = assertThrows(PlaywrightException.class, () -> newPage.onClose(null)); + + assertTrue(e.getMessage().contains("Can't add a null listener")); + } }