Example #1
0
    def matches_filters(self, node):
        """
        Returns whether the given node matches all filters.

        Args:
            node (Element): The node to evaluate.

        Returns:
            bool: Whether the given node matches.
        """

        visible = self.visible

        if self.options["text"]:
            if isregex(self.options["text"]):
                regex = self.options["text"]
            elif self.exact_text is True:
                regex = re.compile(r"\A{}\Z".format(
                    re.escape(self.options["text"])))
            else:
                regex = toregex(self.options["text"])

            text = normalize_text(node.all_text if visible ==
                                  "all" else node.visible_text)

            if not regex.search(text):
                return False

        if isinstance(self.exact_text, (bytes_, str_)):
            regex = re.compile(r"\A{}\Z".format(re.escape(self.exact_text)))

            text = normalize_text(node.all_text if visible ==
                                  "all" else node.visible_text)

            if not regex.search(text):
                return False

        if visible == "visible":
            if not node.visible:
                return False
        elif visible == "hidden":
            if node.visible:
                return False

        for name, query_filter in iter(self._query_filters.items()):
            if name in self.filter_options:
                if not query_filter.matches(node, self.filter_options[name]):
                    return False
            elif query_filter.has_default:
                if not query_filter.matches(node, query_filter.default):
                    return False

        if self.options["filter"] and not self.options["filter"](node):
            return False

        return True
Example #2
0
    def __init__(self, expected_title, exact=False, wait=None):
        self.expected_title = (expected_title if isregex(expected_title) else
                               normalize_text(expected_title))
        self.actual_title = None
        self.search_regexp = toregex(expected_title, exact=exact)

        self.options = {"wait": wait}
Example #3
0
    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
Example #4
0
    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
Example #5
0
    def resolves_for(self, node):
        """
        Resolves this query relative to the given node.

        Args:
            node (node.Document): The node to be evaluated.

        Returns:
            bool: Whether the given node matches this query.
        """

        self.actual_title = normalize_text(node.title)
        return bool(self.search_regexp.search(self.actual_title))
Example #6
0
    def resolve_for(self, node):
        """
        Resolves this query relative to the given node.

        Args:
            node (node.Base): The node to be evaluated.

        Returns:
            int: The number of matches found.
        """

        self.node = node
        self.actual_text = normalize_text(node.visible_text if self.query_type
                                          == "visible" else node.all_text)
        self.count = len(re.findall(self.search_regexp, self.actual_text))

        return self.count
Example #7
0
    def matches_filters(self, node):
        """
        Returns whether the given node matches all filters.

        Args:
            node (Element): The node to evaluate.

        Returns:
            bool: Whether the given node matches.
        """

        visible = self.visible

        if self.options["text"]:
            regex = toregex(self.options["text"])
            text = normalize_text(node.all_text if visible ==
                                  "all" else node.visible_text)

            if not regex.search(text):
                return False

        if visible == "visible":
            if not node.visible:
                return False
        elif visible == "hidden":
            if node.visible:
                return False

        for name, query_filter in iter(self._query_filters.items()):
            if name in self.filter_options:
                if not query_filter.matches(node, self.filter_options[name]):
                    return False
            elif query_filter.has_default:
                if not query_filter.matches(node, query_filter.default):
                    return False
        return True
Example #8
0
 def __init__(self, expected_title):
     self.expected_title = (expected_title if isregex(expected_title) else
                            normalize_text(expected_title))
     self.actual_title = None
     self.search_regexp = toregex(expected_title)