|
36 | 36 |
|
37 | 37 | 'use strict'
|
38 | 38 |
|
39 |
| -const { By, escapeCss } = require('./by') |
| 39 | +const { By } = require('./by') |
40 | 40 | const error = require('./error')
|
41 | 41 |
|
42 | 42 | /**
|
@@ -146,6 +146,10 @@ class Select {
|
146 | 146 | * @param {WebElement} element Select WebElement.
|
147 | 147 | */
|
148 | 148 | constructor(element) {
|
| 149 | + if (element === null) { |
| 150 | + throw new Error(`Element must not be null. Please provide a valid <select> element.`) |
| 151 | + } |
| 152 | + |
149 | 153 | this.element = element
|
150 | 154 |
|
151 | 155 | this.element.getAttribute('tagName').then(function (tagName) {
|
@@ -220,9 +224,7 @@ class Select {
|
220 | 224 | let matched = false
|
221 | 225 | let isMulti = await this.isMultiple()
|
222 | 226 |
|
223 |
| - let options = await this.element.findElements({ |
224 |
| - css: 'option[value =' + escapeCss(value) + ']', |
225 |
| - }) |
| 227 | + let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']')) |
226 | 228 |
|
227 | 229 | for (let option of options) {
|
228 | 230 | await this.setSelected(option)
|
@@ -373,29 +375,9 @@ class Select {
|
373 | 375 | */
|
374 | 376 | text = typeof text === 'number' ? text.toString() : text
|
375 | 377 |
|
376 |
| - const normalized = text |
377 |
| - .trim() // strip leading and trailing white-space characters |
378 |
| - .replace(/\s+/, ' ') // replace sequences of whitespace characters by a single space |
379 |
| - |
380 |
| - /** |
381 |
| - * find option element using xpath |
382 |
| - */ |
383 |
| - const formatted = /"/.test(normalized) |
384 |
| - ? 'concat("' + normalized.split('"').join('", \'"\', "') + '")' |
385 |
| - : `"${normalized}"` |
386 |
| - const dotFormat = `[. = ${formatted}]` |
387 |
| - const spaceFormat = `[normalize-space(text()) = ${formatted}]` |
388 |
| - |
389 |
| - const selections = [ |
390 |
| - `./option${dotFormat}`, |
391 |
| - `./option${spaceFormat}`, |
392 |
| - `./optgroup/option${dotFormat}`, |
393 |
| - `./optgroup/option${spaceFormat}`, |
394 |
| - ] |
395 |
| - |
396 |
| - const optionElement = await this.element.findElement({ |
397 |
| - xpath: selections.join('|'), |
398 |
| - }) |
| 378 | + const optionElement = await this.element.findElement( |
| 379 | + By.xpath('.//option[normalize-space(.) = ' + escapeQuotes(text) + ']'), |
| 380 | + ) |
399 | 381 | if (await optionElement.isSelected()) {
|
400 | 382 | await optionElement.click()
|
401 | 383 | }
|
@@ -451,9 +433,11 @@ class Select {
|
451 | 433 |
|
452 | 434 | let matched = false
|
453 | 435 |
|
454 |
| - let options = await this.element.findElements({ |
455 |
| - css: 'option[value =' + escapeCss(value) + ']', |
456 |
| - }) |
| 436 | + let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']')) |
| 437 | + |
| 438 | + if (options.length === 0) { |
| 439 | + throw new Error(`Cannot locate option with value: ${value}`) |
| 440 | + } |
457 | 441 |
|
458 | 442 | for (let option of options) {
|
459 | 443 | if (await option.isSelected()) {
|
|
0 commit comments