diff --git a/javascript/node/selenium-webdriver/bidi/browser.js b/javascript/node/selenium-webdriver/bidi/browser.js index 4c36d7552627e..021d343c376e6 100644 --- a/javascript/node/selenium-webdriver/bidi/browser.js +++ b/javascript/node/selenium-webdriver/bidi/browser.js @@ -15,6 +15,8 @@ // specific language governing permissions and limitations // under the License. +const { WindowState, ClientWindowInfo } = require('./clientWindowInfo') + /** * Represents the commands and events under Browser Module. * Described in https://w3c.github.io/webdriver-bidi/#module-browser @@ -83,6 +85,20 @@ class Browser { await this.bidi.send(command) } + + /** + * Gets information about all client windows. + * @returns {Promise} Array of client window information + */ + async getClientWindows() { + const command = { + method: 'browser.getClientWindows', + params: {}, + } + + const response = await this.bidi.send(command) + return response.result.clientWindows.map((window) => ClientWindowInfo.fromJson(window)) + } } async function getBrowserInstance(driver) { @@ -92,3 +108,4 @@ async function getBrowserInstance(driver) { } module.exports = getBrowserInstance +module.exports.WindowState = WindowState diff --git a/javascript/node/selenium-webdriver/bidi/clientWindowInfo.js b/javascript/node/selenium-webdriver/bidi/clientWindowInfo.js new file mode 100644 index 0000000000000..09db6c001a17a --- /dev/null +++ b/javascript/node/selenium-webdriver/bidi/clientWindowInfo.js @@ -0,0 +1,54 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you 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. + +const WindowState = Object.freeze({ + FULLSCREEN: 'fullscreen', + MAXIMIZED: 'maximized', + MINIMIZED: 'minimized', + NORMAL: 'normal', +}) + +class ClientWindowInfo { + /** + * @param {Object} params Window information parameters + * @param {string} params.clientWindow Window identifier + * @param {string} params.state Window state from WindowState + * @param {number} params.width Window width + * @param {number} params.height Window height + * @param {number} params.x Window x coordinate + * @param {number} params.y Window y coordinate + * @param {boolean} params.active Whether window is active and can receive keyboard input + */ + constructor({ clientWindow, state, width, height, x, y, active }) { + this.clientWindow = clientWindow + this.state = state + this.width = width + this.height = height + this.x = x + this.y = y + this.active = active + } + + static fromJson(json) { + return new ClientWindowInfo({ + ...json, + state: json.state.toLowerCase(), + }) + } +} + +module.exports = { WindowState, ClientWindowInfo } diff --git a/javascript/node/selenium-webdriver/test/bidi/browser_test.js b/javascript/node/selenium-webdriver/test/bidi/browser_test.js index ace03a47b8ec2..e609d9a8977bb 100644 --- a/javascript/node/selenium-webdriver/test/bidi/browser_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/browser_test.js @@ -21,20 +21,21 @@ const assert = require('node:assert') const { suite } = require('../../lib/test') const { Browser } = require('selenium-webdriver') const BrowserBiDi = require('selenium-webdriver/bidi/browser') +const { WindowState } = require('selenium-webdriver/bidi/browser') suite( function (env) { - describe('BiDi Browser', function () { - let driver + let driver - beforeEach(async function () { - driver = await env.builder().build() - }) + beforeEach(async function () { + driver = await env.builder().build() + }) - afterEach(function () { - return driver.quit() - }) + afterEach(function () { + return driver.quit() + }) + describe('BiDi Browser', function () { it('can create a user context', async function () { const browser = await BrowserBiDi(driver) @@ -79,6 +80,25 @@ suite( await browser.removeUserContext(userContext1) }) }) + + describe('Client Windows', function () { + it('can get client windows', async function () { + const browser = await BrowserBiDi(driver) + const windows = await browser.getClientWindows() + + assert(Array.isArray(windows)) + assert(windows.length > 0) + + const window = windows[0] + assert(window.clientWindow) + assert(Object.values(WindowState).includes(window.state)) + assert(Number.isInteger(window.width) && window.width > 0) + assert(Number.isInteger(window.height) && window.height > 0) + assert(Number.isInteger(window.x)) + assert(Number.isInteger(window.y)) + assert(typeof window.active === 'boolean') + }) + }) }, { browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] }, )