Path: blob/trunk/javascript/selenium-webdriver/test/bidi/browsingcontext_inspector_test.js
2885 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 { Browser, By } = require('selenium-webdriver')21const { Pages, suite, ignore } = require('../../lib/test')22const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')23const BrowsingContextInspector = require('selenium-webdriver/bidi/browsingContextInspector')24const until = require('selenium-webdriver/lib/until')2526suite(27function (env) {28let driver29let browsingcontextInspector3031beforeEach(async function () {32driver = await env.builder().build()33})3435afterEach(async function () {36await browsingcontextInspector.close()37await driver.quit()38})3940describe('Browsing Context Inspector', function () {41it('can listen to window browsing context created event', async function () {42let contextInfo = null43browsingcontextInspector = await BrowsingContextInspector(driver)44await browsingcontextInspector.onBrowsingContextCreated((entry) => {45contextInfo = entry46})4748await driver.switchTo().newWindow('window')49const windowHandle = await driver.getWindowHandle()50assert.equal(contextInfo.id, windowHandle)51assert.equal(contextInfo.url, 'about:blank')52assert.equal(contextInfo.children, null)53assert.equal(contextInfo.parentBrowsingContext, null)54})5556it('can listen to browsing context destroyed event', async function () {57let contextInfo = null58browsingcontextInspector = await BrowsingContextInspector(driver)59await browsingcontextInspector.onBrowsingContextDestroyed((entry) => {60contextInfo = entry61})6263await driver.switchTo().newWindow('window')6465const windowHandle = await driver.getWindowHandle()66await driver.close()6768assert.equal(contextInfo.id, windowHandle)69assert.equal(contextInfo.url, 'about:blank')7071assert(72Array.isArray(contextInfo.children) && contextInfo.children.length === 0,73'children should be an empty array',74)75assert.equal(contextInfo.parentBrowsingContext, null)76})7778it('can listen to tab browsing context created event', async function () {79let contextInfo = null80browsingcontextInspector = await BrowsingContextInspector(driver)81await browsingcontextInspector.onBrowsingContextCreated((entry) => {82contextInfo = entry83})8485await driver.switchTo().newWindow('tab')86const tabHandle = await driver.getWindowHandle()8788assert.equal(contextInfo.id, tabHandle)89assert.equal(contextInfo.url, 'about:blank')90assert.equal(contextInfo.children, null)91assert.equal(contextInfo.parentBrowsingContext, null)92})9394it('can listen to dom content loaded event', async function () {95browsingcontextInspector = await BrowsingContextInspector(driver)96let navigationInfo = null97await browsingcontextInspector.onDomContentLoaded((entry) => {98navigationInfo = entry99})100101const browsingContext = await BrowsingContext(driver, {102browsingContextId: await driver.getWindowHandle(),103})104await browsingContext.navigate(Pages.logEntryAdded, 'complete')105106assert.equal(navigationInfo.browsingContextId, browsingContext.id)107assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))108})109110it('can listen to browsing context loaded event', async function () {111let navigationInfo = null112browsingcontextInspector = await BrowsingContextInspector(driver)113114await browsingcontextInspector.onBrowsingContextLoaded((entry) => {115navigationInfo = entry116})117const browsingContext = await BrowsingContext(driver, {118browsingContextId: await driver.getWindowHandle(),119})120await browsingContext.navigate(Pages.logEntryAdded, 'complete')121122assert.equal(navigationInfo.browsingContextId, browsingContext.id)123assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))124})125126ignore(env.browsers(Browser.CHROME, Browser.EDGE)).it(127'can listen to navigation started event',128async function () {129let navigationInfo = null130const browsingConextInspector = await BrowsingContextInspector(driver)131132await browsingConextInspector.onNavigationStarted((entry) => {133navigationInfo = entry134})135136const browsingContext = await BrowsingContext(driver, {137browsingContextId: await driver.getWindowHandle(),138})139140await browsingContext.navigate(Pages.logEntryAdded, 'complete')141142assert.equal(navigationInfo.browsingContextId, browsingContext.id)143assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))144},145)146147ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(148'can listen to fragment navigated event',149async function () {150let navigationInfo = null151const browsingConextInspector = await BrowsingContextInspector(driver)152153const browsingContext = await BrowsingContext(driver, {154browsingContextId: await driver.getWindowHandle(),155})156await browsingContext.navigate(Pages.linkedImage, 'complete')157158await browsingConextInspector.onFragmentNavigated((entry) => {159navigationInfo = entry160})161162await browsingContext.navigate(Pages.linkedImage + '#linkToAnchorOnThisPage', 'complete')163164// Chrome/Edge do not return the window's browsing context id as per the spec.165// This assertion fails.166assert.equal(navigationInfo.browsingContextId, browsingContext.id)167assert(navigationInfo.url.includes('linkToAnchorOnThisPage'))168},169)170171ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(172'can listen to user prompt opened event',173async function () {174let userpromptOpened = null175browsingcontextInspector = await BrowsingContextInspector(driver)176177const browsingContext = await BrowsingContext(driver, {178browsingContextId: await driver.getWindowHandle(),179})180181await browsingcontextInspector.onUserPromptOpened((entry) => {182userpromptOpened = entry183})184185await driver.get(Pages.alertsPage)186187await driver.findElement(By.id('alert')).click()188189await driver.wait(until.alertIsPresent())190191await browsingContext.handleUserPrompt(true)192193// Chrome/Edge do not return the window's browsing context id as per the spec.194// This assertion fails.195// It is probably a bug in the Chrome/Edge driver.196assert.equal(userpromptOpened.browsingContextId, browsingContext.id)197assert.equal(userpromptOpened.type, 'alert')198},199)200201ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(202'can listen to user prompt closed event',203async function () {204const windowHandle = await driver.getWindowHandle()205let userpromptClosed = null206browsingcontextInspector = await BrowsingContextInspector(driver, windowHandle)207208const browsingContext = await BrowsingContext(driver, {209browsingContextId: windowHandle,210})211212await driver.get(Pages.alertsPage)213214await driver.findElement(By.id('prompt')).click()215216await driver.wait(until.alertIsPresent())217218await browsingcontextInspector.onUserPromptClosed((entry) => {219userpromptClosed = entry220})221222await browsingContext.handleUserPrompt(true, 'selenium')223224// Chrome/Edge do not return the window's browsing context id as per the spec.225// This assertion fails.226// It is probably a bug in the Chrome/Edge driver.227assert.equal(userpromptClosed.browsingContextId, browsingContext.id)228assert.equal(userpromptClosed.accepted, true)229assert.equal(userpromptClosed.userText, 'selenium')230},231)232})233},234{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },235)236237238