From ffa4842ad1fb536afc7874d69864764a2f0102ff Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Fri, 15 Nov 2024 21:15:11 +0100 Subject: [PATCH 1/3] fix: `browser.getClientWindows` returns a unique per window value --- .../modules/browser/BrowserProcessor.ts | 15 ++++- tests/browser/test_get_client_windows.py | 64 ++++++++++++++++++- tests/conftest.py | 4 +- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/bidiMapper/modules/browser/BrowserProcessor.ts b/src/bidiMapper/modules/browser/BrowserProcessor.ts index 2acf6d9d9..8c863a5dd 100644 --- a/src/bidiMapper/modules/browser/BrowserProcessor.ts +++ b/src/bidiMapper/modules/browser/BrowserProcessor.ts @@ -113,7 +113,7 @@ export class BrowserProcessor { {targetId}, ); return { - // Is not supported in CDP yet. + // `activate` is not supported in CDP yet. active: false, clientWindow: `${windowInfo.windowId}`, state: windowInfo.bounds.windowState ?? 'normal', @@ -134,6 +134,17 @@ export class BrowserProcessor { async (targetId) => await this.#getWindowInfo(targetId), ), ); - return {clientWindows}; + + const uniqueClientWindowIds = new Set(); + const uniqueClientWindows = new Array(); + + // Filter out duplicated client windows. + for (const window of clientWindows) { + if (!uniqueClientWindowIds.has(window.clientWindow)) { + uniqueClientWindowIds.add(window.clientWindow); + uniqueClientWindows.push(window); + } + } + return {clientWindows: uniqueClientWindows}; } } diff --git a/tests/browser/test_get_client_windows.py b/tests/browser/test_get_client_windows.py index 4582b3496..728671e65 100644 --- a/tests/browser/test_get_client_windows.py +++ b/tests/browser/test_get_client_windows.py @@ -19,7 +19,7 @@ @pytest.mark.asyncio -async def test_browser_get_client_windows(websocket, context_id): +async def test_browser_get_client_windows_single_tab(websocket, context_id): resp = await execute_command(websocket, { "method": "browser.getClientWindows", "params": {} @@ -27,7 +27,67 @@ async def test_browser_get_client_windows(websocket, context_id): assert resp == { 'clientWindows': [ { - # Not implemented yet + # `active` is not implemented yet + 'active': False, + 'clientWindow': ANY_STR, + 'height': ANY_NUMBER, + 'state': 'normal', + 'width': ANY_NUMBER, + 'x': ANY_NUMBER, + 'y': ANY_NUMBER, + }, + ], + } + + +@pytest.mark.asyncio +async def test_browser_get_client_windows_two_tabs(websocket, context_id, + create_context): + await create_context(context_type='tab') + + resp = await execute_command(websocket, { + "method": "browser.getClientWindows", + "params": {} + }) + assert resp == { + 'clientWindows': [ + { + # `active` is not implemented yet + 'active': False, + 'clientWindow': ANY_STR, + 'height': ANY_NUMBER, + 'state': 'normal', + 'width': ANY_NUMBER, + 'x': ANY_NUMBER, + 'y': ANY_NUMBER, + }, + ], + } + + +@pytest.mark.asyncio +async def test_browser_get_client_windows_two_windows(websocket, context_id, + create_context): + await create_context(context_type='window') + + resp = await execute_command(websocket, { + "method": "browser.getClientWindows", + "params": {} + }) + assert resp == { + 'clientWindows': [ + { + # `active` is not implemented yet + 'active': False, + 'clientWindow': ANY_STR, + 'height': ANY_NUMBER, + 'state': 'normal', + 'width': ANY_NUMBER, + 'x': ANY_NUMBER, + 'y': ANY_NUMBER, + }, + { + # `active` is not implemented yet 'active': False, 'clientWindow': ANY_STR, 'height': ANY_NUMBER, diff --git a/tests/conftest.py b/tests/conftest.py index 65f4e83ac..d73045916 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -174,12 +174,12 @@ async def another_context_id(create_context): @pytest_asyncio.fixture def create_context(websocket): """Return a browsing context factory.""" - async def create_context(user_context_id=None): + async def create_context(user_context_id=None, context_type='tab'): result = await execute_command( websocket, { "method": "browsingContext.create", "params": { - "type": "tab" + "type": context_type } | ({ "userContext": user_context_id } if user_context_id is not None else {}) From 6193900b53b89286588182ac5ba8058e04c28ff8 Mon Sep 17 00:00:00 2001 From: Maksim Sadym <69349599+sadym-chromium@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:47:59 +0100 Subject: [PATCH 2/3] Update src/bidiMapper/modules/browser/BrowserProcessor.ts Co-authored-by: Alex Rudenko --- src/bidiMapper/modules/browser/BrowserProcessor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bidiMapper/modules/browser/BrowserProcessor.ts b/src/bidiMapper/modules/browser/BrowserProcessor.ts index 8c863a5dd..52003ba8e 100644 --- a/src/bidiMapper/modules/browser/BrowserProcessor.ts +++ b/src/bidiMapper/modules/browser/BrowserProcessor.ts @@ -113,7 +113,7 @@ export class BrowserProcessor { {targetId}, ); return { - // `activate` is not supported in CDP yet. + // `active` is not supported in CDP yet. active: false, clientWindow: `${windowInfo.windowId}`, state: windowInfo.bounds.windowState ?? 'normal', From 2bf8e3eb0ca4a552b1d4a3649410d4b1caf7535a Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Mon, 18 Nov 2024 16:19:50 +0100 Subject: [PATCH 3/3] disable some e2e tests for old headless --- tests/browser/test_get_client_windows.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/browser/test_get_client_windows.py b/tests/browser/test_get_client_windows.py index 728671e65..4e8edf028 100644 --- a/tests/browser/test_get_client_windows.py +++ b/tests/browser/test_get_client_windows.py @@ -42,7 +42,11 @@ async def test_browser_get_client_windows_single_tab(websocket, context_id): @pytest.mark.asyncio async def test_browser_get_client_windows_two_tabs(websocket, context_id, - create_context): + create_context, + test_headless_mode): + if test_headless_mode == "old": + pytest.xfail("In old headless mode, each tab is in a separate window") + await create_context(context_type='tab') resp = await execute_command(websocket, {