1717 * under the License.
1818 */
1919
20- 'use strict' ;
20+ 'use strict'
2121
2222const { By, escapeCss } = require ( './by' )
2323
@@ -27,6 +27,7 @@ const { By, escapeCss } = require('./by')
2727 *
2828 * @interface
2929 */
30+ // eslint-disable-next-line no-unused-vars
3031class ISelect {
3132 /**
3233 * @return {!Promise<boolean> } Whether this select element supports selecting multiple options at the same time? This
@@ -122,16 +123,15 @@ class ISelect {
122123 * @implements ISelect
123124 */
124125class Select {
125-
126126 /**
127127 * Create an Select Element
128128 * @param {WebElement } element Select WebElement.
129129 */
130- constructor ( element ) {
130+ constructor ( element ) {
131131 this . element = element
132132
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' ) {
135135 throw new Error ( `Select only works on <select> elements` )
136136 }
137137 } )
@@ -154,23 +154,27 @@ class Select {
154154 * @param index
155155 */
156156 async selectByIndex ( index ) {
157- if ( index < 0 ) {
157+ if ( index < 0 ) {
158158 throw new Error ( 'Index needs to be 0 or any other positive number' )
159159 }
160160
161- let options = await this . element . findElements ( By . tagName ( 'option' ) ) ;
161+ let options = await this . element . findElements ( By . tagName ( 'option' ) )
162162
163163 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" )
165165 }
166166
167167 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+ )
169173 }
170174
171175 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 ( ) ) ) {
174178 await option . click ( )
175179 }
176180 }
@@ -195,25 +199,25 @@ class Select {
195199 * @param {string } value value of option element to be selected
196200 */
197201 async selectByValue ( value ) {
198- let matched = false ;
199- let isMulti = await this . isMultiple ( ) ;
202+ let matched = false
203+ let isMulti = await this . isMultiple ( )
200204
201205 let options = await this . element . findElements ( {
202206 css : 'option[value =' + escapeCss ( value ) + ']' ,
203207 } )
204208
205209 for ( let option of options ) {
206- if ( ! ( await option . isSelected ( ) ) ) {
210+ if ( ! ( await option . isSelected ( ) ) ) {
207211 await option . click ( )
208212 }
209213
210- if ( ! isMulti ) {
214+ if ( ! isMulti ) {
211215 return
212216 }
213- matched = true ;
217+ matched = true
214218 }
215219
216- if ( ! matched ) {
220+ if ( ! matched ) {
217221 throw new Error ( `Cannot locate option with value: ${ value } ` )
218222 }
219223 }
@@ -236,9 +240,7 @@ class Select {
236240 *
237241 */
238242 async selectByVisibleText ( text ) {
239- text = typeof text === 'number'
240- ? text . toString ( )
241- : text
243+ text = typeof text === 'number' ? text . toString ( ) : text
242244
243245 const normalized = text
244246 . trim ( ) // strip leading and trailing white-space characters
@@ -260,8 +262,10 @@ class Select {
260262 `./optgroup/option${ spaceFormat } ` ,
261263 ]
262264
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 ( ) ) ) {
265269 await optionElement . click ( )
266270 }
267271 }
@@ -271,15 +275,15 @@ class Select {
271275 * @returns {!Promise<!Array<!WebElement>> }
272276 */
273277 async getOptions ( ) {
274- return await this . element . findElements ( { tagName : 'option' } )
278+ return await this . element . findElements ( { tagName : 'option' } )
275279 }
276280
277281 /**
278282 * Retruns a boolean value if the select tag is multiple
279283 * @returns {Promise<boolean> }
280284 */
281285 async isMultiple ( ) {
282- return ( await this . element . getAttribute ( 'multiple' ) ) !== null
286+ return ( await this . element . getAttribute ( 'multiple' ) ) !== null
283287 }
284288
285289 /**
@@ -288,38 +292,38 @@ class Select {
288292 * @returns {Promise<void> }
289293 */
290294 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 )
296300 }
297301 }
298- return results ;
302+ return results
299303 }
300304
301305 /**
302306 * Returns first Selected Option
303307 * @returns {Promise<Element> }
304308 */
305309 async getFirstSelectedOption ( ) {
306- return ( await this . getAllSelectedOptions ( ) ) [ 0 ] ;
310+ return ( await this . getAllSelectedOptions ( ) ) [ 0 ]
307311 }
308312
309313 /**
310314 * Deselects all selected options
311315 * @returns {Promise<void> }
312316 */
313317 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' )
316320 }
317321
318- const options = await this . getOptions ( ) ;
322+ const options = await this . getOptions ( )
319323
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 ( )
323327 }
324328 }
325329 }
@@ -330,16 +334,14 @@ class Select {
330334 * @returns {Promise<void> }
331335 */
332336 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' )
335339 }
336340
337- /**
341+ /**
338342 * convert value into string
339343 */
340- text = typeof text === 'number'
341- ? text . toString ( )
342- : text
344+ text = typeof text === 'number' ? text . toString ( ) : text
343345
344346 const normalized = text
345347 . trim ( ) // strip leading and trailing white-space characters
@@ -361,8 +363,10 @@ class Select {
361363 `./optgroup/option${ spaceFormat } ` ,
362364 ]
363365
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 ( ) ) {
366370 await optionElement . click ( )
367371 }
368372 }
@@ -376,27 +380,31 @@ class Select {
376380 * @returns {Promise<void> }
377381 */
378382 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' )
381385 }
382386
383- if ( index < 0 ) {
387+ if ( index < 0 ) {
384388 throw new Error ( 'Index needs to be 0 or any other positive number' )
385389 }
386390
387- let options = await this . element . findElements ( By . tagName ( 'option' ) ) ;
391+ let options = await this . element . findElements ( By . tagName ( 'option' ) )
388392
389393 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" )
391395 }
392396
393397 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+ )
395403 }
396404
397405 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 ( ) ) {
400408 await option . click ( )
401409 }
402410 }
@@ -409,27 +417,27 @@ class Select {
409417 * @returns {Promise<void> }
410418 */
411419 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' )
414422 }
415423
416- let matched = false ;
424+ let matched = false
417425
418426 let options = await this . element . findElements ( {
419427 css : 'option[value =' + escapeCss ( value ) + ']' ,
420428 } )
421429
422430 for ( let option of options ) {
423- if ( await option . isSelected ( ) ) {
431+ if ( await option . isSelected ( ) ) {
424432 await option . click ( )
425433 }
426- matched = true ;
434+ matched = true
427435 }
428436
429- if ( ! matched ) {
437+ if ( ! matched ) {
430438 throw new Error ( `Cannot locate option with value: ${ value } ` )
431439 }
432440 }
433441}
434442
435- module . exports = { Select } ;
443+ module . exports = { Select }
0 commit comments