17
17
* under the License.
18
18
*/
19
19
20
-
'use strict';
20
+
'use strict'
21
21
22
22
const { By, escapeCss } = require('./by')
23
23
@@ -27,6 +27,7 @@ const { By, escapeCss } = require('./by')
27
27
*
28
28
* @interface
29
29
*/
30
+
// eslint-disable-next-line no-unused-vars
30
31
class ISelect {
31
32
/**
32
33
* @return {!Promise<boolean>} Whether this select element supports selecting multiple options at the same time? This
@@ -122,16 +123,15 @@ class ISelect {
122
123
* @implements ISelect
123
124
*/
124
125
class Select {
125
-
126
126
/**
127
127
* Create an Select Element
128
128
* @param {WebElement} element Select WebElement.
129
129
*/
130
-
constructor (element) {
130
+
constructor(element) {
131
131
this.element = element
132
132
133
-
this.element.getAttribute('tagName').then(function(tagName){
134
-
if(tagName.toLowerCase() !== 'select') {
133
+
this.element.getAttribute('tagName').then(function (tagName) {
134
+
if (tagName.toLowerCase() !== 'select') {
135
135
throw new Error(`Select only works on <select> elements`)
136
136
}
137
137
})
@@ -154,23 +154,27 @@ class Select {
154
154
* @param index
155
155
*/
156
156
async selectByIndex(index) {
157
-
if(index < 0) {
157
+
if (index < 0) {
158
158
throw new Error('Index needs to be 0 or any other positive number')
159
159
}
160
160
161
-
let options = await this.element.findElements(By.tagName('option'));
161
+
let options = await this.element.findElements(By.tagName('option'))
162
162
163
163
if (options.length === 0) {
164
-
throw new Error('Select element doesn\'t contain any option element')
164
+
throw new Error("Select element doesn't contain any option element")
165
165
}
166
166
167
167
if (options.length - 1 < index) {
168
-
throw new Error(`Option with index "${index}" not found. Select element only contains ${options.length-1} option elements`)
168
+
throw new Error(
169
+
`Option with index "${index}" not found. Select element only contains ${
170
+
options.length - 1
171
+
} option elements`
172
+
)
169
173
}
170
174
171
175
for (let option of options) {
172
-
if(await option.getAttribute('index') === index.toString()) {
173
-
if(!(await option.isSelected())){
176
+
if ((await option.getAttribute('index')) === index.toString()) {
177
+
if (!(await option.isSelected())) {
174
178
await option.click()
175
179
}
176
180
}
@@ -195,25 +199,25 @@ class Select {
195
199
* @param {string} value value of option element to be selected
196
200
*/
197
201
async selectByValue(value) {
198
-
let matched = false;
199
-
let isMulti = await this.isMultiple();
202
+
let matched = false
203
+
let isMulti = await this.isMultiple()
200
204
201
205
let options = await this.element.findElements({
202
206
css: 'option[value =' + escapeCss(value) + ']',
203
207
})
204
208
205
209
for (let option of options) {
206
-
if(!(await option.isSelected())){
210
+
if (!(await option.isSelected())) {
207
211
await option.click()
208
212
}
209
213
210
-
if(!isMulti) {
214
+
if (!isMulti) {
211
215
return
212
216
}
213
-
matched = true;
217
+
matched = true
214
218
}
215
219
216
-
if(!matched) {
220
+
if (!matched) {
217
221
throw new Error(`Cannot locate option with value: ${value}`)
218
222
}
219
223
}
@@ -236,9 +240,7 @@ class Select {
236
240
*
237
241
*/
238
242
async selectByVisibleText(text) {
239
-
text = typeof text === 'number'
240
-
? text.toString()
241
-
: text
243
+
text = typeof text === 'number' ? text.toString() : text
242
244
243
245
const normalized = text
244
246
.trim() // strip leading and trailing white-space characters
@@ -260,8 +262,10 @@ class Select {
260
262
`./optgroup/option${spaceFormat}`,
261
263
]
262
264
263
-
const optionElement = await this.element.findElement({xpath: selections.join('|')})
264
-
if(!(await optionElement.isSelected())){
265
+
const optionElement = await this.element.findElement({
266
+
xpath: selections.join('|'),
267
+
})
268
+
if (!(await optionElement.isSelected())) {
265
269
await optionElement.click()
266
270
}
267
271
}
@@ -271,15 +275,15 @@ class Select {
271
275
* @returns {!Promise<!Array<!WebElement>>}
272
276
*/
273
277
async getOptions() {
274
-
return await this.element.findElements({tagName: 'option'})
278
+
return await this.element.findElements({ tagName: 'option' })
275
279
}
276
280
277
281
/**
278
282
* Retruns a boolean value if the select tag is multiple
279
283
* @returns {Promise<boolean>}
280
284
*/
281
285
async isMultiple() {
282
-
return (await this.element.getAttribute('multiple'))!==null
286
+
return (await this.element.getAttribute('multiple')) !== null
283
287
}
284
288
285
289
/**
@@ -288,38 +292,38 @@ class Select {
288
292
* @returns {Promise<void>}
289
293
*/
290
294
async getAllSelectedOptions() {
291
-
const opts = await this.getOptions();
292
-
const results = [];
293
-
for(let options of opts) {
294
-
if(await options.isSelected()){
295
-
results.push(options);
295
+
const opts = await this.getOptions()
296
+
const results = []
297
+
for (let options of opts) {
298
+
if (await options.isSelected()) {
299
+
results.push(options)
296
300
}
297
301
}
298
-
return results;
302
+
return results
299
303
}
300
304
301
305
/**
302
306
* Returns first Selected Option
303
307
* @returns {Promise<Element>}
304
308
*/
305
309
async getFirstSelectedOption() {
306
-
return (await this.getAllSelectedOptions())[0];
310
+
return (await this.getAllSelectedOptions())[0]
307
311
}
308
312
309
313
/**
310
314
* Deselects all selected options
311
315
* @returns {Promise<void>}
312
316
*/
313
317
async deselectAll() {
314
-
if(!this.isMultiple()){
315
-
throw new Error("You may only deselect all options of a multi-select")
318
+
if (!this.isMultiple()) {
319
+
throw new Error('You may only deselect all options of a multi-select')
316
320
}
317
321
318
-
const options = await this.getOptions();
322
+
const options = await this.getOptions()
319
323
320
-
for(let option of options){
321
-
if(await option.isSelected()) {
322
-
await option.click();
324
+
for (let option of options) {
325
+
if (await option.isSelected()) {
326
+
await option.click()
323
327
}
324
328
}
325
329
}
@@ -330,16 +334,14 @@ class Select {
330
334
* @returns {Promise<void>}
331
335
*/
332
336
async deselectByVisibleText(text) {
333
-
if(!(await this.isMultiple())){
334
-
throw new Error("You may only deselect options of a multi-select")
337
+
if (!(await this.isMultiple())) {
338
+
throw new Error('You may only deselect options of a multi-select')
335
339
}
336
340
337
-
/**
341
+
/**
338
342
* convert value into string
339
343
*/
340
-
text = typeof text === 'number'
341
-
? text.toString()
342
-
: text
344
+
text = typeof text === 'number' ? text.toString() : text
343
345
344
346
const normalized = text
345
347
.trim() // strip leading and trailing white-space characters
@@ -361,8 +363,10 @@ class Select {
361
363
`./optgroup/option${spaceFormat}`,
362
364
]
363
365
364
-
const optionElement = await this.element.findElement({xpath: selections.join('|')})
365
-
if(await optionElement.isSelected()){
366
+
const optionElement = await this.element.findElement({
367
+
xpath: selections.join('|'),
368
+
})
369
+
if (await optionElement.isSelected()) {
366
370
await optionElement.click()
367
371
}
368
372
}
@@ -376,27 +380,31 @@ class Select {
376
380
* @returns {Promise<void>}
377
381
*/
378
382
async deselectByIndex(index) {
379
-
if(!(await this.isMultiple())){
380
-
throw new Error("You may only deselect options of a multi-select")
383
+
if (!(await this.isMultiple())) {
384
+
throw new Error('You may only deselect options of a multi-select')
381
385
}
382
386
383
-
if(index < 0) {
387
+
if (index < 0) {
384
388
throw new Error('Index needs to be 0 or any other positive number')
385
389
}
386
390
387
-
let options = await this.element.findElements(By.tagName('option'));
391
+
let options = await this.element.findElements(By.tagName('option'))
388
392
389
393
if (options.length === 0) {
390
-
throw new Error('Select element doesn\'t contain any option element')
394
+
throw new Error("Select element doesn't contain any option element")
391
395
}
392
396
393
397
if (options.length - 1 < index) {
394
-
throw new Error(`Option with index "${index}" not found. Select element only contains ${options.length-1} option elements`)
398
+
throw new Error(
399
+
`Option with index "${index}" not found. Select element only contains ${
400
+
options.length - 1
401
+
} option elements`
402
+
)
395
403
}
396
404
397
405
for (let option of options) {
398
-
if(await option.getAttribute('index') === index.toString()) {
399
-
if(await option.isSelected()){
406
+
if ((await option.getAttribute('index')) === index.toString()) {
407
+
if (await option.isSelected()) {
400
408
await option.click()
401
409
}
402
410
}
@@ -409,27 +417,27 @@ class Select {
409
417
* @returns {Promise<void>}
410
418
*/
411
419
async deselectByValue(value) {
412
-
if(!(await this.isMultiple())){
413
-
throw new Error("You may only deselect options of a multi-select")
420
+
if (!(await this.isMultiple())) {
421
+
throw new Error('You may only deselect options of a multi-select')
414
422
}
415
423
416
-
let matched = false;
424
+
let matched = false
417
425
418
426
let options = await this.element.findElements({
419
427
css: 'option[value =' + escapeCss(value) + ']',
420
428
})
421
429
422
430
for (let option of options) {
423
-
if(await option.isSelected()){
431
+
if (await option.isSelected()) {
424
432
await option.click()
425
433
}
426
-
matched = true;
434
+
matched = true
427
435
}
428
436
429
-
if(!matched) {
437
+
if (!matched) {
430
438
throw new Error(`Cannot locate option with value: ${value}`)
431
439
}
432
440
}
433
441
}
434
442
435
-
module.exports = { Select };
443
+
module.exports = { Select }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4