Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: browser.getClientWindows returns a unique per window value #2783

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/bidiMapper/modules/browser/BrowserProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class BrowserProcessor {
{targetId},
);
return {
// Is not supported in CDP yet.
// `active` is not supported in CDP yet.
active: false,
clientWindow: `${windowInfo.windowId}`,
state: windowInfo.bounds.windowState ?? 'normal',
Expand All @@ -134,6 +134,17 @@ export class BrowserProcessor {
async (targetId) => await this.#getWindowInfo(targetId),
),
);
return {clientWindows};

const uniqueClientWindowIds = new Set<string>();
const uniqueClientWindows = new Array<Browser.ClientWindowInfo>();

// 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};
}
}
68 changes: 66 additions & 2 deletions tests/browser/test_get_client_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,79 @@


@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": {}
})
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,
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, {
"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,
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Loading