From fa658f499add45c0d1a281169ae5f94062f73f30 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 24 Jan 2024 11:01:14 +0530 Subject: [PATCH] [bidi] Add browsing context destroyed event --- .../bidi/BrowsingContextInspector.java | 2 +- .../BrowsingContextInspectorTest.java | 25 +++++++++++++++++++ .../bidi/browsingContextInspector.js | 7 ++++++ .../bidi/browsingcontext_inspector_test.js | 18 +++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/java/src/org/openqa/selenium/bidi/BrowsingContextInspector.java b/java/src/org/openqa/selenium/bidi/BrowsingContextInspector.java index 9eb4048c7ac99..08f4b9ddef2da 100644 --- a/java/src/org/openqa/selenium/bidi/BrowsingContextInspector.java +++ b/java/src/org/openqa/selenium/bidi/BrowsingContextInspector.java @@ -113,7 +113,7 @@ public void onBrowsingContextCreated(Consumer consumer) { } } - private void onBrowsingContextDestroyed(Consumer consumer) { + public void onBrowsingContextDestroyed(Consumer consumer) { if (browsingContextIds.isEmpty()) { this.bidi.addListener(browsingContextDestroyed, consumer); } else { diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java index f9c42018998c4..9f1e77b2cd5ce 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java @@ -73,6 +73,31 @@ void canListenToWindowBrowsingContextCreatedEvent() } } + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(CHROME) + @NotYetImplemented(EDGE) + void canListenToBrowsingContextDestroyedEvent() + throws ExecutionException, InterruptedException, TimeoutException { + try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) { + CompletableFuture future = new CompletableFuture<>(); + + inspector.onBrowsingContextDestroyed(future::complete); + + String windowHandle = driver.switchTo().newWindow(WindowType.WINDOW).getWindowHandle(); + + driver.close(); + + BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS); + + assertThat(browsingContextInfo.getId()).isEqualTo(windowHandle); + assertThat("about:blank").isEqualTo(browsingContextInfo.getUrl()); + assertThat(browsingContextInfo.getChildren()).isEqualTo(null); + assertThat(browsingContextInfo.getParentBrowsingContext()).isEqualTo(null); + } + } + @Test @NotYetImplemented(SAFARI) @NotYetImplemented(IE) diff --git a/javascript/node/selenium-webdriver/bidi/browsingContextInspector.js b/javascript/node/selenium-webdriver/bidi/browsingContextInspector.js index 1504cf0ac1f01..3bdd286ec83ad 100644 --- a/javascript/node/selenium-webdriver/bidi/browsingContextInspector.js +++ b/javascript/node/selenium-webdriver/bidi/browsingContextInspector.js @@ -37,6 +37,13 @@ class BrowsingContextInspector { ) } + async onBrowsingContextDestroyed(callback) { + await this.subscribeAndHandleEvent( + 'browsingContext.contextDestroyed', + callback + ) + } + async onNavigationStarted(callback) { await this.subscribeAndHandleEvent( 'browsingContext.navigationStarted', diff --git a/javascript/node/selenium-webdriver/test/bidi/browsingcontext_inspector_test.js b/javascript/node/selenium-webdriver/test/bidi/browsingcontext_inspector_test.js index 7222872f171ec..dcc4219f6a49d 100644 --- a/javascript/node/selenium-webdriver/test/bidi/browsingcontext_inspector_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/browsingcontext_inspector_test.js @@ -56,6 +56,24 @@ suite( assert.equal(contextInfo.parentBrowsingContext, null) }) + it('can listen to browsing context destroyed event', async function () { + let contextInfo = null + const browsingcontextInspector = await BrowsingContextInspector(driver) + await browsingcontextInspector.onBrowsingContextDestroyed((entry) => { + contextInfo = entry + }) + + await driver.switchTo().newWindow('window') + + const windowHandle = await driver.getWindowHandle() + await driver.close() + + assert.equal(contextInfo.id, windowHandle) + assert.equal(contextInfo.url, 'about:blank') + assert.equal(contextInfo.children, null) + assert.equal(contextInfo.parentBrowsingContext, null) + }) + it('can listen to tab browsing context created event', async function () { let contextInfo = null const browsingcontextInspector = await BrowsingContextInspector(driver)