* * Select option with displayed text matching the argument. * * Option 1 Option 2 Option 3 const selectBox = await driver.findElement(By.id("select
(text)
| 258 | * |
| 259 | */ |
| 260 | async selectByVisibleText(text) { |
| 261 | text = typeof text === 'number' ? text.toString() : text |
| 262 | |
| 263 | const xpath = './/option[normalize-space(.) = ' + escapeQuotes(text) + ']' |
| 264 | |
| 265 | const options = await this.element.findElements(By.xpath(xpath)) |
| 266 | |
| 267 | for (let option of options) { |
| 268 | await this.setSelected(option) |
| 269 | if (!(await this.isMultiple())) { |
| 270 | return |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | let matched = Array.isArray(options) && options.length > 0 |
| 275 | |
| 276 | if (!matched && text.includes(' ')) { |
| 277 | const subStringWithoutSpace = getLongestSubstringWithoutSpace(text) |
| 278 | let candidates |
| 279 | if ('' === subStringWithoutSpace) { |
| 280 | candidates = await this.element.findElements(By.tagName('option')) |
| 281 | } else { |
| 282 | const xpath = './/option[contains(., ' + escapeQuotes(subStringWithoutSpace) + ')]' |
| 283 | candidates = await this.element.findElements(By.xpath(xpath)) |
| 284 | } |
| 285 | |
| 286 | const trimmed = text.trim() |
| 287 | |
| 288 | for (let option of candidates) { |
| 289 | const optionText = await option.getText() |
| 290 | if (trimmed === optionText.trim()) { |
| 291 | await this.setSelected(option) |
| 292 | if (!(await this.isMultiple())) { |
| 293 | return |
| 294 | } |
| 295 | matched = true |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (!matched) { |
| 301 | throw new Error(`Cannot locate option with text: ${text}`) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Returns a list of all options belonging to this select tag |
nothing calls this directly
no test coverage detected