Path: blob/trunk/javascript/selenium-webdriver/test/bidi/permissions_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 { Pages, suite } = require('../../lib/test')21const { Browser } = require('selenium-webdriver')22const BrowserBiDi = require('selenium-webdriver/bidi/browser')23const getScriptManager = require('selenium-webdriver/bidi/scriptManager')24const { getPermissionInstance, PermissionState } = require('selenium-webdriver/bidi/external/permissions')25const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')26const { CreateContextParameters } = require('selenium-webdriver/bidi/createContextParameters')2728suite(29function (env) {30describe('BiDi Permissions', function () {31let driver, permission, browser, script3233const GET_GEOLOCATION_PERMISSION =34"async () => { const perm = await navigator.permissions.query({ name: 'geolocation' }); return perm.state; }"35const GET_ORIGIN = '() => {return window.location.origin;}'3637beforeEach(async function () {38driver = await env.builder().build()39permission = await getPermissionInstance(driver)40browser = await BrowserBiDi(driver)41script = await getScriptManager([], driver)42})4344afterEach(function () {45return driver.quit()46})4748it('can set permission to granted', async function () {49const context = await BrowsingContext(driver, { type: 'tab' })50await context.navigate(Pages.blankPage, 'complete')5152const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, [])53const originValue = origin.result.value5455await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, originValue)5657const result = await script.callFunctionInBrowsingContext(context.id, GET_GEOLOCATION_PERMISSION, true, [])58assert.strictEqual(result.result.value, PermissionState.GRANTED)59})6061it('can set permission to denied', async function () {62const context = await BrowsingContext(driver, { type: 'tab' })63await context.navigate(Pages.blankPage, 'complete')6465const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, [])6667const originValue = origin.result.value68await permission.setPermission({ name: 'geolocation' }, PermissionState.DENIED, originValue)6970const result = await script.callFunctionInBrowsingContext(context.id, GET_GEOLOCATION_PERMISSION, true, [])71assert.strictEqual(result.result.value, PermissionState.DENIED)72})7374it('can set permission to prompt', async function () {75const context = await BrowsingContext(driver, { type: 'tab' })76await context.navigate(Pages.blankPage, 'complete')7778const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, [])7980const originValue = origin.result.value81await permission.setPermission({ name: 'geolocation' }, PermissionState.DENIED, originValue)8283await permission.setPermission({ name: 'geolocation' }, PermissionState.PROMPT, originValue)8485const result = await script.callFunctionInBrowsingContext(context.id, GET_GEOLOCATION_PERMISSION, true, [])86assert.strictEqual(result.result.value, PermissionState.PROMPT)87})8889it('can set permission for a user context', async function () {90const userContext = await browser.createUserContext()9192const context1 = await BrowsingContext(driver, { type: 'tab' })93const context2 = await BrowsingContext(driver, {94type: 'tab',95createParameters: new CreateContextParameters().userContext(userContext),96})9798await context1.navigate(Pages.blankPage, 'complete')99await context2.navigate(Pages.blankPage, 'complete')100101const origin = await script.callFunctionInBrowsingContext(context1.id, GET_ORIGIN, true, [])102const originValue = origin.result.value103104// Get the actual permission states from each context105const originalTabPermission = await script.callFunctionInBrowsingContext(106context1.id,107GET_GEOLOCATION_PERMISSION,108true,109[],110)111112const newTabPermission = await script.callFunctionInBrowsingContext(113context2.id,114GET_GEOLOCATION_PERMISSION,115true,116[],117)118119const originalTabState = originalTabPermission.result.value120const newTabState = newTabPermission.result.value121122// Set permission only for the user context123await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, originValue, userContext)124125// Check that the original tab's permission state hasn't changed126const originalTabUpdatedPermission = await script.callFunctionInBrowsingContext(127context1.id,128GET_GEOLOCATION_PERMISSION,129true,130[],131)132assert.strictEqual(originalTabUpdatedPermission.result.value, originalTabState)133134// Check that the new tab's permission state has been updated to GRANTED135const newTabUpdatedPermission = await script.callFunctionInBrowsingContext(136context2.id,137GET_GEOLOCATION_PERMISSION,138true,139[],140)141assert.strictEqual(newTabUpdatedPermission.result.value, PermissionState.GRANTED)142})143})144},145{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },146)147148149