|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +'use strict' |
| 19 | + |
| 20 | +const assert = require('node:assert') |
| 21 | +const { Pages, suite } = require('../../lib/test') |
| 22 | +const { Browser } = require('selenium-webdriver') |
| 23 | +const BrowserBiDi = require('selenium-webdriver/bidi/browser') |
| 24 | +const getScriptManager = require('selenium-webdriver/bidi/scriptManager') |
| 25 | +const { getPermissionInstance, PermissionState } = require('selenium-webdriver/bidi/external/permissions') |
| 26 | +const BrowsingContext = require('selenium-webdriver/bidi/browsingContext') |
| 27 | +const { CreateContextParameters } = require('selenium-webdriver/bidi/createContextParameters') |
| 28 | + |
| 29 | +suite( |
| 30 | + function (env) { |
| 31 | + describe('BiDi Permissions', function () { |
| 32 | + let driver, permission, browser, script |
| 33 | + |
| 34 | + const GET_GEOLOCATION_PERMISSION = |
| 35 | + "async () => { const perm = await navigator.permissions.query({ name: 'geolocation' }); return perm.state; }" |
| 36 | + const GET_ORIGIN = '() => {return window.location.origin;}' |
| 37 | + |
| 38 | + beforeEach(async function () { |
| 39 | + driver = await env.builder().build() |
| 40 | + permission = await getPermissionInstance(driver) |
| 41 | + browser = await BrowserBiDi(driver) |
| 42 | + script = await getScriptManager([], driver) |
| 43 | + }) |
| 44 | + |
| 45 | + afterEach(function () { |
| 46 | + return driver.quit() |
| 47 | + }) |
| 48 | + |
| 49 | + it('can set permission to granted', async function () { |
| 50 | + const context = await BrowsingContext(driver, { type: 'tab' }) |
| 51 | + await context.navigate(Pages.blankPage, 'complete') |
| 52 | + |
| 53 | + const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, []) |
| 54 | + const originValue = origin.result.value |
| 55 | + |
| 56 | + await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, originValue) |
| 57 | + |
| 58 | + const result = await script.callFunctionInBrowsingContext(context.id, GET_GEOLOCATION_PERMISSION, true, []) |
| 59 | + assert.strictEqual(result.result.value, PermissionState.GRANTED) |
| 60 | + }) |
| 61 | + |
| 62 | + it('can set permission to denied', async function () { |
| 63 | + const context = await BrowsingContext(driver, { type: 'tab' }) |
| 64 | + await context.navigate(Pages.blankPage, 'complete') |
| 65 | + |
| 66 | + const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, []) |
| 67 | + |
| 68 | + const originValue = origin.result.value |
| 69 | + await permission.setPermission({ name: 'geolocation' }, PermissionState.DENIED, originValue) |
| 70 | + |
| 71 | + const result = await script.callFunctionInBrowsingContext(context.id, GET_GEOLOCATION_PERMISSION, true, []) |
| 72 | + assert.strictEqual(result.result.value, PermissionState.DENIED) |
| 73 | + }) |
| 74 | + |
| 75 | + it('can set permission to prompt', async function () { |
| 76 | + const context = await BrowsingContext(driver, { type: 'tab' }) |
| 77 | + await context.navigate(Pages.blankPage, 'complete') |
| 78 | + |
| 79 | + const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, []) |
| 80 | + |
| 81 | + const originValue = origin.result.value |
| 82 | + await permission.setPermission({ name: 'geolocation' }, PermissionState.DENIED, originValue) |
| 83 | + |
| 84 | + await permission.setPermission({ name: 'geolocation' }, PermissionState.PROMPT, originValue) |
| 85 | + |
| 86 | + const result = await script.callFunctionInBrowsingContext(context.id, GET_GEOLOCATION_PERMISSION, true, []) |
| 87 | + assert.strictEqual(result.result.value, PermissionState.PROMPT) |
| 88 | + }) |
| 89 | + |
| 90 | + it('can set permission for a user context', async function () { |
| 91 | + const userContext = await browser.createUserContext() |
| 92 | + |
| 93 | + const context1 = await BrowsingContext(driver, { type: 'tab' }) |
| 94 | + const context2 = await BrowsingContext(driver, { |
| 95 | + type: 'tab', |
| 96 | + createParameters: new CreateContextParameters().userContext(userContext), |
| 97 | + }) |
| 98 | + |
| 99 | + await context1.navigate(Pages.blankPage, 'complete') |
| 100 | + await context2.navigate(Pages.blankPage, 'complete') |
| 101 | + |
| 102 | + const origin = await script.callFunctionInBrowsingContext(context1.id, GET_ORIGIN, true, []) |
| 103 | + const originValue = origin.result.value |
| 104 | + |
| 105 | + // Get the actual permission states from each context |
| 106 | + const originalTabPermission = await script.callFunctionInBrowsingContext( |
| 107 | + context1.id, |
| 108 | + GET_GEOLOCATION_PERMISSION, |
| 109 | + true, |
| 110 | + [], |
| 111 | + ) |
| 112 | + |
| 113 | + const newTabPermission = await script.callFunctionInBrowsingContext( |
| 114 | + context2.id, |
| 115 | + GET_GEOLOCATION_PERMISSION, |
| 116 | + true, |
| 117 | + [], |
| 118 | + ) |
| 119 | + |
| 120 | + const originalTabState = originalTabPermission.result.value |
| 121 | + const newTabState = newTabPermission.result.value |
| 122 | + |
| 123 | + // Set permission only for the user context |
| 124 | + await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, originValue, userContext) |
| 125 | + |
| 126 | + // Check that the original tab's permission state hasn't changed |
| 127 | + const originalTabUpdatedPermission = await script.callFunctionInBrowsingContext( |
| 128 | + context1.id, |
| 129 | + GET_GEOLOCATION_PERMISSION, |
| 130 | + true, |
| 131 | + [], |
| 132 | + ) |
| 133 | + assert.strictEqual(originalTabUpdatedPermission.result.value, originalTabState) |
| 134 | + |
| 135 | + // Check that the new tab's permission state has been updated to GRANTED |
| 136 | + const newTabUpdatedPermission = await script.callFunctionInBrowsingContext( |
| 137 | + context2.id, |
| 138 | + GET_GEOLOCATION_PERMISSION, |
| 139 | + true, |
| 140 | + [], |
| 141 | + ) |
| 142 | + assert.strictEqual(newTabUpdatedPermission.result.value, PermissionState.GRANTED) |
| 143 | + }) |
| 144 | + }) |
| 145 | + }, |
| 146 | + { browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] }, |
| 147 | +) |
0 commit comments