Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/select_test.js
2884 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 assert = require('node:assert')
21
const { Select, By } = require('selenium-webdriver')
22
const { Pages, suite } = require('../lib/test')
23
const { escapeQuotes } = require('selenium-webdriver/lib/select')
24
25
let singleSelectValues1 = {
26
name: 'selectomatic',
27
values: ['One', 'Two', 'Four', 'Still learning how to count, apparently'],
28
}
29
30
let disabledSingleSelect = {
31
name: 'single_disabled',
32
values: ['Enabled', 'Disabled'],
33
}
34
let disabledMultiSelect = {
35
name: 'multi_disabled',
36
values: ['Enabled', 'Disabled'],
37
}
38
39
let multiSelectValues1 = {
40
name: 'multi',
41
values: ['Eggs', 'Ham', 'Sausages', 'Onion gravy'],
42
}
43
let multiSelectValues2 = {
44
name: 'select_empty_multiple',
45
values: ['select_1', 'select_2', 'select_3', 'select_4'],
46
}
47
48
suite(
49
function (env) {
50
let driver
51
52
before(async function () {
53
driver = await env.builder().build()
54
})
55
after(async () => await driver.quit())
56
57
describe('Select by tests', function () {
58
it('Should be able to select by value', async function () {
59
await driver.get(Pages.formPage)
60
61
let selector = new Select(driver.findElement(By.name(singleSelectValues1['name'])))
62
for (let x in singleSelectValues1['values']) {
63
await selector.selectByValue(singleSelectValues1['values'][x].toLowerCase())
64
let ele = await selector.getFirstSelectedOption()
65
assert.deepEqual(await ele.getText(), singleSelectValues1['values'][x])
66
}
67
})
68
69
it('Should be able to select by index', async function () {
70
await driver.get(Pages.formPage)
71
72
let selector = new Select(driver.findElement(By.name(singleSelectValues1['name'])))
73
for (let x in singleSelectValues1['values']) {
74
await selector.selectByIndex(x)
75
let ele = await selector.getFirstSelectedOption()
76
assert.deepEqual(await ele.getText(), singleSelectValues1['values'][x])
77
}
78
})
79
80
it('Should be able to select by visible text', async function () {
81
await driver.get(Pages.formPage)
82
83
let selector = new Select(driver.findElement(By.name(singleSelectValues1['name'])))
84
for (let x in singleSelectValues1['values']) {
85
await selector.selectByVisibleText(singleSelectValues1['values'][x])
86
let ele = await selector.getFirstSelectedOption()
87
assert.deepEqual(await ele.getText(), singleSelectValues1['values'][x])
88
}
89
})
90
91
it('Should be able to select by visible text with spaces', async function () {
92
await driver.get(Pages.selectSpacePage)
93
94
const elem = await driver.findElement(By.id('selectWithoutMultiple'))
95
const select = new Select(elem)
96
await select.selectByVisibleText(' five')
97
let selectedElement = await select.getFirstSelectedOption()
98
selectedElement.getText().then((text) => {
99
assert.strictEqual(text, ' five')
100
})
101
})
102
103
it('Should convert an unquoted string into one with quotes', async function () {
104
assert.strictEqual(escapeQuotes('abc'), '"abc"')
105
assert.strictEqual(escapeQuotes('abc aqewqqw'), '"abc aqewqqw"')
106
assert.strictEqual(escapeQuotes(''), '""')
107
assert.strictEqual(escapeQuotes(' '), '" "')
108
assert.strictEqual(escapeQuotes(' abc '), '" abc "')
109
})
110
111
it('Should add double quotes to a string that contains a single quote', async function () {
112
assert.strictEqual(escapeQuotes("f'oo"), `"f'oo"`)
113
})
114
115
it('Should add single quotes to a string that contains a double quotes', async function () {
116
assert.strictEqual(escapeQuotes('f"oo'), `'f"oo'`)
117
})
118
119
it('Should provide concatenated strings when string to escape contains both single and double quotes', async function () {
120
assert.strictEqual(escapeQuotes(`f"o'o`), `concat("f", '"', "o'o")`)
121
})
122
123
it('Should provide concatenated strings when string ends with quote', async function () {
124
assert.strictEqual(escapeQuotes(`'"`), `concat("'", '"')`)
125
})
126
127
it('Should select by multiple index', async function () {
128
await driver.get(Pages.formPage)
129
130
let selector = new Select(driver.findElement(By.name(multiSelectValues1['name'])))
131
await selector.deselectAll()
132
133
for (let x in multiSelectValues1['values']) {
134
await selector.selectByIndex(x)
135
}
136
137
let ele = await selector.getAllSelectedOptions()
138
139
for (let x in ele) {
140
assert.deepEqual(await ele[x].getText(), multiSelectValues1['values'][x])
141
}
142
})
143
144
it('Should select by multiple value', async function () {
145
await driver.get(Pages.formPage)
146
147
let selector = new Select(driver.findElement(By.name(multiSelectValues2['name'])))
148
await selector.deselectAll()
149
150
for (let value of multiSelectValues2['values']) {
151
await selector.selectByValue(value)
152
}
153
154
let ele = await selector.getAllSelectedOptions()
155
156
for (let x in ele) {
157
assert.deepEqual(await ele[x].getText(), multiSelectValues2['values'][x])
158
}
159
})
160
161
it('Should select by multiple text', async function () {
162
await driver.get(Pages.formPage)
163
164
let selector = new Select(driver.findElement(By.name(multiSelectValues2['name'])))
165
await selector.deselectAll()
166
167
for (let value of multiSelectValues2['values']) {
168
await selector.selectByVisibleText(value)
169
}
170
171
let ele = await selector.getAllSelectedOptions()
172
173
for (let x in ele) {
174
assert.deepEqual(await ele[x].getText(), multiSelectValues2['values'][x])
175
}
176
})
177
178
it('Should raise exception select by value single disabled', async function () {
179
await driver.get(Pages.formPage)
180
181
let selector = new Select(driver.findElement(By.name(disabledSingleSelect['name'])))
182
183
await assert.rejects(
184
async () => {
185
await selector.selectByValue(disabledSingleSelect.values[1].toLowerCase())
186
},
187
(err) => {
188
assert.strictEqual(err.name, 'UnsupportedOperationError')
189
assert.strictEqual(err.message, 'You may not select a disabled option')
190
return true
191
},
192
)
193
})
194
195
it('Should raise exception select by index single disabled', async function () {
196
await driver.get(Pages.formPage)
197
198
let selector = new Select(driver.findElement(By.name(disabledSingleSelect['name'])))
199
200
await assert.rejects(
201
async () => {
202
await selector.selectByIndex(1)
203
},
204
(err) => {
205
assert.strictEqual(err.name, 'UnsupportedOperationError')
206
assert.strictEqual(err.message, 'You may not select a disabled option')
207
return true
208
},
209
)
210
})
211
212
it('Should raise exception select by text single disabled', async function () {
213
await driver.get(Pages.formPage)
214
215
let selector = new Select(driver.findElement(By.name(disabledSingleSelect['name'])))
216
217
await assert.rejects(
218
async () => {
219
await selector.selectByVisibleText(disabledSingleSelect.values[1])
220
},
221
(err) => {
222
assert.strictEqual(err.name, 'UnsupportedOperationError')
223
assert.strictEqual(err.message, 'You may not select a disabled option')
224
return true
225
},
226
)
227
})
228
229
it('Should raise exception select by index multiple disabled', async function () {
230
await driver.get(Pages.formPage)
231
232
let selector = new Select(driver.findElement(By.name(disabledMultiSelect['name'])))
233
234
await assert.rejects(
235
async () => {
236
await selector.selectByIndex(1)
237
},
238
(err) => {
239
assert.strictEqual(err.name, 'UnsupportedOperationError')
240
assert.strictEqual(err.message, 'You may not select a disabled option')
241
return true
242
},
243
)
244
})
245
246
it('Should raise exception select by value multiple disabled', async function () {
247
await driver.get(Pages.formPage)
248
249
let selector = new Select(driver.findElement(By.name(disabledMultiSelect['name'])))
250
251
await assert.rejects(
252
async () => {
253
await selector.selectByValue(disabledMultiSelect.values[1].toLowerCase())
254
},
255
(err) => {
256
assert.strictEqual(err.name, 'UnsupportedOperationError')
257
assert.strictEqual(err.message, 'You may not select a disabled option')
258
return true
259
},
260
)
261
})
262
263
it('Should raise exception select by text multiple disabled', async function () {
264
await driver.get(Pages.formPage)
265
266
let selector = new Select(driver.findElement(By.name(disabledMultiSelect['name'])))
267
268
await assert.rejects(
269
async () => {
270
await selector.selectByVisibleText(disabledMultiSelect.values[1])
271
},
272
(err) => {
273
assert.strictEqual(err.name, 'UnsupportedOperationError')
274
assert.strictEqual(err.message, 'You may not select a disabled option')
275
return true
276
},
277
)
278
})
279
})
280
},
281
{ browsers: ['firefox', 'chrome'] },
282
)
283
284