Path: blob/trunk/javascript/selenium-webdriver/test/bidi/browsingcontext_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 until = require('selenium-webdriver/lib/until')24const { Origin, CaptureScreenshotParameters } = require('selenium-webdriver/bidi/captureScreenshotParameters')25const { BoxClipRectangle, ElementClipRectangle } = require('selenium-webdriver/bidi/clipRectangle')26const { CreateContextParameters } = require('selenium-webdriver/bidi/createContextParameters')27const BrowserBiDi = require('selenium-webdriver/bidi/browser')2829suite(30function (env) {31let driver3233beforeEach(async function () {34driver = await env.builder().build()35})3637afterEach(async function () {38await driver.quit()39})4041describe('Browsing Context', function () {42let startIndex = 043let endIndex = 544let pdfMagicNumber = 'JVBER'45let pngMagicNumber = 'iVBOR'4647it('can create a browsing context for given id', async function () {48const id = await driver.getWindowHandle()49const browsingContext = await BrowsingContext(driver, {50browsingContextId: id,51})52assert.equal(browsingContext.id, id)53})5455it('can create a window', async function () {56const browsingContext = await BrowsingContext(driver, {57type: 'window',58})59assert.notEqual(browsingContext.id, null)60})6162it('can create a window with a reference context', async function () {63const browsingContext = await BrowsingContext(driver, {64type: 'window',65createParameters: new CreateContextParameters().referenceContext(await driver.getWindowHandle()),66})67assert.notEqual(browsingContext.id, null)68})6970it('can create a tab with all parameters', async function () {71const browser = await BrowserBiDi(driver)72const userContext = await browser.createUserContext()73const browsingContext = await BrowsingContext(driver, {74type: 'window',75createParameters: new CreateContextParameters()76.referenceContext(await driver.getWindowHandle())77.background(true)78.userContext(userContext),79})80assert.notEqual(browsingContext.id, null)81assert.notEqual(browsingContext.id, await driver.getWindowHandle())82})8384it('can create a tab', async function () {85const browsingContext = await BrowsingContext(driver, {86type: 'tab',87})88assert.notEqual(browsingContext.id, null)89})9091it('can create a tab with a reference context', async function () {92const browsingContext = await BrowsingContext(driver, {93type: 'tab',94referenceContext: new CreateContextParameters().referenceContext(await driver.getWindowHandle()),95})96assert.notEqual(browsingContext.id, null)97})9899it('can navigate to a url', async function () {100const browsingContext = await BrowsingContext(driver, {101type: 'tab',102})103104let info = await browsingContext.navigate(Pages.logEntryAdded)105106assert.notEqual(browsingContext.id, null)107assert.notEqual(info.navigationId, null)108assert(info.url.includes('/bidi/logEntryAdded.html'))109})110111it('can navigate to a url with readiness state', async function () {112const browsingContext = await BrowsingContext(driver, {113type: 'tab',114})115116const info = await browsingContext.navigate(Pages.logEntryAdded, 'complete')117118assert.notEqual(browsingContext.id, null)119assert.notEqual(info.navigationId, null)120assert(info.url.includes('/bidi/logEntryAdded.html'))121})122123it('can get tree with a child', async function () {124const browsingContextId = await driver.getWindowHandle()125const parentWindow = await BrowsingContext(driver, {126browsingContextId: browsingContextId,127})128await parentWindow.navigate(Pages.iframePage, 'complete')129130const contextInfo = await parentWindow.getTree()131assert.equal(contextInfo.children.length, 1)132assert.equal(contextInfo.id, browsingContextId)133assert(contextInfo.children[0]['url'].includes('formPage.html'))134})135136it('can get tree with depth', async function () {137const browsingContextId = await driver.getWindowHandle()138const parentWindow = await BrowsingContext(driver, {139browsingContextId: browsingContextId,140})141await parentWindow.navigate(Pages.iframePage, 'complete')142143const contextInfo = await parentWindow.getTree(0)144assert.equal(contextInfo.children, null)145assert.equal(contextInfo.id, browsingContextId)146})147148it('can close a window', async function () {149const window1 = await BrowsingContext(driver, { type: 'window' })150const window2 = await BrowsingContext(driver, { type: 'window' })151152await window2.close()153154assert.doesNotThrow(async function () {155await window1.getTree()156})157await assert.rejects(window2.getTree(), { message: 'no such frame' })158})159160it('can print PDF with total pages', async function () {161const id = await driver.getWindowHandle()162const browsingContext = await BrowsingContext(driver, {163browsingContextId: id,164})165166await driver.get(Pages.printPage)167const result = await browsingContext.printPage()168169let base64Code = result.data.slice(startIndex, endIndex)170assert.strictEqual(base64Code, pdfMagicNumber)171})172173it('can print PDF with all valid parameters', async function () {174const id = await driver.getWindowHandle()175const browsingContext = await BrowsingContext(driver, {176browsingContextId: id,177})178179await driver.get(Pages.printPage)180const result = await browsingContext.printPage({181orientation: 'landscape',182scale: 1,183background: true,184width: 30,185height: 30,186top: 1,187bottom: 1,188left: 1,189right: 1,190shrinkToFit: true,191pageRanges: ['1-2'],192})193194let base64Code = result.data.slice(startIndex, endIndex)195assert.strictEqual(base64Code, pdfMagicNumber)196})197198it('can take screenshot', async function () {199const id = await driver.getWindowHandle()200const browsingContext = await BrowsingContext(driver, {201browsingContextId: id,202})203204const response = await browsingContext.captureScreenshot()205const base64code = response.slice(startIndex, endIndex)206assert.equal(base64code, pngMagicNumber)207})208209it('can take screenshot with all parameters for box screenshot', async function () {210const id = await driver.getWindowHandle()211const browsingContext = await BrowsingContext(driver, {212browsingContextId: id,213})214215let captureScreenshotParams = new CaptureScreenshotParameters()216captureScreenshotParams.origin(Origin.VIEWPORT).clipRectangle(new BoxClipRectangle(5, 5, 10, 10))217218const response = await browsingContext.captureScreenshot(captureScreenshotParams)219220const base64code = response.slice(startIndex, endIndex)221assert.equal(base64code, pngMagicNumber)222})223224it('can take screenshot with all parameters for element screenshot', async function () {225const id = await driver.getWindowHandle()226const browsingContext = await BrowsingContext(driver, {227browsingContextId: id,228})229230await driver.get(Pages.formPage)231const element = await driver.findElement(By.id('checky'))232const elementId = await element.getId()233234let captureScreenshotParams = new CaptureScreenshotParameters()235captureScreenshotParams.origin(Origin.VIEWPORT).clipRectangle(new ElementClipRectangle(elementId))236237const response = await browsingContext.captureScreenshot(captureScreenshotParams)238239const base64code = response.slice(startIndex, endIndex)240assert.equal(base64code, pngMagicNumber)241})242243it('can take box screenshot', async function () {244const id = await driver.getWindowHandle()245const browsingContext = await BrowsingContext(driver, {246browsingContextId: id,247})248249const response = await browsingContext.captureBoxScreenshot(5, 5, 10, 10)250251const base64code = response.slice(startIndex, endIndex)252assert.equal(base64code, pngMagicNumber)253})254255it('can take element screenshot', async function () {256const id = await driver.getWindowHandle()257const browsingContext = await BrowsingContext(driver, {258browsingContextId: id,259})260261await driver.get(Pages.formPage)262const element = await driver.findElement(By.id('checky'))263const elementId = await element.getId()264const response = await browsingContext.captureElementScreenshot(elementId)265266const base64code = response.slice(startIndex, endIndex)267assert.equal(base64code, pngMagicNumber)268})269270it('can activate a browsing context', async function () {271const id = await driver.getWindowHandle()272const window1 = await BrowsingContext(driver, {273browsingContextId: id,274})275276await BrowsingContext(driver, {277type: 'window',278})279280const result = await driver.executeScript('return document.hasFocus();')281282assert.equal(result, false)283284await window1.activate()285const result2 = await driver.executeScript('return document.hasFocus();')286287assert.equal(result2, true)288})289290it('can handle user prompt', async function () {291const id = await driver.getWindowHandle()292const browsingContext = await BrowsingContext(driver, {293browsingContextId: id,294})295296await driver.get(Pages.alertsPage)297298await driver.findElement(By.id('alert')).click()299300await driver.wait(until.alertIsPresent())301302await browsingContext.handleUserPrompt()303304const result = await driver.getTitle()305306assert.equal(result, 'Testing Alerts')307})308309it('can accept user prompt', async function () {310const id = await driver.getWindowHandle()311const browsingContext = await BrowsingContext(driver, {312browsingContextId: id,313})314315await driver.get(Pages.alertsPage)316317await driver.findElement(By.id('alert')).click()318319await driver.wait(until.alertIsPresent())320321await browsingContext.handleUserPrompt(true)322323const result = await driver.getTitle()324325assert.equal(result, 'Testing Alerts')326})327328it('can dismiss user prompt', async function () {329const id = await driver.getWindowHandle()330const browsingContext = await BrowsingContext(driver, {331browsingContextId: id,332})333334await driver.get(Pages.alertsPage)335336await driver.findElement(By.id('alert')).click()337338await driver.wait(until.alertIsPresent())339340await browsingContext.handleUserPrompt(false)341342const result = await driver.getTitle()343344assert.equal(result, 'Testing Alerts')345})346347it('can pass user text to user prompt', async function () {348const id = await driver.getWindowHandle()349const browsingContext = await BrowsingContext(driver, {350browsingContextId: id,351})352353await driver.get(Pages.userpromptPage)354355await driver.findElement(By.id('alert')).click()356357await driver.wait(until.alertIsPresent())358359const userText = 'Selenium automates browsers'360361await browsingContext.handleUserPrompt(undefined, userText)362363const result = await driver.getPageSource()364assert.equal(result.includes(userText), true)365})366367it('can accept user prompt with user text', async function () {368const id = await driver.getWindowHandle()369const browsingContext = await BrowsingContext(driver, {370browsingContextId: id,371})372373await driver.get(Pages.userpromptPage)374375await driver.findElement(By.id('alert')).click()376377await driver.wait(until.alertIsPresent())378379const userText = 'Selenium automates browsers'380381await browsingContext.handleUserPrompt(true, userText)382383const result = await driver.getPageSource()384assert.equal(result.includes(userText), true)385})386387it('can dismiss user prompt with user text', async function () {388const id = await driver.getWindowHandle()389const browsingContext = await BrowsingContext(driver, {390browsingContextId: id,391})392393await driver.get(Pages.userpromptPage)394395await driver.findElement(By.id('alert')).click()396397await driver.wait(until.alertIsPresent())398399const userText = 'Selenium automates browsers'400401await browsingContext.handleUserPrompt(false, userText)402403const result = await driver.getPageSource()404assert.equal(result.includes(userText), false)405})406407xit('can reload a browsing context', async function () {408const id = await driver.getWindowHandle()409const browsingContext = await BrowsingContext(driver, {410browsingContextId: id,411})412413const result = await browsingContext.navigate(Pages.logEntryAdded, 'complete')414415await browsingContext.reload()416assert.equal(result.navigationId, null)417assert(result.url.includes('/bidi/logEntryAdded.html'))418})419420it('can reload with readiness state', async function () {421const id = await driver.getWindowHandle()422const browsingContext = await BrowsingContext(driver, {423browsingContextId: id,424})425426const result = await browsingContext.navigate(Pages.logEntryAdded, 'complete')427428await browsingContext.reload(undefined, 'complete')429assert.notEqual(result.navigationId, null)430assert(result.url.includes('/bidi/logEntryAdded.html'))431})432433it('can set viewport', async function () {434const id = await driver.getWindowHandle()435const browsingContext = await BrowsingContext(driver, {436browsingContextId: id,437})438439await driver.get(Pages.blankPage)440441await browsingContext.setViewport(250, 300)442443const result = await driver.executeScript('return [window.innerWidth, window.innerHeight];')444assert.equal(result[0], 250)445assert.equal(result[1], 300)446})447448ignore(env.browsers(Browser.FIREFOX)).it('can set viewport with device pixel ratio', async function () {449const id = await driver.getWindowHandle()450const browsingContext = await BrowsingContext(driver, {451browsingContextId: id,452})453454await driver.get(Pages.blankPage)455456await browsingContext.setViewport(250, 300, 5)457458const result = await driver.executeScript('return [window.innerWidth, window.innerHeight];')459assert.equal(result[0], 250)460assert.equal(result[1], 300)461462const devicePixelRatio = await driver.executeScript('return window.devicePixelRatio;')463assert.equal(devicePixelRatio, 5)464})465466it('Get All Top level browsing contexts', async () => {467const id = await driver.getWindowHandle()468const window1 = await BrowsingContext(driver, {469browsingContextId: id,470})471472await BrowsingContext(driver, { type: 'window' })473474const res = await window1.getTopLevelContexts()475assert.equal(res.length, 2)476})477})478},479{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },480)481482483