Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/browsing_context_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, ignore, Pages } = require('../../../lib/test')21const { Browser, until } = require('selenium-webdriver')2223// Generated BrowsingContext domain class produced by generate_bidi.mjs.24const { BrowsingContext } = require('selenium-webdriver/bidi/generated/browsing_context')2526suite(27function (env) {28let driver29let browsingContext3031beforeEach(async function () {32driver = await env.builder().build()33browsingContext = await BrowsingContext.create(driver)34})3536afterEach(function () {37return driver.quit()38})3940describe('create and close', function () {41it('can create a new tab', async function () {42const result = await browsingContext.create({ type: 'tab' })4344assert.ok(result.context, 'context id should be present')45assert.strictEqual(typeof result.context, 'string')4647await browsingContext.close({ context: result.context })48})4950it('can create a new window', async function () {51const result = await browsingContext.create({ type: 'window' })5253assert.ok(result.context)54assert.strictEqual(typeof result.context, 'string')5556await browsingContext.close({ context: result.context })57})5859it('can get browsing context tree', async function () {60const contextId = await driver.getWindowHandle()61const treeResult = await browsingContext.getTree({ root: contextId })6263assert.ok(Array.isArray(treeResult.contexts))64assert.strictEqual(treeResult.contexts.length, 1)65assert.strictEqual(treeResult.contexts[0].context, contextId)66})6768it('can get tree with max depth', async function () {69const contextId = await driver.getWindowHandle()70const result = await browsingContext.getTree({ root: contextId, maxDepth: 1 })7172assert.ok(Array.isArray(result.contexts))73})74})7576describe('navigation', function () {77it('can navigate to a url', async function () {78const contextId = await driver.getWindowHandle()79const result = await browsingContext.navigate({80context: contextId,81url: Pages.emptyPage,82wait: 'complete',83})8485assert.ok(result.url.includes('emptyPage') || result.url.includes('empty'))86})8788it('can reload a page', async function () {89const contextId = await driver.getWindowHandle()90await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })9192const result = await browsingContext.reload({ context: contextId, wait: 'complete' })9394assert.ok(result.url)95})9697it('can traverse history back and forward', async function () {98const contextId = await driver.getWindowHandle()99await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })100await browsingContext.navigate({ context: contextId, url: Pages.logEntryAdded, wait: 'complete' })101102// Navigate back103await browsingContext.traverseHistory({ context: contextId, delta: -1 })104await driver.wait(until.urlContains('emptyPage'), 5000)105106const url = await driver.getCurrentUrl()107assert.ok(url.includes('emptyPage') || url.includes('empty'))108109// Navigate forward110await browsingContext.traverseHistory({ context: contextId, delta: 1 })111await driver.wait(until.urlContains('logEntryAdded'), 5000)112})113})114115describe('activate', function () {116it('can activate a browsing context', async function () {117const contextId = await driver.getWindowHandle()118await browsingContext.activate({ context: contextId })119})120})121122describe('viewport and screenshot', function () {123it('can set viewport size', async function () {124const contextId = await driver.getWindowHandle()125await browsingContext.setViewport({126context: contextId,127viewport: { width: 800, height: 600 },128})129})130131it('can capture a screenshot', async function () {132const contextId = await driver.getWindowHandle()133await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })134135const result = await browsingContext.captureScreenshot({ context: contextId })136137assert.ok(result.data, 'screenshot data should be present')138assert.strictEqual(typeof result.data, 'string')139assert.ok(result.data.length > 0)140})141142it('can capture screenshot with viewport origin', async function () {143const contextId = await driver.getWindowHandle()144await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })145146const result = await browsingContext.captureScreenshot({147context: contextId,148origin: 'viewport',149})150151assert.ok(result.data)152assert.ok(result.data.length > 0)153})154})155156describe('handleUserPrompt', function () {157it('can accept an alert', async function () {158const contextId = await driver.getWindowHandle()159await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })160161await driver.findElement({ id: 'alert' }).click()162await driver.wait(until.alertIsPresent(), 5000)163164// Dismiss the alert using the generated API165await browsingContext.handleUserPrompt({ context: contextId, accept: true })166})167168it('can dismiss a confirm dialog', async function () {169const contextId = await driver.getWindowHandle()170await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })171172await driver.findElement({ id: 'confirm' }).click()173await driver.wait(until.alertIsPresent(), 5000)174175await browsingContext.handleUserPrompt({ context: contextId, accept: false })176})177})178179describe('context lifecycle events', function () {180it('fires contextCreated when a new tab is opened', async function () {181let createdContext = null182183await browsingContext.onContextCreated((params) => {184createdContext = params185})186187const result = await browsingContext.create({ type: 'tab' })188189await driver.wait(() => createdContext !== null, 5000)190assert.ok(createdContext, 'contextCreated event should have fired')191assert.ok(createdContext.context)192193await browsingContext.close({ context: result.context })194})195196it('fires contextDestroyed when a tab is closed', async function () {197const created = await browsingContext.create({ type: 'tab' })198199let destroyedContext = null200await browsingContext.onContextDestroyed((params) => {201if (params.context === created.context) {202destroyedContext = params203}204})205206await browsingContext.close({ context: created.context })207208await driver.wait(() => destroyedContext !== null, 5000)209assert.ok(destroyedContext, 'contextDestroyed event should have fired')210})211})212213describe('navigation events', function () {214it('fires navigationCommitted on page navigate', async function () {215const contextId = await driver.getWindowHandle()216let navEvent = null217218await browsingContext.onNavigationCommitted((params) => {219if (params.context === contextId) {220navEvent = params221}222})223224await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })225await driver.wait(() => navEvent !== null, 5000)226227assert.ok(navEvent, 'navigationCommitted event should have fired')228assert.strictEqual(navEvent.context, contextId)229assert.ok(navEvent.url)230})231232it('fires fragmentNavigated on hash change', async function () {233const contextId = await driver.getWindowHandle()234await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })235236let fragmentEvent = null237await browsingContext.onFragmentNavigated((params) => {238if (params.context === contextId) {239fragmentEvent = params240}241})242243// Trigger a hash navigation244await driver.executeScript('window.location.hash = "section1"')245await driver.wait(() => fragmentEvent !== null, 5000)246247assert.ok(fragmentEvent, 'fragmentNavigated event should have fired')248})249})250251describe('user prompt events', function () {252it('fires userPromptOpened when an alert appears', async function () {253const contextId = await driver.getWindowHandle()254let promptOpened = null255256await browsingContext.onUserPromptOpened((params) => {257if (params.context === contextId) {258promptOpened = params259}260})261262await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })263await driver.findElement({ id: 'alert' }).click()264await driver.wait(() => promptOpened !== null, 5000)265266assert.ok(promptOpened, 'userPromptOpened should have fired')267assert.strictEqual(promptOpened.type, 'alert')268269await browsingContext.handleUserPrompt({ context: contextId, accept: true })270})271272it('fires userPromptClosed when an alert is handled', async function () {273const contextId = await driver.getWindowHandle()274let promptClosed = null275276await browsingContext.onUserPromptClosed((params) => {277if (params.context === contextId) {278promptClosed = params279}280})281282await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })283await driver.findElement({ id: 'alert' }).click()284await driver.wait(until.alertIsPresent(), 5000)285286await browsingContext.handleUserPrompt({ context: contextId, accept: true })287await driver.wait(() => promptClosed !== null, 5000)288289assert.ok(promptClosed, 'userPromptClosed should have fired')290assert.strictEqual(promptClosed.accepted, true)291})292})293294describe('locateNodes', function () {295it('can locate nodes by css selector', async function () {296const contextId = await driver.getWindowHandle()297await browsingContext.navigate({ context: contextId, url: Pages.logEntryAdded, wait: 'complete' })298299const result = await browsingContext.locateNodes({300context: contextId,301locator: { type: 'css', value: 'button' },302})303304assert.ok(Array.isArray(result.nodes))305assert.ok(result.nodes.length > 0)306})307308it('can locate nodes with maxNodeCount', async function () {309const contextId = await driver.getWindowHandle()310await browsingContext.navigate({ context: contextId, url: Pages.logEntryAdded, wait: 'complete' })311312const result = await browsingContext.locateNodes({313context: contextId,314locator: { type: 'css', value: 'button' },315maxNodeCount: 1,316})317318assert.ok(Array.isArray(result.nodes))319assert.strictEqual(result.nodes.length, 1)320})321})322},323{ browsers: [Browser.CHROME, Browser.FIREFOX] },324)325326327