Path: blob/trunk/javascript/selenium-webdriver/test/chrome/options_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 fs = require('node:fs')21const chrome = require('selenium-webdriver/chrome')22const symbols = require('selenium-webdriver/lib/symbols')23const test = require('../../lib/test')24const { locate } = require('../../lib/test/resources')2526const WEBEXTENSION_CRX = locate('common/extensions/webextensions-selenium-example.crx')2728describe('chrome.Options', function () {29describe('addArguments', function () {30it('takes var_args', function () {31let options = new chrome.Options()32assert.deepStrictEqual(options[symbols.serialize](), {33browserName: 'chrome',34'goog:chromeOptions': {},35})3637options.addArguments('a', 'b')38assert.deepStrictEqual(options[symbols.serialize](), {39browserName: 'chrome',40'goog:chromeOptions': {41args: ['a', 'b'],42},43})44})4546it('flattens input arrays', function () {47let options = new chrome.Options()48assert.deepStrictEqual(options[symbols.serialize](), {49browserName: 'chrome',50'goog:chromeOptions': {},51})5253options.addArguments(['a', 'b'], 'c', [1, 2], 3)54assert.deepStrictEqual(options[symbols.serialize](), {55browserName: 'chrome',56'goog:chromeOptions': {57args: ['a', 'b', 'c', 1, 2, 3],58},59})60})61})6263describe('addExtensions', function () {64it('takes var_args', function () {65let options = new chrome.Options()66assert.strictEqual(options.options_.extensions, undefined)6768options.addExtensions('a', 'b')69assert.deepStrictEqual(options.options_.extensions.extensions, ['a', 'b'])70})7172it('flattens input arrays', function () {73let options = new chrome.Options()74assert.strictEqual(options.options_.extensions, undefined)7576options.addExtensions(['a', 'b'], 'c', [1, 2], 3)77assert.deepStrictEqual(options.options_.extensions.extensions, ['a', 'b', 'c', 1, 2, 3])78})79})8081describe('serialize', function () {82it('base64 encodes extensions', async function () {83let expected = fs.readFileSync(WEBEXTENSION_CRX, 'base64')84let wire = new chrome.Options().addExtensions(WEBEXTENSION_CRX)[symbols.serialize]()8586let extensions = wire['goog:chromeOptions'].extensions[symbols.serialize]()87assert.strictEqual(extensions.length, 1)88assert.strictEqual(await extensions[0], expected)89})90})9192describe('windowTypes', function () {93it('takes var_args', function () {94let options = new chrome.Options()95assert.strictEqual(options.options_.windowTypes, undefined)9697options.windowTypes('a', 'b')98assert.deepStrictEqual(options.options_.windowTypes, ['a', 'b'])99})100101it('flattens input arrays', function () {102let options = new chrome.Options()103assert.strictEqual(options.options_.windowTypes, undefined)104105options.windowTypes(['a', 'b'], 'c', [1, 2], 3)106assert.deepStrictEqual(options.options_.windowTypes, ['a', 'b', 'c', 1, 2, 3])107})108})109})110111test.suite(112function (env) {113var driver114115beforeEach(function () {116driver = null117})118119afterEach(function () {120return driver && driver.quit()121})122123describe('Chrome options', function () {124it('can start Chrome with custom args', async function () {125const options = env.builder().getChromeOptions() || new chrome.Options()126options.addArguments('user-agent=foo;bar')127128driver = await env.builder().setChromeOptions(options).build()129130await driver.get(test.Pages.ajaxyPage)131132const userAgent = await driver.executeScript('return window.navigator.userAgent')133assert.strictEqual(userAgent, 'foo;bar')134})135136it('can start chromium with network conditions set', async function () {137driver = await env.builder().build()138await driver.get(test.Pages.ajaxyPage)139await driver.setNetworkConditions({140offline: true,141latency: 0,142download_throughput: 0,143upload_throughput: 0,144})145assert.deepStrictEqual(await driver.getNetworkConditions(), {146download_throughput: 0,147latency: 0,148offline: true,149upload_throughput: 0,150})151await driver.deleteNetworkConditions()152})153154it('can install an extension from path', async function () {155let options = env.builder().getChromeOptions() || new chrome.Options()156options.addExtensions(WEBEXTENSION_CRX)157158driver = await env.builder().forBrowser('chrome').setChromeOptions(options).build()159160await driver.get(test.Pages.echoPage)161await verifyWebExtensionWasInstalled()162})163164it('can install an extension from Buffer', async function () {165let options = env.builder().getChromeOptions() || new chrome.Options()166options.addExtensions(fs.readFileSync(WEBEXTENSION_CRX))167168driver = await env.builder().forBrowser('chrome').setChromeOptions(options).build()169170await driver.get(test.Pages.echoPage)171await verifyWebExtensionWasInstalled()172})173174async function verifyWebExtensionWasInstalled() {175let footer = await driver.findElement({176id: 'webextensions-selenium-example',177})178let text = await footer.getText()179assert.strictEqual(text, 'Content injected by webextensions-selenium-example')180}181})182},183{ browsers: ['chrome'] },184)185186187