Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/capabilities_test.js
4500 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities
21
const Symbols = require('selenium-webdriver/lib/symbols')
22
const test = require('../../lib/test')
23
const chrome = require('selenium-webdriver/chrome')
24
const { Browser, By, until } = require('selenium-webdriver')
25
const remote = require('selenium-webdriver/remote')
26
27
const assert = require('node:assert')
28
const fs = require('node:fs')
29
const path = require('node:path')
30
const io = require('selenium-webdriver/io')
31
32
const Pages = test.Pages
33
34
describe('Capabilities', function () {
35
it('can set and unset a capability', function () {
36
let caps = new Capabilities()
37
assert.strictEqual(undefined, caps.get('foo'))
38
assert.ok(!caps.has('foo'))
39
40
caps.set('foo', 'bar')
41
assert.strictEqual('bar', caps.get('foo'))
42
assert.ok(caps.has('foo'))
43
44
caps.set('foo', null)
45
assert.strictEqual(null, caps.get('foo'))
46
assert.ok(caps.has('foo'))
47
})
48
49
it('requires string capability keys', function () {
50
let caps = new Capabilities()
51
assert.throws(() => caps.set({}, 'hi'))
52
})
53
54
it('can merge capabilities', function () {
55
const caps1 = new Capabilities().set('foo', 'bar').set('color', 'red')
56
57
const caps2 = new Capabilities().set('color', 'green')
58
59
assert.strictEqual('bar', caps1.get('foo'))
60
assert.strictEqual('red', caps1.get('color'))
61
assert.strictEqual('green', caps2.get('color'))
62
assert.strictEqual(undefined, caps2.get('foo'))
63
64
caps2.merge(caps1)
65
assert.strictEqual('bar', caps1.get('foo'))
66
assert.strictEqual('red', caps1.get('color'))
67
assert.strictEqual('red', caps2.get('color'))
68
assert.strictEqual('bar', caps2.get('foo'))
69
70
const caps3 = new Map().set('color', 'blue')
71
72
caps2.merge(caps3)
73
assert.strictEqual('blue', caps2.get('color'))
74
assert.strictEqual('bar', caps2.get('foo'))
75
76
const caps4 = { foo: 'baz' }
77
78
const caps5 = caps2.merge(caps4)
79
80
assert.strictEqual('blue', caps2.get('color'))
81
assert.strictEqual('baz', caps2.get('foo'))
82
assert.strictEqual('blue', caps5.get('color'))
83
assert.strictEqual('baz', caps5.get('foo'))
84
assert.strictEqual(true, caps5 instanceof Capabilities)
85
assert.strictEqual(caps2, caps5)
86
})
87
88
it('can be initialized from a hash object', function () {
89
let caps = new Capabilities({ one: 123, abc: 'def' })
90
assert.strictEqual(123, caps.get('one'))
91
assert.strictEqual('def', caps.get('abc'))
92
})
93
94
it('can be initialized from a map', function () {
95
let m = new Map([
96
['one', 123],
97
['abc', 'def'],
98
])
99
100
let caps = new Capabilities(m)
101
assert.strictEqual(123, caps.get('one'))
102
assert.strictEqual('def', caps.get('abc'))
103
})
104
105
describe('serialize', function () {
106
it('works for simple capabilities', function () {
107
let m = new Map([
108
['one', 123],
109
['abc', 'def'],
110
])
111
let caps = new Capabilities(m)
112
assert.deepStrictEqual({ one: 123, abc: 'def' }, caps[Symbols.serialize]())
113
})
114
115
it('does not omit capabilities set to a false-like value', function () {
116
let caps = new Capabilities()
117
caps.set('bool', false)
118
caps.set('number', 0)
119
caps.set('string', '')
120
121
assert.deepStrictEqual({ bool: false, number: 0, string: '' }, caps[Symbols.serialize]())
122
})
123
124
it('omits capabilities with a null value', function () {
125
let caps = new Capabilities()
126
caps.set('foo', null)
127
caps.set('bar', 123)
128
assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())
129
})
130
131
it('omits capabilities with an undefined value', function () {
132
let caps = new Capabilities()
133
caps.set('foo', undefined)
134
caps.set('bar', 123)
135
assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())
136
})
137
})
138
})
139
140
test.suite(function (env) {
141
test
142
.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))
143
.it(
144
'should fail to upload files to a non interactable input when StrictFileInteractability is on',
145
async function () {
146
const options = env.builder().getChromeOptions() || new chrome.Options()
147
options.setStrictFileInteractability(true)
148
const driver = env.builder().setChromeOptions(options).build()
149
150
const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'
151
const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'
152
153
let fp = await io.tmpFile().then(function (fp) {
154
fs.writeFileSync(fp, FILE_HTML)
155
return fp
156
})
157
158
driver.setFileDetector(new remote.FileDetector())
159
await driver.get(Pages.uploadInvisibleTestPage)
160
const input = await driver.findElement(By.id('upload'))
161
try {
162
await input.sendKeys(fp)
163
assert(false, 'element was interactable')
164
} catch (e) {
165
assert(e.message.includes('element not interactable'))
166
}
167
168
if (driver) {
169
return driver.quit()
170
}
171
},
172
)
173
174
test
175
.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))
176
.it('Should upload files to a non interactable file input', async function () {
177
const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'
178
const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'
179
180
let fp = await io.tmpFile().then(function (fp) {
181
fs.writeFileSync(fp, FILE_HTML)
182
return fp
183
})
184
185
const options = env.builder().getChromeOptions() || new chrome.Options()
186
options.setStrictFileInteractability(false)
187
const driver = env.builder().setChromeOptions(options).build()
188
189
driver.setFileDetector(new remote.FileDetector())
190
await driver.get(Pages.uploadInvisibleTestPage)
191
192
const input1 = await driver.findElement(By.id('upload'))
193
input1.sendKeys(fp)
194
await driver.findElement(By.id('go')).click()
195
196
// Uploading files across a network may take a while, even if they're really small
197
let label = await driver.findElement(By.id('upload_label'))
198
await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')
199
200
const frame = await driver.findElement(By.id('upload_target'))
201
await driver.switchTo().frame(frame)
202
assert.strictEqual(await driver.findElement(By.css('body')).getText(), path.basename(fp))
203
204
if (driver) {
205
return driver.quit()
206
}
207
})
208
})
209
210