Skip to content

Commit

Permalink
fix: use non-force close for BrowsingContext.close
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade authored and Thiago Perrotta committed Jul 3, 2023
1 parent 166874b commit 55dabfb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/bidiMapper/domains/context/browsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,4 +828,8 @@ export class BrowsingContextImpl {
},
};
}

async close(): Promise<void> {
await this.#cdpTarget.cdpClient.sendCommand('Page.close');
}
}
12 changes: 5 additions & 7 deletions src/bidiMapper/domains/context/browsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,32 +510,30 @@ export class BrowsingContextProcessor {
async process_browsingContext_close(
commandParams: BrowsingContext.CloseParameters
): Promise<Message.EmptyResult> {
const browserCdpClient = this.#cdpConnection.browserClient();

const context = this.#browsingContextStorage.getContext(
commandParams.context
);

if (!context.isTopLevelContext()) {
throw new Message.InvalidArgumentException(
`Non top-level browsing context ${context.id} cannot be closed.`
);
}

const browserCdpClient = this.#cdpConnection.browserClient();
const detachedFromTargetPromise = new Promise<void>((resolve) => {
const onContextDestroyed = (
eventParams: Protocol.Target.DetachedFromTargetEvent
event: Protocol.Target.DetachedFromTargetEvent
) => {
if (eventParams.targetId === commandParams.context) {
if (event.targetId === commandParams.context) {
browserCdpClient.off('Target.detachedFromTarget', onContextDestroyed);
resolve();
}
};
browserCdpClient.on('Target.detachedFromTarget', onContextDestroyed);
});

await browserCdpClient.sendCommand('Target.closeTarget', {
targetId: commandParams.context,
});
await context.close();

// Sometimes CDP command finishes before `detachedFromTarget` event,
// sometimes after. Wait for the CDP command to be finished, and then wait
Expand Down
17 changes: 7 additions & 10 deletions tests/test_browsing_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,12 @@ async def test_browsingContext_close_browsingContext_closed(
websocket, context_id):
await subscribe(websocket, ["browsingContext.contextDestroyed"])

await send_JSON_command(
websocket, {
"id": 12,
"method": "browsingContext.close",
"params": {
"context": context_id
}
})
command_id = await send_JSON_command(websocket, {
"method": "browsingContext.close",
"params": {
"context": context_id
}
})

# Assert "browsingContext.contextCreated" event emitted.
resp = await read_JSON_message(websocket)
Expand All @@ -450,9 +448,8 @@ async def test_browsingContext_close_browsingContext_closed(
}
}

# Assert command done.
resp = await read_JSON_message(websocket)
assert resp == {"id": 12, "result": {}}
assert resp == {"id": command_id, "result": {}}

result = await get_tree(websocket)

Expand Down
14 changes: 6 additions & 8 deletions tests/test_realm.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,12 @@ async def test_realm_realmDestroyed_sandbox(websocket, context_id):
}
})

await send_JSON_command(
websocket, {
"id": 22,
"method": "browsingContext.close",
"params": {
"context": context_id,
}
})
await send_JSON_command(websocket, {
"method": "browsingContext.close",
"params": {
"context": context_id,
}
})

response = await read_JSON_message(websocket)

Expand Down

0 comments on commit 55dabfb

Please sign in to comment.