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
2885 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 io = require('selenium-webdriver/io')
30
31
const Pages = test.Pages
32
33
describe('Capabilities', function () {
34
it('can set and unset a capability', function () {
35
let caps = new Capabilities()
36
assert.strictEqual(undefined, caps.get('foo'))
37
assert.ok(!caps.has('foo'))
38
39
caps.set('foo', 'bar')
40
assert.strictEqual('bar', caps.get('foo'))
41
assert.ok(caps.has('foo'))
42
43
caps.set('foo', null)
44
assert.strictEqual(null, caps.get('foo'))
45
assert.ok(caps.has('foo'))
46
})
47
48
it('requires string capability keys', function () {
49
let caps = new Capabilities()
50
assert.throws(() => caps.set({}, 'hi'))
51
})
52
53
it('can merge capabilities', function () {
54
const caps1 = new Capabilities().set('foo', 'bar').set('color', 'red')
55
56
const caps2 = new Capabilities().set('color', 'green')
57
58
assert.strictEqual('bar', caps1.get('foo'))
59
assert.strictEqual('red', caps1.get('color'))
60
assert.strictEqual('green', caps2.get('color'))
61
assert.strictEqual(undefined, caps2.get('foo'))
62
63
caps2.merge(caps1)
64
assert.strictEqual('bar', caps1.get('foo'))
65
assert.strictEqual('red', caps1.get('color'))
66
assert.strictEqual('red', caps2.get('color'))
67
assert.strictEqual('bar', caps2.get('foo'))
68
69
const caps3 = new Map().set('color', 'blue')
70
71
caps2.merge(caps3)
72
assert.strictEqual('blue', caps2.get('color'))
73
assert.strictEqual('bar', caps2.get('foo'))
74
75
const caps4 = { foo: 'baz' }
76
77
const caps5 = caps2.merge(caps4)
78
79
assert.strictEqual('blue', caps2.get('color'))
80
assert.strictEqual('baz', caps2.get('foo'))
81
assert.strictEqual('blue', caps5.get('color'))
82
assert.strictEqual('baz', caps5.get('foo'))
83
assert.strictEqual(true, caps5 instanceof Capabilities)
84
assert.strictEqual(caps2, caps5)
85
})
86
87
it('can be initialized from a hash object', function () {
88
let caps = new Capabilities({ one: 123, abc: 'def' })
89
assert.strictEqual(123, caps.get('one'))
90
assert.strictEqual('def', caps.get('abc'))
91
})
92
93
it('can be initialized from a map', function () {
94
let m = new Map([
95
['one', 123],
96
['abc', 'def'],
97
])
98
99
let caps = new Capabilities(m)
100
assert.strictEqual(123, caps.get('one'))
101
assert.strictEqual('def', caps.get('abc'))
102
})
103
104
describe('serialize', function () {
105
it('works for simple capabilities', function () {
106
let m = new Map([
107
['one', 123],
108
['abc', 'def'],
109
])
110
let caps = new Capabilities(m)
111
assert.deepStrictEqual({ one: 123, abc: 'def' }, caps[Symbols.serialize]())
112
})
113
114
it('does not omit capabilities set to a false-like value', function () {
115
let caps = new Capabilities()
116
caps.set('bool', false)
117
caps.set('number', 0)
118
caps.set('string', '')
119
120
assert.deepStrictEqual({ bool: false, number: 0, string: '' }, caps[Symbols.serialize]())
121
})
122
123
it('omits capabilities with a null value', function () {
124
let caps = new Capabilities()
125
caps.set('foo', null)
126
caps.set('bar', 123)
127
assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())
128
})
129
130
it('omits capabilities with an undefined value', function () {
131
let caps = new Capabilities()
132
caps.set('foo', undefined)
133
caps.set('bar', 123)
134
assert.deepStrictEqual({ bar: 123 }, caps[Symbols.serialize]())
135
})
136
})
137
})
138
139
test.suite(function (env) {
140
test
141
.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))
142
.it(
143
'should fail to upload files to a non interactable input when StrictFileInteractability is on',
144
async function () {
145
const options = env.builder().getChromeOptions() || new chrome.Options()
146
options.setStrictFileInteractability(true)
147
const driver = env.builder().setChromeOptions(options).build()
148
149
const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'
150
const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'
151
152
let fp = await io.tmpFile().then(function (fp) {
153
fs.writeFileSync(fp, FILE_HTML)
154
return fp
155
})
156
157
driver.setFileDetector(new remote.FileDetector())
158
await driver.get(Pages.uploadInvisibleTestPage)
159
const input = await driver.findElement(By.id('upload'))
160
try {
161
await input.sendKeys(fp)
162
assert(false, 'element was interactable')
163
} catch (e) {
164
assert(e.message.includes('element not interactable'))
165
}
166
167
if (driver) {
168
return driver.quit()
169
}
170
},
171
)
172
173
test
174
.ignore(env.browsers(Browser.SAFARI, Browser.FIREFOX))
175
.it('Should upload files to a non interactable file input', async function () {
176
const LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'
177
const FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'
178
179
let fp = await io.tmpFile().then(function (fp) {
180
fs.writeFileSync(fp, FILE_HTML)
181
return fp
182
})
183
184
const options = env.builder().getChromeOptions() || new chrome.Options()
185
options.setStrictFileInteractability(false)
186
const driver = env.builder().setChromeOptions(options).build()
187
188
driver.setFileDetector(new remote.FileDetector())
189
await driver.get(Pages.uploadInvisibleTestPage)
190
191
const input1 = await driver.findElement(By.id('upload'))
192
input1.sendKeys(fp)
193
await driver.findElement(By.id('go')).click()
194
195
// Uploading files across a network may take a while, even if they're really small
196
let label = await driver.findElement(By.id('upload_label'))
197
await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')
198
199
const frame = await driver.findElement(By.id('upload_target'))
200
await driver.switchTo().frame(frame)
201
assert.strictEqual(await driver.findElement(By.css('body')).getText(), fp.split('/').pop())
202
203
if (driver) {
204
return driver.quit()
205
}
206
})
207
})
208
209