Skip to content

Commit

Permalink
[js] Ensure parity in the locators used by methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed May 8, 2024
1 parent 0345a23 commit 4682486
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions javascript/node/selenium-webdriver/lib/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ class Select {
* @param {WebElement} element Select WebElement.
*/
constructor(element) {
if (element === null) {
throw new Error(`Element must not be null. Please provide a valid <select> element.`)
}

this.element = element

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

let options = await this.element.findElements({
css: 'option[value =' + escapeCss(value) + ']',
})
let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']'))

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

const normalized = text
.trim() // strip leading and trailing white-space characters
.replace(/\s+/, ' ') // replace sequences of whitespace characters by a single space

/**
* find option element using xpath
*/
const formatted = /"/.test(normalized)
? 'concat("' + normalized.split('"').join('", \'"\', "') + '")'
: `"${normalized}"`
const dotFormat = `[. = ${formatted}]`
const spaceFormat = `[normalize-space(text()) = ${formatted}]`

const selections = [
`./option${dotFormat}`,
`./option${spaceFormat}`,
`./optgroup/option${dotFormat}`,
`./optgroup/option${spaceFormat}`,
]

const optionElement = await this.element.findElement({
xpath: selections.join('|'),
})
const optionElement = await this.element.findElement(
By.xpath('.//option[normalize-space(.) = ' + escapeQuotes(text) + ']'),
)
if (await optionElement.isSelected()) {
await optionElement.click()
}
Expand Down Expand Up @@ -451,9 +433,11 @@ class Select {

let matched = false

let options = await this.element.findElements({
css: 'option[value =' + escapeCss(value) + ']',
})
let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']'))

if (options.length === 0) {
throw new Error(`Cannot locate option with value: ${value}`)
}

for (let option of options) {
if (await option.isSelected()) {
Expand Down

0 comments on commit 4682486

Please sign in to comment.