def clear(self): """ Clears all selected options """ if not self.multiple: raise Error('you can only clear multi-selects') for option in self.selected_options: option.click()
def _select_by_all(self, how, term): from ..locators.element.selector_builder import SelectorBuilder if not self.multiple: raise Error('you can only use #select_all on multi-selects') found = [] def func(sel): elements = [] if type(term) in SelectorBuilder.VALID_WHATS: opt = {how: term} elements.extend(sel.options(**opt)) if not list(found): elements.extend(sel.options(label=term)) else: raise TypeError('expected str or regexp, got {}:{}'.format( term, term.__class__)) if len(elements) > 0: found.extend(elements) return True try: Wait.until(func, object=self) except TimeoutError: raise NoValueFoundException( '{} not found in select list'.format(term)) return self._select_matching(found)
def _matches_regexp(self, how, element, exp): if how == 'text': return (re.search(exp, self.el.text) or re.search(exp, self.el.label)) \ is not None elif how == 'value': return re.search(exp, element.value) is not None else: raise Error('unknown how: {}'.format(how))
def stale(self): """ Returns True if a previously located element is no longer attached to the DOM :rtype: bool """ if self.el is None: raise Error('Can not check staleness of unused element') self._ensure_context() return self.stale_in_context
def _select_all_by(self, term): if not self.multiple: raise Error('you can only use #select_all on multi-selects') found = self._find_options('text', term) if found: return self._select_matching(found) raise NoValueFoundException('{} not found in select list'.format(term))
def _set_content_editable(self, *args): input_text = ''.join(args) self._element_call( lambda: self._execute_js('setText', self.el, input_text)) text = self.text if text == input_text: return raise Error("#js_set text: '{}' does not match expected input: " "'{}'".format(text, input_text))
def _execute_js(self, function_name, *args): filepath = path.abspath( path.join(path.dirname(__file__), 'js_snippets', '{}.js'.format(function_name))) if not path.isfile(filepath): from nerodia.exception import Error raise Error('Can not excute script as {!r} does not exist'.format( filepath)) with open(filepath, 'r') as myfile: script = 'return ({}).apply(null, arguments)'.format(myfile.read()) return self.query_scope.execute_script(script, *args)
def text(self): """ Returns the text of the button. For input elements, returns the 'value' attribute. For button elements, returns the inner text. :rtype: str """ tn = self.tag_name if tn == 'input': return self.value elif tn == 'button': return super(Button, self).text else: raise Error("unknown tag name for button: {}".format(tn))
def js_set(self, *args): """ Uses JavaScript to enter most of the given value Selenium is used to enter the first and last characters (in order to raise an error if the element would normally raise on sending keys) :param value: value to set the input to """ from nerodia.exception import Error value = ''.join(args) if value: self.set(value[0]) if len(value) > 1: self._element_call( lambda: self._execute_js('setValue', self.el, value[:-1])) self.append(value[-1]) if self.value != value: raise Error('js_set value does not match expected input')
def js_set(self, *args): """ Uses JavaScript to enter most of the given value Selenium is used to enter the first and last characters (in order to raise an error if the element would normally raise on sending keys) :param value: value to set the input to """ from nerodia.exception import Error input_value = ''.join(args) if input_value: self.set(input_value[0]) if hasattr(self, '_content_editable'): return self._set_content_editable(*args) if len(input_value) > 1: self._element_call(lambda: self._execute_js( 'setValue', self.el, input_value[:-1])) self.append(input_value[-1]) value = self.value if value != input_value: raise Error("#js_set value: '{}' does not match expected input: " "'{}'".format(value, input_value))
def _select_all_by(self, term): if not self.multiple: raise Error('you can only use #select_all on multi-selects') found = self._find_options('text', term) return self._select_matching(found)