Path: blob/trunk/javascript/selenium-webdriver/test/lib/capabilities_test.js
4500 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 Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities20const Symbols = require('selenium-webdriver/lib/symbols')21const test = require('../../lib/test')22const chrome = require('selenium-webdriver/chrome')23const { Browser, By, until } = require('selenium-webdriver')24const remote = require('selenium-webdriver/remote')2526const assert = require('node:assert')27const fs = require('node:fs')28const path = require('node:path')29const io = require('selenium-webdriver/io')3031const Pages = test.Pages3233describe('Capabilities', function () {34it('can set and unset a capability', function () {35let caps = new Capabilities()36assert.strictEqual(undefined, caps.get('foo'))37assert.ok(!caps.has('foo'))3839caps.set('foo', 'bar')40assert.strictEqual('bar', caps.get('foo'))41assert.ok(caps.has('foo'))4243caps.set('foo', null)44assert.strictEqual(null, caps.get('foo'))45assert.ok(caps.has('foo'))46})4748it('requires string capability keys', function () {49let caps = new Capabilities()50assert.throws(() => caps.set({}, 'hi'))51})5253it('can merge capabilities', function () {54const caps1 = new Capabilities().set('foo', 'bar').set('color', 'red')5556const caps2 = new Capabilities().set('color', 'green')5758assert.strictEqual('bar', caps1.get('foo'))59assert.strictEqual('red', caps1.get('color'))60assert.strictEqual('green', caps2.get('color'))61assert.strictEqual(undefined, caps2.get('foo'))6263caps2.merge(caps1)64assert.strictEqual('bar', caps1.get('foo'))65assert.strictEqual('red', caps1.get('color'))66assert.strictEqual('red', caps2.get('color'))67assert.strictEqual('bar', caps2.get('foo'))6869const caps3 = new Map().set('color', 'blue')7071caps2.merge(caps3)72assert.strictEqual('blue', caps2.get('color'))73assert.strictEqual('bar', caps2.get('foo'))7475const caps4 = { foo: 'baz' }7677const caps5 = caps2.merge(caps4)7879assert.strictEqual('blue', caps2.get('color'))80assert.strictEqual('baz', caps2.get('foo'))81assert.strictEqual('blue', caps5.get('color'))82assert.strictEqual('baz', caps5.get('foo'))83assert.strictEqual(true, caps5 instanceof Capabilities)84assert.strictEqual(caps2, caps5)85})8687it('can be initialized from a hash object', function () {88let caps = new Capabilities({ one: 123, abc: 'def' })89assert.strictEqual(123, caps.get('one'))90assert.strictEqual('def', caps.get('abc'))91})9293it('can be initialized from a map', function () {94let m = new Map([95['one', 123],96['abc', 'def'],97])9899let caps = new Capabilities(m)100assert.strictEqual(123, caps.get('one'))101assert.strictEqual('def', caps.get('abc'))102})103104describe('serialize', function () {105it('works for simple capabilities', function () {106let m = new Map([107['one', 123],108['abc', 'def'],109])110let caps = new Capabilities(m)111assert.deepStrictEqual({ one: 123, abc: 'def' }, caps[Symbols.serialize]())112})113114it('does not omit capabilities set to a false-like value', function () {115let caps = new Capabilities()116caps.set('bool', false)117caps.set('number', 0)118caps.set('string', '')119120assert.deepStrictEqual({ bool: false, number: 0, string: '' }, caps[Symbols.serialize]())121})122123it('omits capabilities with a null value', function () {124let caps = new Capabilities()125caps.set('foo', null)126caps.set('bar', 123)127assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())128})129130it('omits capabilities with an undefined value', function () {131let caps = new Capabilities()132caps.set('foo', undefined)133caps.set('bar', 123)134assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())135})136})137})138139test.suite(function (env) {140test141.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))142.it(143'should fail to upload files to a non interactable input when StrictFileInteractability is on',144async function () {145const options = env.builder().getChromeOptions() || new chrome.Options()146options.setStrictFileInteractability(true)147const driver = env.builder().setChromeOptions(options).build()148149const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'150const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'151152let fp = await io.tmpFile().then(function (fp) {153fs.writeFileSync(fp, FILE_HTML)154return fp155})156157driver.setFileDetector(new remote.FileDetector())158await driver.get(Pages.uploadInvisibleTestPage)159const input = await driver.findElement(By.id('upload'))160try {161await input.sendKeys(fp)162assert(false, 'element was interactable')163} catch (e) {164assert(e.message.includes('element not interactable'))165}166167if (driver) {168return driver.quit()169}170},171)172173test174.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))175.it('Should upload files to a non interactable file input', async function () {176const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'177const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'178179let fp = await io.tmpFile().then(function (fp) {180fs.writeFileSync(fp, FILE_HTML)181return fp182})183184const options = env.builder().getChromeOptions() || new chrome.Options()185options.setStrictFileInteractability(false)186const driver = env.builder().setChromeOptions(options).build()187188driver.setFileDetector(new remote.FileDetector())189await driver.get(Pages.uploadInvisibleTestPage)190191const input1 = await driver.findElement(By.id('upload'))192input1.sendKeys(fp)193await driver.findElement(By.id('go')).click()194195// Uploading files across a network may take a while, even if they're really small196let label = await driver.findElement(By.id('upload_label'))197await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')198199const frame = await driver.findElement(By.id('upload_target'))200await driver.switchTo().frame(frame)201assert.strictEqual(await driver.findElement(By.css('body')).getText(), path.basename(fp))202203if (driver) {204return driver.quit()205}206})207})208209210