Skip to content

Commit

Permalink
[bidi] Add browsing context destroyed event
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Jan 24, 2024
1 parent 8383860 commit fa658f4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Expand Up @@ -113,7 +113,7 @@ public void onBrowsingContextCreated(Consumer<BrowsingContextInfo> consumer) {
}
}

private void onBrowsingContextDestroyed(Consumer<BrowsingContextInfo> consumer) {
public void onBrowsingContextDestroyed(Consumer<BrowsingContextInfo> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(browsingContextDestroyed, consumer);
} else {
Expand Down
Expand Up @@ -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<BrowsingContextInfo> 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)
Expand Down
Expand Up @@ -37,6 +37,13 @@ class BrowsingContextInspector {
)
}

async onBrowsingContextDestroyed(callback) {
await this.subscribeAndHandleEvent(
'browsingContext.contextDestroyed',
callback
)
}

async onNavigationStarted(callback) {
await this.subscribeAndHandleEvent(
'browsingContext.navigationStarted',
Expand Down
Expand Up @@ -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)
Expand Down

0 comments on commit fa658f4

Please sign in to comment.