* * Select option with specified index. * * Option 1 Option 2 Option 3 const selectBox = await driver.findElement(By.id("selectbox")); await sele
(index)
| 180 | * @param index |
| 181 | */ |
| 182 | async selectByIndex(index) { |
| 183 | if (index < 0) { |
| 184 | throw new Error('Index needs to be 0 or any other positive number') |
| 185 | } |
| 186 | |
| 187 | let options = await this.element.findElements(By.tagName('option')) |
| 188 | |
| 189 | if (options.length === 0) { |
| 190 | throw new Error("Select element doesn't contain any option element") |
| 191 | } |
| 192 | |
| 193 | if (options.length - 1 < index) { |
| 194 | throw new Error( |
| 195 | `Option with index "${index}" not found. Select element only contains ${options.length - 1} option elements`, |
| 196 | ) |
| 197 | } |
| 198 | |
| 199 | for (let option of options) { |
| 200 | if ((await option.getAttribute('index')) === index.toString()) { |
| 201 | await this.setSelected(option) |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * |
nothing calls this directly
no test coverage detected