Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/emulation_test.js
11822 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 { suite, ignore, Pages } = require('../../../lib/test')21const { Browser } = require('selenium-webdriver')2223const { Emulation } = require('selenium-webdriver/bidi/generated/emulation')24const { Script } = require('selenium-webdriver/bidi/generated/script')25const { BrowsingContext } = require('selenium-webdriver/bidi/generated/browsing_context')2627suite(28function (env) {29let driver30let emulation31let script32let browsingContext3334beforeEach(async function () {35driver = await env.builder().build()36emulation = await Emulation.create(driver)37script = await Script.create(driver)38browsingContext = await BrowsingContext.create(driver)39await driver.get(Pages.emptyPage)40})4142afterEach(function () {43return driver.quit()44})4546describe('setUserAgentOverride', function () {47it('can override the user agent', async function () {48const contextId = await driver.getWindowHandle()49const customUA = 'MyCustomUA/1.0'5051await emulation.setUserAgentOverride({52userAgent: customUA,53contexts: [contextId],54})5556// Navigate after setting override so the new UA is used for the request57await driver.get(Pages.emptyPage)58const ua = await driver.executeScript('return navigator.userAgent')59assert.strictEqual(ua, customUA)60})6162it('can clear the user agent override', async function () {63const contextId = await driver.getWindowHandle()6465await emulation.setUserAgentOverride({ userAgent: 'test-ua', contexts: [contextId] })66await driver.get(Pages.emptyPage)6768// Clear by passing null69await emulation.setUserAgentOverride({ userAgent: null, contexts: [contextId] })70await driver.get(Pages.emptyPage)7172const ua = await driver.executeScript('return navigator.userAgent')73// After clearing, user agent should not be 'test-ua'74assert.notStrictEqual(ua, 'test-ua')75})76})7778describe('setTimezoneOverride', function () {79it('can override the timezone', async function () {80// Open a fresh tab, set the override for it, navigate, then evaluate.81// Using a new context ensures the override is applied before any page82// load so the new realm picks it up from the start.83const { context: newContextId } = await browsingContext.create({ type: 'tab' })8485await emulation.setTimezoneOverride({86timezone: 'America/New_York',87contexts: [newContextId],88})8990await browsingContext.navigate({91context: newContextId,92url: Pages.emptyPage,93wait: 'complete',94})9596const evalResult = await script.evaluate({97expression: 'Intl.DateTimeFormat().resolvedOptions().timeZone',98target: { context: newContextId },99awaitPromise: false,100})101102await browsingContext.close({ context: newContextId })103assert.strictEqual(evalResult.result.value, 'America/New_York')104})105106it('can set UTC timezone', async function () {107const { context: newContextId } = await browsingContext.create({ type: 'tab' })108109await emulation.setTimezoneOverride({110timezone: 'UTC',111contexts: [newContextId],112})113114await browsingContext.navigate({115context: newContextId,116url: Pages.emptyPage,117wait: 'complete',118})119120const evalResult = await script.evaluate({121expression: 'Intl.DateTimeFormat().resolvedOptions().timeZone',122target: { context: newContextId },123awaitPromise: false,124})125126await browsingContext.close({ context: newContextId })127assert.strictEqual(evalResult.result.value, 'UTC')128})129})130131describe('setLocaleOverride', function () {132it('can override the locale', async function () {133// Same fresh-tab approach as setTimezoneOverride — set override before134// any page load so the new realm picks it up from the start.135const { context: newContextId } = await browsingContext.create({ type: 'tab' })136137await emulation.setLocaleOverride({138locale: 'fr-FR',139contexts: [newContextId],140})141142await browsingContext.navigate({143context: newContextId,144url: Pages.emptyPage,145wait: 'complete',146})147148const evalResult = await script.evaluate({149expression: 'Intl.DateTimeFormat().resolvedOptions().locale',150target: { context: newContextId },151awaitPromise: false,152})153154await browsingContext.close({ context: newContextId })155assert.ok(156evalResult.result.value.startsWith('fr'),157`locale should start with 'fr', got '${evalResult.result.value}'`,158)159})160161it('can clear the locale override', async function () {162const contextId = await driver.getWindowHandle()163164await emulation.setLocaleOverride({ locale: 'ja-JP', contexts: [contextId] })165// Clear by passing null166await emulation.setLocaleOverride({ locale: null, contexts: [contextId] })167})168})169170describe('setNetworkConditions', function () {171it('can set network conditions to offline', async function () {172const contextId = await driver.getWindowHandle()173174await emulation.setNetworkConditions({175networkConditions: { type: 'offline' },176contexts: [contextId],177})178179// Restore180await emulation.setNetworkConditions({181networkConditions: null,182contexts: [contextId],183})184})185186it('can clear network conditions', async function () {187const contextId = await driver.getWindowHandle()188189await emulation.setNetworkConditions({190networkConditions: { type: 'offline' },191contexts: [contextId],192})193await emulation.setNetworkConditions({194networkConditions: null,195contexts: [contextId],196})197})198})199200describe('setGeolocationOverride', function () {201it('can override geolocation coordinates', async function () {202const contextId = await driver.getWindowHandle()203204await emulation.setGeolocationOverride({205coordinates: {206latitude: 37.7749,207longitude: -122.4194,208accuracy: 10,209},210contexts: [contextId],211})212})213214it('can clear geolocation override', async function () {215const contextId = await driver.getWindowHandle()216217await emulation.setGeolocationOverride({218coordinates: { latitude: 0, longitude: 0, accuracy: 1 },219contexts: [contextId],220})221await emulation.setGeolocationOverride({222coordinates: null,223contexts: [contextId],224})225})226})227228describe('setScreenOrientationOverride', function () {229it('can override screen orientation to portrait', async function () {230const contextId = await driver.getWindowHandle()231232await emulation.setScreenOrientationOverride({233screenOrientation: { natural: 'portrait', type: 'portrait-primary' },234contexts: [contextId],235})236})237238it('can override screen orientation to landscape', async function () {239const contextId = await driver.getWindowHandle()240241await emulation.setScreenOrientationOverride({242screenOrientation: { natural: 'landscape', type: 'landscape-primary' },243contexts: [contextId],244})245})246247it('can clear screen orientation override', async function () {248const contextId = await driver.getWindowHandle()249250await emulation.setScreenOrientationOverride({251screenOrientation: { natural: 'portrait', type: 'portrait-primary' },252contexts: [contextId],253})254await emulation.setScreenOrientationOverride({255screenOrientation: null,256contexts: [contextId],257})258})259})260261ignore(env.browsers(Browser.FIREFOX)).describe('setScriptingEnabled', function () {262it('can disable scripting for a context', async function () {263const contextId = await driver.getWindowHandle()264265await emulation.setScriptingEnabled({266enabled: false,267contexts: [contextId],268})269270// Restore by passing null271await emulation.setScriptingEnabled({272enabled: null,273contexts: [contextId],274})275})276})277278ignore(env.browsers(Browser.FIREFOX)).describe('setTouchOverride', function () {279it('can enable touch emulation', async function () {280const contextId = await driver.getWindowHandle()281282await emulation.setTouchOverride({283maxTouchPoints: 5,284contexts: [contextId],285})286287// Restore by passing null288await emulation.setTouchOverride({289maxTouchPoints: null,290contexts: [contextId],291})292})293})294},295{ browsers: [Browser.CHROME, Browser.FIREFOX] },296)297298299