def __init__(self, selector, locator=None, between=None, count=None, exact=None, exact_text=None, filter=None, match=None, maximum=None, minimum=None, text=None, visible=None, wait=None, **filter_options): if locator is None and ( not isinstance(selector, Hashable) or selector not in selectors ): locator = selector selector = capybara.default_selector self.selector = selectors[selector] self.expression = self.selector(locator) self.locator = locator self.options = { "between": between, "count": count, "exact": exact, "exact_text": exact_text, "filter": filter, "match": match, "maximum": maximum, "minimum": minimum, "text": text, "visible": visible, "wait": wait} self.filter_options = filter_options assert self.match in VALID_MATCH, \ "invalid option {match} for match, should be one of {valid_values}".format( match=desc(self.match), valid_values=", ".join(desc(VALID_MATCH)))
def description(self): if isregex(self.expected_text): return "text matching {}".format(desc(self.expected_text)) elif self.options["exact_text"]: return "exact text {}".format(desc(self.expected_text)) else: return "text {}".format(desc(self.expected_text))
def _build_message(self, negated=""): verb = "match" if isregex(self.expected_title) else "include" return "expected {actual}{negated} to {verb} {expected}".format( actual=desc(self.actual_title), negated=negated, verb=verb, expected=desc(self.expected_title))
def __init__(self, query_type, expected_text=None, between=None, count=None, exact_text=False, maximum=None, minimum=None, wait=None): if expected_text is None: expected_text = query_type query_type = None if query_type is None: query_type = ("visible" if capybara.ignore_hidden_elements or capybara.visible_text_only else "all") assert query_type in VALID_QUERY_TYPE, \ "invalid option {query_type} for query_type, should be one of {valid_values}".format( query_type=desc(query_type), valid_values=", ".join(desc(VALID_QUERY_TYPE))) self.expected_text = (expected_text if isregex(expected_text) else normalize_text(expected_text)) self.query_type = query_type self.search_regexp = toregex(expected_text, exact=exact_text) self.options = { "between": between, "count": count, "exact_text": exact_text, "maximum": maximum, "minimum": minimum, "wait": wait} self.node = None self.actual_text = None self.count = None
def _build_message(self, negated=""): verb = "match" if isregex(self.expected_path) else "equal" return "expected {actual}{negated} to {verb} {expected}".format( actual=desc(self.actual_path), negated=negated, verb=verb, expected=desc(self.expected_path))
def description(self): """ str: A long description of this query. """ description = self.label if self.locator: description += " {}".format(desc(self.locator)) if self.options["text"] is not None: description += " with text {}".format(desc(self.options["text"])) description += self.selector.description(self.filter_options) return description
def _build_message(self, report_on_invisible): message = failure_message(self.description, self.options) if any(self.options.values()): message += " but found {count} {times}".format( count=self.count, times=declension("time", "times", self.count)) message += " in {actual}".format(actual=desc(self.actual_text)) details_message = [] if self.node and not self.search_regexp.flags & re.IGNORECASE: insensitive_regex = re.compile( self.search_regexp.pattern, flags=(self.search_regexp.flags | re.IGNORECASE)) insensitive_count = len(re.findall(insensitive_regex, self.actual_text)) if insensitive_count != self.count: details_message.append( "it was found {count} {times} using a case insensitive search".format( count=insensitive_count, times=declension("time", "times", insensitive_count))) if self.node and self.query_type == "visible" and report_on_invisible: invisible_text = normalize_text(self.node.all_text) invisible_count = len(re.findall(self.search_regexp, invisible_text)) if invisible_count != self.count: details_message.append( "it was found {count} {times} including non-visible text".format( count=invisible_count, times=declension("time", "times", invisible_count))) if details_message: message += ". (However, {details}.)".format(details=" and ".join(details_message)) return message
def describe(options): description = "" if options.get("multiple") is True: description += " with the multiple attribute" if options.get("multiple") is False: description += " without the multiple attribute" if options.get("options"): description += " with options {}".format(desc(options["options"])) if options.get("selected"): description += " with {} selected".format(desc( options["selected"])) if options.get("with_options"): description += " with at least options {}".format( desc(options["with_options"])) return description
def failure_message(self): """ str: A message describing the query failure. """ message = failure_message(self.query.description, self.query.options) if len(self) > 0: message += ", found {count} {matches}: {results}".format( count=len(self), matches=declension("match", "matches", len(self)), results=", ".join([desc(node.text) for node in self])) else: message += " but there were no matches" if self.rest: elements = ", ".join([desc(element.text) for element in self.rest]) message += (". Also found {}, which matched the selector" " but not all filters.".format(elements)) return message
def _find_modal(self, text=None, wait=None): wait = wait or capybara.default_max_wait_time try: alert = WebDriverWait(self.browser, wait).until(EC.alert_is_present()) regexp = toregex(text) if not regexp.search(alert.text): qualifier = "matching" if isregex(text) else "with" raise ModalNotFound( "Unable to find modal dialog {0} {1}".format( qualifier, desc(text))) return alert except TimeoutException: raise ModalNotFound("Unable to find modal dialog")
def describe(options): description = "" if options.get("href"): description += " with href {}".format(desc(options["href"])) return description
def describe(options): description = "" if options.get("value"): description += " with value {}".format(desc(options["value"])) return description
def failure_message(self): """ str: A message describing the query failure. """ return ("Expected node to have styles {expected}. " "Actual styles were {actual}").format( expected=desc(self.expected_styles), actual=desc(self.actual_styles))
def description(self): if isregex(self.expected_text): return "text matching {}".format(desc(self.expected_text)) else: return "text {}".format(desc(self.expected_text))