def select_option(select, option, unselect=False, **kwargs): # pylint: disable=unused-argument """Click and optionally verify condition after click. Parameters ---------- select : object Instance of Select class option : str Text to select unselect : bool Select (False, default) or unselect (True) given option """ option_list = [] value_list = [] if option.startswith('[[') and option.endswith(']]'): option = option.strip('[]') if option.isdigit(): try: if unselect: select.deselect_by_index(option) else: select.select_by_index(option) return True except TypeError as te: raise QWebValueMismatchError('Index out of range') from te try: if unselect: select.deselect_by_visible_text(option) else: select.select_by_visible_text(option) return True except NoSuchElementException: try: if unselect: select.deselect_by_value(option) else: select.select_by_value(option) return True except NoSuchElementException: if select: for opt in select.options: option_list.append(opt.text) value_list.append(opt.get_attribute('value')) if option_list != value_list: raise QWebValueMismatchError(f'Option "{option}" is not in the options list.\n' # pylint: disable=W0707 f'The list contained these options: {option_list}.\n' f'The list contained these values: {value_list}.') raise QWebValueMismatchError(f'Option "{option}" is not in the options list.\n' # pylint: disable=W0707 'The list contained these options: {option_list}.\n')
def verify(self, text, normalize=False): txt_content = self._normalize_text( self.content) if normalize else self.content if text in txt_content: return True raise QWebValueMismatchError( 'File did not contain the text "{}"'.format(text))
def get_substring(text, **kwargs): if '\xa0' in text: text = text.replace('\xa0', ' ') start, end = kwargs.get('between', '{}???{}').format(0, len(text)).split('???') include_start = kwargs.get('include_locator', False) exclude_end = kwargs.get('exclude_post', True) start = get_index_of(text, start, include_start) end = get_index_of(text, end, exclude_end) if end == 0: end = len(text) if 'from_start' in kwargs: end = start + int(kwargs.get('from_start')) if 'from_end' in kwargs: start = end - int(kwargs.get('from_end')) logger.debug('substring start: {}'.format(start)) logger.debug('substring end: {}'.format(end)) text = str(text[start:end]).strip().replace('\n', "") try: if 'int' in kwargs: num = float(text.replace(' ', '').replace(',', '.')) return int(num) if 'float' in kwargs: return float(text.replace(' ', '').replace(',', '.')) except ValueError as e: raise QWebValueMismatchError( 'Unable to convert. Got exception: {}'.format(e)) from e return text
def get_index_of(self, text, condition): index = self.content.find(text) if index > -1: if util.par2bool(condition) is False: index += len(text) return index raise QWebValueMismatchError( 'File did not contain the text "{}"'.format(text))
def get_element_text(web_element, expected=None, timeout=0): # pylint: disable=unused-argument real_text = web_element.text.strip() if expected is not None: try: return _compare_texts(real_text, expected.strip(), timeout) except QWebValueMismatchError as e: raise QWebValueError('Expected {}, found {}'.format(expected, real_text)) from e if real_text is not None: return real_text raise QWebValueMismatchError('Text not found')
def verify_length(expected_length): """Verify lists length.""" if _list_exists(): active = ACTIVE_LIST.update_list() list_length = len(active.web_list) if int(expected_length) == list_length: return raise QWebValueMismatchError( 'Expected length "{}" didn\'t match to list length "{}".'.format( expected_length, list_length))
def compare_input_values(input_element, expected_value, timeout): # pylint: disable=unused-argument try: real_value = util.get_substring(input_value(input_element, timeout="0.5s")) except QWebValueError: real_value = "" logger.debug('Real value: {}, expected value {}'.format(real_value, expected_value)) if fnmatch.fnmatch(real_value.strip(), expected_value): return True raise QWebValueMismatchError('Expected value "{}" didn\'t match to real value "{}"' .format(expected_value, real_value))
def select_option(select, option, **kwargs): # pylint: disable=unused-argument """Click and optionally verify condition after click. Parameters ---------- select : object Instance of Select class option : str Text to select """ option_list = [] value_list = [] if option.startswith('[[') and option.endswith(']]'): option = option.strip('[]') if option.isdigit(): try: select.select_by_index(option) return True except TypeError: raise QWebValueMismatchError('Index out of range') try: select.select_by_visible_text(option) return True except NoSuchElementException: try: select.select_by_value(option) return True except NoSuchElementException: if select: for opt in select.options: option_list.append(opt.text) value_list.append(opt.get_attribute('value')) if option_list != value_list: raise QWebValueMismatchError( 'Option "{}" is not in the options list.\n' 'The list contained these options: {}.\n' 'The list contained these values: {}.'.format( option, option_list, value_list)) raise QWebValueMismatchError( 'Option "{}" is not in the options list.\n' 'The list contained these options: {}.\n'.format( option, option_list))
def create_text_file_instance(filename): filepath = download.get_path(filename) try: with open(filepath, 'rb') as txt_file: data = txt_file.read() data = data.decode("utf-8") if data != '': return File(data, filepath) raise QWebValueMismatchError( 'Text not found. Seems that the file is empty.') except TypeError as e: raise QWebFileNotFoundError( 'File not found. Got {} instead.'.format(e)) from e
def text_appearance(text, **kwargs): """ Sub for retry click. Works as keywords is_text and is_no_text. Returns True if text exists/not exists in given time. Raises QWebValueMismatchErr for decorator to handle if condition is not expected (False). """ try: element = internal_text.get_element_by_locator_text( text, allow_non_existent=True, **kwargs) except QWebTimeoutError: if kwargs['text_appear'] is False: return True raise QWebValueMismatchError('return value should be true') try: if element and kwargs['text_appear'] is True: return True if not element and kwargs['text_appear'] is False: return True except QWebUnexpectedConditionError: logger.debug('StaleElement Err from text appearance') raise QWebValueMismatchError('return value should be true')
def get_index_of(text, locator, condition): try: return int(locator.strip()) except ValueError: if locator.startswith('\\'): locator.replace('\\', "") index = text.find(locator.strip()) if index > -1: if par2bool(condition) is False: index += len(locator) return index raise QWebValueMismatchError( 'File did not contain the text "{}"'.format(locator))
def get_select_options(select, expected=None, **kwargs): # pylint: disable=unused-argument options = select.options if expected: for option in options: logger.debug(option.text) if fnmatch.fnmatch(expected, option.text): return True raise QWebValueMismatchError( 'Expected value "{}" not found from selectable options'.format(expected)) # parse all options to a list and return it option_list = [] for option in options: option_list.append(option.text) return option_list
def create_pdf_instance(filename): all_text = '' filepath = download.get_path(filename) try: with open(filepath, 'rb') as pdf_obj: pdf = slate_pdf_reader.PDF(pdf_obj) for page in pdf: all_text += page.strip() if all_text != '': return File(all_text, filepath) raise QWebValueMismatchError( 'Text not found. Seems that the pdf is empty.') except TypeError as e: raise QWebFileNotFoundError( f'File not found. Got {e} instead.') from e except PSEOF as e: raise QWebFileNotFoundError( f'File found, but it\'s not valid pdf-file: {e}') from e
def get_selected_value(select, expected=None, **kwargs): # pylint: disable=unused-argument """Get or verify selected value. Parameters ---------- select : object Instance of Select class expected : str Text to compare with selected value """ selected = select.first_selected_option.text if expected: if fnmatch.fnmatch(expected, selected) is True: return True raise QWebValueMismatchError( 'Expected value "{}" didn\'t match to real value "{}".'.format( expected, selected)) return selected
def verify_no_list(text, index=None): """Verify that text is not in the list. Examples -------- .. code-block:: robotframework UseList Qentinel VerifyNoList Pace Robot #Text is not in list VerifyNoList Test Automation 2 #List index 2 not containing text """ if _list_exists(): active = ACTIVE_LIST.update_list() if index: index = _check_index(index) if not active.contains(text, index): return raise QWebValueMismatchError('List contains text "{}"'.format(text))
def get_selected_value(select, expected=None, **kwargs): # pylint: disable=unused-argument """Get or verify selected value. Parameters ---------- select : object Instance of Select class expected : str Text to compare with selected value """ sel_elems = select.all_selected_options selected = [ele.text for ele in sel_elems] txt_selected = ",".join(selected) if expected: if expected in selected: return True raise QWebValueMismatchError('Expected value "{}" didn\'t match to real value "{}".' .format(expected, txt_selected)) return txt_selected
def verify(self, text): if text in self.content: return True raise QWebValueMismatchError( 'File did not contain the text "{}"'.format(text))
def _compare_texts(text_to_compare, expected, timeout): # pylint: disable=unused-argument if fnmatch.fnmatch(text_to_compare, expected) is False: raise QWebValueMismatchError('Expected {0}, found {1}'.format( expected, text_to_compare)) return True