|  | 
| 21 | 21 | import static org.openqa.selenium.testing.drivers.Browser.*; | 
| 22 | 22 | 
 | 
| 23 | 23 | import java.util.List; | 
|  | 24 | +import java.util.Optional; | 
| 24 | 25 | import java.util.concurrent.CompletableFuture; | 
| 25 | 26 | import java.util.concurrent.ExecutionException; | 
| 26 | 27 | import java.util.concurrent.TimeUnit; | 
|  | 
| 29 | 30 | import org.openqa.selenium.By; | 
| 30 | 31 | import org.openqa.selenium.WindowType; | 
| 31 | 32 | import org.openqa.selenium.bidi.module.BrowsingContextInspector; | 
|  | 33 | +import org.openqa.selenium.bidi.module.Script; | 
| 32 | 34 | import org.openqa.selenium.testing.JupiterTestBase; | 
| 33 | 35 | import org.openqa.selenium.testing.NeedsFreshDriver; | 
| 34 | 36 | import org.openqa.selenium.testing.NotYetImplemented; | 
| @@ -278,4 +280,27 @@ void canListenToNavigationFailedEvent() | 
| 278 | 280 |           .isEqualTo("http://invalid-domain-that-does-not-exist.test/"); | 
| 279 | 281 |     } | 
| 280 | 282 |   } | 
|  | 283 | + | 
|  | 284 | +  @Test | 
|  | 285 | +  @NeedsFreshDriver | 
|  | 286 | +  void canListenToHistoryUpdatedEvent() | 
|  | 287 | +      throws ExecutionException, InterruptedException, TimeoutException { | 
|  | 288 | +    try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver); | 
|  | 289 | +        Script script = new Script(driver)) { | 
|  | 290 | +      CompletableFuture<HistoryUpdated> future = new CompletableFuture<>(); | 
|  | 291 | + | 
|  | 292 | +      BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); | 
|  | 293 | +      context.navigate(appServer.whereIs("/simpleTest.html"), ReadinessState.COMPLETE); | 
|  | 294 | + | 
|  | 295 | +      inspector.onHistoryUpdated(future::complete); | 
|  | 296 | + | 
|  | 297 | +      // Use history.pushState to trigger history updated event | 
|  | 298 | +      script.evaluateFunctionInBrowsingContext( | 
|  | 299 | +          context.getId(), "history.pushState({}, '', '/new-path')", false, Optional.empty()); | 
|  | 300 | + | 
|  | 301 | +      HistoryUpdated historyUpdated = future.get(5, TimeUnit.SECONDS); | 
|  | 302 | +      assertThat(historyUpdated.getBrowsingContextId()).isEqualTo(context.getId()); | 
|  | 303 | +      assertThat(historyUpdated.getUrl()).contains("/new-path"); | 
|  | 304 | +    } | 
|  | 305 | +  } | 
| 281 | 306 | } | 
0 commit comments