Skip to content

Commit c97d9b7

Browse files
authored
[js][bidi]: implement bidi getClientWindows command in browser module (#15248)
1 parent 51b9af3 commit c97d9b7

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

javascript/node/selenium-webdriver/bidi/browser.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
const { WindowState, ClientWindowInfo } = require('./clientWindowInfo')
19+
1820
/**
1921
* Represents the commands and events under Browser Module.
2022
* Described in https://w3c.github.io/webdriver-bidi/#module-browser
@@ -83,6 +85,20 @@ class Browser {
8385

8486
await this.bidi.send(command)
8587
}
88+
89+
/**
90+
* Gets information about all client windows.
91+
* @returns {Promise<ClientWindowInfo[]>} Array of client window information
92+
*/
93+
async getClientWindows() {
94+
const command = {
95+
method: 'browser.getClientWindows',
96+
params: {},
97+
}
98+
99+
const response = await this.bidi.send(command)
100+
return response.result.clientWindows.map((window) => ClientWindowInfo.fromJson(window))
101+
}
86102
}
87103

88104
async function getBrowserInstance(driver) {
@@ -92,3 +108,4 @@ async function getBrowserInstance(driver) {
92108
}
93109

94110
module.exports = getBrowserInstance
111+
module.exports.WindowState = WindowState
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
const WindowState = Object.freeze({
19+
FULLSCREEN: 'fullscreen',
20+
MAXIMIZED: 'maximized',
21+
MINIMIZED: 'minimized',
22+
NORMAL: 'normal',
23+
})
24+
25+
class ClientWindowInfo {
26+
/**
27+
* @param {Object} params Window information parameters
28+
* @param {string} params.clientWindow Window identifier
29+
* @param {string} params.state Window state from WindowState
30+
* @param {number} params.width Window width
31+
* @param {number} params.height Window height
32+
* @param {number} params.x Window x coordinate
33+
* @param {number} params.y Window y coordinate
34+
* @param {boolean} params.active Whether window is active and can receive keyboard input
35+
*/
36+
constructor({ clientWindow, state, width, height, x, y, active }) {
37+
this.clientWindow = clientWindow
38+
this.state = state
39+
this.width = width
40+
this.height = height
41+
this.x = x
42+
this.y = y
43+
this.active = active
44+
}
45+
46+
static fromJson(json) {
47+
return new ClientWindowInfo({
48+
...json,
49+
state: json.state.toLowerCase(),
50+
})
51+
}
52+
}
53+
54+
module.exports = { WindowState, ClientWindowInfo }

javascript/node/selenium-webdriver/test/bidi/browser_test.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@ const assert = require('node:assert')
2121
const { suite } = require('../../lib/test')
2222
const { Browser } = require('selenium-webdriver')
2323
const BrowserBiDi = require('selenium-webdriver/bidi/browser')
24+
const { WindowState } = require('selenium-webdriver/bidi/browser')
2425

2526
suite(
2627
function (env) {
27-
describe('BiDi Browser', function () {
28-
let driver
28+
let driver
2929

30-
beforeEach(async function () {
31-
driver = await env.builder().build()
32-
})
30+
beforeEach(async function () {
31+
driver = await env.builder().build()
32+
})
3333

34-
afterEach(function () {
35-
return driver.quit()
36-
})
34+
afterEach(function () {
35+
return driver.quit()
36+
})
3737

38+
describe('BiDi Browser', function () {
3839
it('can create a user context', async function () {
3940
const browser = await BrowserBiDi(driver)
4041

@@ -79,6 +80,25 @@ suite(
7980
await browser.removeUserContext(userContext1)
8081
})
8182
})
83+
84+
describe('Client Windows', function () {
85+
it('can get client windows', async function () {
86+
const browser = await BrowserBiDi(driver)
87+
const windows = await browser.getClientWindows()
88+
89+
assert(Array.isArray(windows))
90+
assert(windows.length > 0)
91+
92+
const window = windows[0]
93+
assert(window.clientWindow)
94+
assert(Object.values(WindowState).includes(window.state))
95+
assert(Number.isInteger(window.width) && window.width > 0)
96+
assert(Number.isInteger(window.height) && window.height > 0)
97+
assert(Number.isInteger(window.x))
98+
assert(Number.isInteger(window.y))
99+
assert(typeof window.active === 'boolean')
100+
})
101+
})
82102
},
83103
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
84104
)

0 commit comments

Comments
 (0)