Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix formatting
  • Loading branch information
pujagani committed Aug 13, 2025
commit 0b722abe0fdf364c001319c355887205d13921f1
20 changes: 9 additions & 11 deletions javascript/selenium-webdriver/bidi/emulation/emulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
// specific language governing permissions and limitations
// under the License.

const GeolocationCoordinates = require("./geolocationCoordinates");
const GeolocationCoordinates = require('./geolocationCoordinates')

const GeolocationPositionError = Object.freeze({
type: 'positionUnavailable'
type: 'positionUnavailable',
})

class Emulation {
Expand All @@ -34,28 +34,26 @@ class Emulation {
this.bidi = await this._driver.getBidi()
}

async setGeolocationOverride(value, contexts= undefined, userContexts = undefined) {
async setGeolocationOverride(value, contexts = undefined, userContexts = undefined) {
const map = new Map()

if (value instanceof GeolocationCoordinates) {
map.set('coordinates', Object.fromEntries(value.asMap()))
} else if (value === GeolocationPositionError) {
map.set('error', value)
} else {
throw new Error(
'First argument must be a GeoCoordinates instance or GeolocationPositionError constant'
)
throw new Error('First argument must be a GeoCoordinates instance or GeolocationPositionError constant')
}

if (contexts !== undefined && typeof contexts === 'string') {
if (contexts !== undefined && typeof contexts === 'string') {
contexts = [contexts]
} else if (contexts !== undefined && !Array.isArray(contexts)) {
} else if (contexts !== undefined && !Array.isArray(contexts)) {
throw new Error('contexts must be a string or an array of strings')
}

map.set('contexts', contexts)

if (userContexts !== undefined && typeof userContexts === 'string') {
if (userContexts !== undefined && typeof userContexts === 'string') {
userContexts = [userContexts]
} else if (userContexts !== undefined && !Array.isArray(userContexts)) {
throw new Error('userContexts must be a string or an array of strings')
Expand All @@ -65,7 +63,7 @@ class Emulation {

const command = {
method: 'emulation.setGeolocationOverride',
params: Object.fromEntries(map)
params: Object.fromEntries(map),
}

const response = await this.bidi.send(command)
Expand All @@ -82,4 +80,4 @@ async function getEmulationInstance(driver) {
return instance
}

module.exports = { getEmulationInstance, GeolocationPositionError}
module.exports = { getEmulationInstance, GeolocationPositionError }
36 changes: 16 additions & 20 deletions javascript/selenium-webdriver/test/bidi/emulation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
'use strict'

const assert = require('node:assert')
const {Pages, suite} = require('../../lib/test')
const {Browser} = require('selenium-webdriver')
const { Pages, suite, ignore } = require('../../lib/test')
const { Browser } = require('selenium-webdriver')
const BrowserBiDi = require('selenium-webdriver/bidi/browser')
const getScriptManager = require('selenium-webdriver/bidi/scriptManager')
const {GeolocationPositionError, getEmulationInstance } = require('selenium-webdriver/bidi/emulation/emulation')
const { GeolocationPositionError, getEmulationInstance } = require('selenium-webdriver/bidi/emulation/emulation')
const GeolocationCoordinates = require('selenium-webdriver/bidi/emulation/geolocationCoordinates')
const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')
const {getPermissionInstance, PermissionState} = require('selenium-webdriver/bidi/external/permissions')
const {CreateContextParameters} = require('selenium-webdriver/bidi/createContextParameters')
const { getPermissionInstance, PermissionState } = require('selenium-webdriver/bidi/external/permissions')
const { CreateContextParameters } = require('selenium-webdriver/bidi/createContextParameters')

suite(
function (env) {
Expand Down Expand Up @@ -71,12 +71,12 @@ suite(

it('can override geolocation for browsing context', async function () {
const windowHandle = await driver.getWindowHandle()
const context = await BrowsingContext(driver, {browsingContextId: windowHandle})
const context = await BrowsingContext(driver, { browsingContextId: windowHandle })
await context.navigate(Pages.blankPage, 'complete')

const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, [])
const originValue = origin.result.value
await permission.setPermission({name: 'geolocation'}, PermissionState.GRANTED, originValue)
await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, originValue)

const coords = new GeolocationCoordinates(37.7749, -122.4194)

Expand All @@ -99,9 +99,9 @@ suite(
const createParams1 = new CreateContextParameters().userContext(userContext1)
const createParams2 = new CreateContextParameters().userContext(userContext2)

const context1 = await BrowsingContext(driver, {type: 'tab', createParameters: createParams1})
const context1 = await BrowsingContext(driver, { type: 'tab', createParameters: createParams1 })

const context2 = await BrowsingContext(driver, {type: 'tab', createParameters: createParams2})
const context2 = await BrowsingContext(driver, { type: 'tab', createParameters: createParams2 })

const coords = new GeolocationCoordinates(45.5, -122.4194)

Expand All @@ -111,7 +111,7 @@ suite(

await context1.navigate(Pages.blankPage, 'complete')
const origin1 = (await script.callFunctionInBrowsingContext(context1.id, GET_ORIGIN, true, [])).result.value
await permission.setPermission({name: 'geolocation'}, PermissionState.GRANTED, origin1, userContext1)
await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, origin1, userContext1)

const result1 = await script.evaluateFunctionInBrowsingContext(context1.id, GET_CURRENT_GEOLOCATION, true)
const geolocation1 = result1.result.value
Expand All @@ -125,7 +125,7 @@ suite(

await context2.navigate(Pages.blankPage, 'complete')
const origin2 = (await script.callFunctionInBrowsingContext(context1.id, GET_ORIGIN, true, [])).result.value
await permission.setPermission({name: 'geolocation'}, PermissionState.GRANTED, origin2, userContext2)
await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, origin2, userContext2)

const result2 = await script.evaluateFunctionInBrowsingContext(context2.id, GET_CURRENT_GEOLOCATION, true)
const geolocation2 = result2.result.value
Expand All @@ -136,32 +136,28 @@ suite(
assert.strictEqual(currentLongitude2, -122.4194)
})

it('can override geolocation with error', async function () {
ignore(env.browsers(Browser.FIREFOX)).it('can override geolocation with error', async function () {
const windowHandle = await driver.getWindowHandle()
const context = await BrowsingContext(driver, { browsingContextId: windowHandle })
await context.navigate(Pages.blankPage, 'complete')

const origin = await script.callFunctionInBrowsingContext(context.id, GET_ORIGIN, true, [])
const originValue = origin.result.value

await permission.setPermission({name: 'geolocation'}, PermissionState.GRANTED, originValue)
await permission.setPermission({ name: 'geolocation' }, PermissionState.GRANTED, originValue)

await emulation.setGeolocationOverride(GeolocationPositionError, windowHandle)

const result = await script.evaluateFunctionInBrowsingContext(
context.id,
GET_CURRENT_GEOLOCATION,
true
)
const result = await script.evaluateFunctionInBrowsingContext(context.id, GET_CURRENT_GEOLOCATION, true)

const geolocation = result.result.value

assert.ok(
Object.hasOwn(geolocation, 'error'),
`Expected geolocation to have 'error' key, but got: ${JSON.stringify(geolocation)}`
`Expected geolocation to have 'error' key, but got: ${JSON.stringify(geolocation)}`,
)
})
})
},
{browsers: [Browser.CHROME]},
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
)