def _find_options(self, how, term): types = [six.text_type, six.binary_type, int, Pattern] found = [] def func(sel): if type(term) in types: collection = sel.options(value=term) if how == 'value' else [] if not list(collection): collection = sel.options(text=term) if not list(collection): collection = sel.options(label=term) if collection: found.append(collection) return False else: return not found and nerodia.relaxed_locate else: raise TypeError('expected {!r}, got {}:{}'.format( types, term, term.__class__)) try: Wait.until_not(func, object=self) if found: return found[0] raise NoValueFoundException( '{} not found in select list'.format(term)) except TimeoutError: raise NoValueFoundException( '{} not found in select list'.format(term))
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 _select_by(self, how, term): from ..locators.element.selector_builder import SelectorBuilder 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) > 1: nerodia.logger.deprecate( 'Selecting Multiple Options with #select', '#select_all') 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 _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 _select_by(self, term): found = self._find_options('value', term) if found: if len(found) > 1: nerodia.logger.deprecate( 'Selecting Multiple Options with #select', '#select_all') return self._select_matching(found) raise NoValueFoundException('{} not found in select list'.format(term))
def _js_select_by(self, term, number): if isinstance(term, Pattern): js_rx = term.pattern js_rx = js_rx.replace('\\A', '^', 1) js_rx = js_rx.replace('\\Z', '$', 1) js_rx = js_rx.replace('\\z', '$', 1) js_rx = re.sub(r'\(\?#.+\)', '', js_rx) js_rx = re.sub(r'\(\?-\w+:', '(', js_rx) elif type(term) in [six.text_type, six.binary_type]: js_rx = '^{}$'.format(term) else: raise TypeError('expected String or Regexp, got {}'.format(term)) for way in ['text', 'label', 'value']: self._element_call(lambda: self._execute_js('selectOptions{}'.format(way.capitalize()), self, js_rx, str(number))) if self._is_matching_option(way, term): return self.selected_options[0].text raise NoValueFoundException('{} not found in select list'.format(term))
def _raise_no_value_found(self, term): raise NoValueFoundException('{} not found in {}'.format(term, self))