Skip to content

Commit 67ba005

Browse files
authored
[js] Ensure parity in the locators used by methods (#13902)
1 parent 2062410 commit 67ba005

File tree

1 file changed

+14
-30
lines changed
  • javascript/node/selenium-webdriver/lib

1 file changed

+14
-30
lines changed

javascript/node/selenium-webdriver/lib/select.js

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
'use strict'
3838

39-
const { By, escapeCss } = require('./by')
39+
const { By } = require('./by')
4040
const error = require('./error')
4141

4242
/**
@@ -146,6 +146,10 @@ class Select {
146146
* @param {WebElement} element Select WebElement.
147147
*/
148148
constructor(element) {
149+
if (element === null) {
150+
throw new Error(`Element must not be null. Please provide a valid <select> element.`)
151+
}
152+
149153
this.element = element
150154

151155
this.element.getAttribute('tagName').then(function (tagName) {
@@ -220,9 +224,7 @@ class Select {
220224
let matched = false
221225
let isMulti = await this.isMultiple()
222226

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) + ']'))
226228

227229
for (let option of options) {
228230
await this.setSelected(option)
@@ -373,29 +375,9 @@ class Select {
373375
*/
374376
text = typeof text === 'number' ? text.toString() : text
375377

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+
)
399381
if (await optionElement.isSelected()) {
400382
await optionElement.click()
401383
}
@@ -451,9 +433,11 @@ class Select {
451433

452434
let matched = false
453435

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+
}
457441

458442
for (let option of options) {
459443
if (await option.isSelected()) {

0 commit comments

Comments
 (0)