/** * * Select option which's displayed text matches the argument. * * :example.html :selectByVisibleText.js it('demonstrate the selectByVisibleText command', function () { var selectBox = $('#selectbox'); console.log(selectBox.getText('option:checked')); // returns "uno" selectBox.selectByVisibleText('cuatro'); console.log(selectBox.getText('option:checked')); // returns "cuatro" }) * * * @alias browser.selectByVisibleText * @param {String} selector select element that contains the options * @param {String} text text of option element to get selected * @uses protocol/element, protocol/elementIdClick, protocol/elementIdElement * @type action * */ import { RuntimeError } from '../utils/ErrorHandler' let selectByVisibleText = function (selector, text) { /** * get select element */ return this.element(selector).then((res) => { /** * check if element was found and throw error if not */ if (!res.value) { throw new RuntimeError(7) } /** * find option elem using xpath */ let formatted = `"${text.trim()}"` if (/"/.test(text)) { formatted = 'concat("' + text.trim().split('"').join('", \'"\', "') + '")' // escape quotes } var normalized = `[normalize-space(.) = ${formatted}]` return this.elementIdElement(res.value.ELEMENT, `./option${normalized}|./optgroup/option${normalized}`) }).then((res) => { /** * check if element was found and throw error if not */ if (!res.value) { throw new RuntimeError(7) } /** * select option */ return this.elementIdClick(res.value.ELEMENT) }) } export default selectByVisibleText