From 5dbcc6c693a0e94e32245bd739530866c6b8058f Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Fri, 15 Nov 2024 16:12:42 +0100 Subject: [PATCH] feat: implement `browser.getClientWindows` --- src/bidiMapper/CommandProcessor.ts | 9 +++-- .../modules/browser/BrowserProcessor.ts | 38 +++++++++++++++++- tests/browser/test_get_client_windows.py | 40 +++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 tests/browser/test_get_client_windows.py diff --git a/src/bidiMapper/CommandProcessor.ts b/src/bidiMapper/CommandProcessor.ts index 7b16c1f399..53c985a7ad 100644 --- a/src/bidiMapper/CommandProcessor.ts +++ b/src/bidiMapper/CommandProcessor.ts @@ -96,7 +96,10 @@ export class CommandProcessor extends EventEmitter { this.#bluetoothProcessor = bluetoothProcessor; // keep-sorted start block=yes - this.#browserProcessor = new BrowserProcessor(browserCdpClient); + this.#browserProcessor = new BrowserProcessor( + browserCdpClient, + browsingContextStorage, + ); this.#browsingContextProcessor = new BrowsingContextProcessor( browserCdpClient, browsingContextStorage, @@ -171,9 +174,7 @@ export class CommandProcessor extends EventEmitter { case 'browser.createUserContext': return await this.#browserProcessor.createUserContext(command.params); case 'browser.getClientWindows': - throw new UnknownErrorException( - `Method ${command.method} is not implemented.`, - ); + return await this.#browserProcessor.getClientWindows(); case 'browser.getUserContexts': return await this.#browserProcessor.getUserContexts(); case 'browser.removeUserContext': diff --git a/src/bidiMapper/modules/browser/BrowserProcessor.ts b/src/bidiMapper/modules/browser/BrowserProcessor.ts index dc69eb469f..2acf6d9d9c 100644 --- a/src/bidiMapper/modules/browser/BrowserProcessor.ts +++ b/src/bidiMapper/modules/browser/BrowserProcessor.ts @@ -24,12 +24,18 @@ import { NoSuchUserContextException, } from '../../../protocol/protocol.js'; import type {CdpClient} from '../../BidiMapper.js'; +import type {BrowsingContextStorage} from '../context/BrowsingContextStorage'; export class BrowserProcessor { readonly #browserCdpClient: CdpClient; + readonly #browsingContextStorage: BrowsingContextStorage; - constructor(browserCdpClient: CdpClient) { + constructor( + browserCdpClient: CdpClient, + browsingContextStorage: BrowsingContextStorage, + ) { this.#browserCdpClient = browserCdpClient; + this.#browsingContextStorage = browsingContextStorage; } close(): EmptyResult { @@ -100,4 +106,34 @@ export class BrowserProcessor { ], }; } + + async #getWindowInfo(targetId: string): Promise { + const windowInfo = await this.#browserCdpClient.sendCommand( + 'Browser.getWindowForTarget', + {targetId}, + ); + return { + // Is not supported in CDP yet. + active: false, + clientWindow: `${windowInfo.windowId}`, + state: windowInfo.bounds.windowState ?? 'normal', + height: windowInfo.bounds.height ?? 0, + width: windowInfo.bounds.width ?? 0, + x: windowInfo.bounds.left ?? 0, + y: windowInfo.bounds.top ?? 0, + }; + } + + async getClientWindows(): Promise { + const topLevelTargetIds = this.#browsingContextStorage + .getTopLevelContexts() + .map((b) => b.cdpTarget.id); + + const clientWindows = await Promise.all( + topLevelTargetIds.map( + async (targetId) => await this.#getWindowInfo(targetId), + ), + ); + return {clientWindows}; + } } diff --git a/tests/browser/test_get_client_windows.py b/tests/browser/test_get_client_windows.py new file mode 100644 index 0000000000..4582b34964 --- /dev/null +++ b/tests/browser/test_get_client_windows.py @@ -0,0 +1,40 @@ +# Copyright 2024 Google LLC. +# Copyright (c) Microsoft Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest +from anys import ANY_NUMBER, ANY_STR +from test_helpers import execute_command + + +@pytest.mark.asyncio +async def test_browser_get_client_windows(websocket, context_id): + resp = await execute_command(websocket, { + "method": "browser.getClientWindows", + "params": {} + }) + assert resp == { + 'clientWindows': [ + { + # Not implemented yet + 'active': False, + 'clientWindow': ANY_STR, + 'height': ANY_NUMBER, + 'state': 'normal', + 'width': ANY_NUMBER, + 'x': ANY_NUMBER, + 'y': ANY_NUMBER, + }, + ], + }