Skip to content

Commit

Permalink
[js][bidi]: implement bidi getClientWindows command in browser modu…
Browse files Browse the repository at this point in the history
…le (#15248)
  • Loading branch information
navin772 authored Feb 17, 2025
1 parent 51b9af3 commit c97d9b7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 8 deletions.
17 changes: 17 additions & 0 deletions javascript/node/selenium-webdriver/bidi/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,6 +85,20 @@ class Browser {

await this.bidi.send(command)
}

/**
* Gets information about all client windows.
* @returns {Promise<ClientWindowInfo[]>} 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) {
Expand All @@ -92,3 +108,4 @@ async function getBrowserInstance(driver) {
}

module.exports = getBrowserInstance
module.exports.WindowState = WindowState
54 changes: 54 additions & 0 deletions javascript/node/selenium-webdriver/bidi/clientWindowInfo.js
Original file line number Diff line number Diff line change
@@ -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 }
36 changes: 28 additions & 8 deletions javascript/node/selenium-webdriver/test/bidi/browser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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] },
)

0 comments on commit c97d9b7

Please sign in to comment.