Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/browser_test.js
11822 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const { suite } = require('../../../lib/test')21const { Browser } = require('selenium-webdriver')2223// Generated Browser domain class — produced by generate_bidi.mjs from the24// WebDriver BiDi CDDL spec. This test exercises the generated API to verify25// that the auto-generated code works correctly against a real browser.26const { Browser: GeneratedBrowser } = require('selenium-webdriver/bidi/generated/browser')2728suite(29function (env) {30let driver3132beforeEach(async function () {33driver = await env.builder().build()34})3536afterEach(function () {37return driver.quit()38})3940describe('Generated BiDi Browser domain', function () {41// The generated Browser class uses a static factory method Browser.create(driver)42// instead of the hand-written getBrowserInstance(driver) helper function.4344it('can create a user context', async function () {45const browser = await GeneratedBrowser.create(driver)4647// createUserContext({}) returns BrowserCreateUserContextResult = { userContext: string }48// All params fields are optional per the spec so an empty object is valid.49const result = await browser.createUserContext({})5051assert.notEqual(result.userContext, null)52assert.strictEqual(typeof result.userContext, 'string')5354await browser.removeUserContext({ userContext: result.userContext })55})5657it('can get user contexts', async function () {58const browser = await GeneratedBrowser.create(driver)5960const result1 = await browser.createUserContext({})61const result2 = await browser.createUserContext({})6263// getUserContexts() returns BrowserGetUserContextsResult = { userContexts: BrowserUserContextInfo[] }64// Each BrowserUserContextInfo has a { userContext: string } field.65const { userContexts } = await browser.getUserContexts()6667assert.strictEqual(userContexts.length >= 2, true)6869const ids = userContexts.map((ctx) => ctx.userContext)70assert.ok(ids.includes(result1.userContext))71assert.ok(ids.includes(result2.userContext))7273await browser.removeUserContext({ userContext: result1.userContext })74await browser.removeUserContext({ userContext: result2.userContext })75})7677it('can remove a user context', async function () {78const browser = await GeneratedBrowser.create(driver)7980const result1 = await browser.createUserContext({})81const result2 = await browser.createUserContext({})8283const before = await browser.getUserContexts()84const beforeIds = before.userContexts.map((ctx) => ctx.userContext)85assert.ok(beforeIds.includes(result1.userContext))86assert.ok(beforeIds.includes(result2.userContext))8788// removeUserContext takes { userContext: string } — an object, not a bare string89await browser.removeUserContext({ userContext: result2.userContext })9091const after = await browser.getUserContexts()92const afterIds = after.userContexts.map((ctx) => ctx.userContext)93assert.ok(afterIds.includes(result1.userContext))94assert.ok(!afterIds.includes(result2.userContext))9596await browser.removeUserContext({ userContext: result1.userContext })97})9899it('can get client windows', async function () {100const browser = await GeneratedBrowser.create(driver)101102// getClientWindows() returns BrowserGetClientWindowsResult = { clientWindows: BrowserClientWindowInfo[] }103const { clientWindows } = await browser.getClientWindows()104105assert.ok(Array.isArray(clientWindows))106assert.ok(clientWindows.length > 0)107108const window = clientWindows[0]109assert.strictEqual(typeof window.clientWindow, 'string')110assert.ok(['fullscreen', 'maximized', 'minimized', 'normal'].includes(window.state))111assert.ok(Number.isInteger(window.width) && window.width > 0)112assert.ok(Number.isInteger(window.height) && window.height > 0)113assert.ok(Number.isInteger(window.x))114assert.ok(Number.isInteger(window.y))115assert.strictEqual(typeof window.active, 'boolean')116})117})118},119{ browsers: [Browser.CHROME, Browser.FIREFOX] },120)121122123