Path: blob/trunk/javascript/selenium-webdriver/test/lib/capabilities_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 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 io = require('selenium-webdriver/io')2930const Pages = test.Pages3132describe('Capabilities', function () {33it('can set and unset a capability', function () {34let caps = new Capabilities()35assert.strictEqual(undefined, caps.get('foo'))36assert.ok(!caps.has('foo'))3738caps.set('foo', 'bar')39assert.strictEqual('bar', caps.get('foo'))40assert.ok(caps.has('foo'))4142caps.set('foo', null)43assert.strictEqual(null, caps.get('foo'))44assert.ok(caps.has('foo'))45})4647it('requires string capability keys', function () {48let caps = new Capabilities()49assert.throws(() => caps.set({}, 'hi'))50})5152it('can merge capabilities', function () {53const caps1 = new Capabilities().set('foo', 'bar').set('color', 'red')5455const caps2 = new Capabilities().set('color', 'green')5657assert.strictEqual('bar', caps1.get('foo'))58assert.strictEqual('red', caps1.get('color'))59assert.strictEqual('green', caps2.get('color'))60assert.strictEqual(undefined, caps2.get('foo'))6162caps2.merge(caps1)63assert.strictEqual('bar', caps1.get('foo'))64assert.strictEqual('red', caps1.get('color'))65assert.strictEqual('red', caps2.get('color'))66assert.strictEqual('bar', caps2.get('foo'))6768const caps3 = new Map().set('color', 'blue')6970caps2.merge(caps3)71assert.strictEqual('blue', caps2.get('color'))72assert.strictEqual('bar', caps2.get('foo'))7374const caps4 = { foo: 'baz' }7576const caps5 = caps2.merge(caps4)7778assert.strictEqual('blue', caps2.get('color'))79assert.strictEqual('baz', caps2.get('foo'))80assert.strictEqual('blue', caps5.get('color'))81assert.strictEqual('baz', caps5.get('foo'))82assert.strictEqual(true, caps5 instanceof Capabilities)83assert.strictEqual(caps2, caps5)84})8586it('can be initialized from a hash object', function () {87let caps = new Capabilities({ one: 123, abc: 'def' })88assert.strictEqual(123, caps.get('one'))89assert.strictEqual('def', caps.get('abc'))90})9192it('can be initialized from a map', function () {93let m = new Map([94['one', 123],95['abc', 'def'],96])9798let caps = new Capabilities(m)99assert.strictEqual(123, caps.get('one'))100assert.strictEqual('def', caps.get('abc'))101})102103describe('serialize', function () {104it('works for simple capabilities', function () {105let m = new Map([106['one', 123],107['abc', 'def'],108])109let caps = new Capabilities(m)110assert.deepStrictEqual({ one: 123, abc: 'def' }, caps[Symbols.serialize]())111})112113it('does not omit capabilities set to a false-like value', function () {114let caps = new Capabilities()115caps.set('bool', false)116caps.set('number', 0)117caps.set('string', '')118119assert.deepStrictEqual({ bool: false, number: 0, string: '' }, caps[Symbols.serialize]())120})121122it('omits capabilities with a null value', function () {123let caps = new Capabilities()124caps.set('foo', null)125caps.set('bar', 123)126assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())127})128129it('omits capabilities with an undefined value', function () {130let caps = new Capabilities()131caps.set('foo', undefined)132caps.set('bar', 123)133assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())134})135})136})137138test.suite(function (env) {139test140.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))141.it(142'should fail to upload files to a non interactable input when StrictFileInteractability is on',143async function () {144const options = env.builder().getChromeOptions() || new chrome.Options()145options.setStrictFileInteractability(true)146const driver = env.builder().setChromeOptions(options).build()147148const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'149const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'150151let fp = await io.tmpFile().then(function (fp) {152fs.writeFileSync(fp, FILE_HTML)153return fp154})155156driver.setFileDetector(new remote.FileDetector())157await driver.get(Pages.uploadInvisibleTestPage)158const input = await driver.findElement(By.id('upload'))159try {160await input.sendKeys(fp)161assert(false, 'element was interactable')162} catch (e) {163assert(e.message.includes('element not interactable'))164}165166if (driver) {167return driver.quit()168}169},170)171172test173.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))174.it('Should upload files to a non interactable file input', async function () {175const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'176const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'177178let fp = await io.tmpFile().then(function (fp) {179fs.writeFileSync(fp, FILE_HTML)180return fp181})182183const options = env.builder().getChromeOptions() || new chrome.Options()184options.setStrictFileInteractability(false)185const driver = env.builder().setChromeOptions(options).build()186187driver.setFileDetector(new remote.FileDetector())188await driver.get(Pages.uploadInvisibleTestPage)189190const input1 = await driver.findElement(By.id('upload'))191input1.sendKeys(fp)192await driver.findElement(By.id('go')).click()193194// Uploading files across a network may take a while, even if they're really small195let label = await driver.findElement(By.id('upload_label'))196await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')197198const frame = await driver.findElement(By.id('upload_target'))199await driver.switchTo().frame(frame)200assert.strictEqual(await driver.findElement(By.css('body')).getText(), fp.split('/').pop())201202if (driver) {203return driver.quit()204}205})206})207208209