UNPKG

webdriverio

Version:

Next-gen browser and mobile automation test framework for Node.js

24 lines (19 loc) 1.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = selectByVisibleText; var _utils = require("../../utils"); async function selectByVisibleText(text) { text = typeof text === 'number' ? text.toString() : text; const normalized = text.trim().replace(/\s+/, ' '); 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.findElementFromElement(this.elementId, 'xpath', selections.join('|')); if (optionElement && optionElement.error === 'no such element') { throw new Error(`Option with text "${text}" not found.`); } return this.elementClick((0, _utils.getElementFromResponse)(optionElement)); }