Beispiel #1
0
    def notfind_in(self, what, where, flags='', flat=False, escape=False):
        """
        Alternate version of ``notfind`` method that allow to check that text
        not found in another text, not only in current loaded page.

        ``notfind_in`` supports all keywords from original ``notfind`` method.

        If ``what`` found in ``where``, ``notfind_in`` raises
        ``TwillAssertionError``.
        """
        found = False

        if escape:
            what = real_escape(what)

        if flat and what in where:
            found = True
        elif not flat:
            regexp = re.compile(what, _parseFindFlags(flags))
            if regexp.search(where):
                found = True

        if found:
            self.text_to_twill(where)
            raise TwillAssertionError('Match to %r' % what)

        return True
Beispiel #2
0
    def find_in(self, what, where, flags='', flat=False, count=None,
                escape=False):
        """
        Alternate version of ``find`` method that allow to find text in another
        text, not only in current loaded page.

        ``find_in`` supports all keywords from original ``find`` method.

        If ``what`` not found in ``where``, ``find_in`` raises
        ``TwillAssertionError``.
        """
        if escape:
            what = real_escape(what)

        if not flat and not count:
            regexp = re.compile(what, _parseFindFlags(flags))

            if not regexp.search(where):
                self.text_to_twill(where)
                raise TwillAssertionError('No match to %r' % what)

            return True

        real_count = where.count(what)

        if count is not None and count != real_count:
            self.text_to_twill(where)
            raise TwillAssertionError('Matched to %r %d times, not %d ' \
                                      'times.' % (what, real_count, count))
        elif real_count == 0:
            self.text_to_twill(where)
            raise TwillAssertionError('No match to %r' % what)

        return True
Beispiel #3
0
    def notfind(self, what, flags='', flat=False, escape=False):
        """
        Twill used regexp for searching content on web-page. Use ``flat=True``
        to search content on web-page by standart Python ``not what in html``
        expression.

        If this expression was not True (was found on page) it's raises
        ``TwillAssertionError`` as in ``twill.commands.notfind`` method.

        You could escape ``what`` text by standart ``django.utils.html.escape``
        function if call method with ``escape=True``, like::

            self.notfind('Text with "quotes"', escape=True)

        """
        if escape:
            what = real_escape(what)

        if not flat:
            return self._notfind(what, flags)

        html = self.get_browser().get_html()
        if what in html:
            raise TwillAssertionError('Match to %r' % what)

        return True
Beispiel #4
0
    def find(self, what, flags='', flat=False, count=None, escape=False):
        """
        Twill used regexp for searching content on web-page. Use ``flat=True``
        to search content on web-page by standart Python ``what in html``
        expression.

        If this expression was not True (was not found on page) it's raises
        ``TwillAssertionError`` as in ``twill.commands.find`` method.

        Specify ``count`` to test that ``what`` occurs ``count`` times in the
        content of the web-page.

        You could escape ``what`` text by standart ``django.utils.html.escape``
        function if call method with ``escape=True``, like::

            self.find('Text with "quotes"', escape=True)

        """
        if escape:
            what = real_escape(what)

        if not flat and not count:
            return self._find(what, flags)

        html = self.get_browser().get_html()
        real_count = html.count(what)

        if count is not None and count != real_count:
            raise TwillAssertionError('Matched to %r %d times, not %d ' \
                                      'times.' % (what, real_count, count))
        elif real_count == 0:
            raise TwillAssertionError('No match to %r' % what)

        return True
    def find(self, what, flags='', flat=False, count=None, escape=False,
            plain_text=False, collapse_whitespace=True):
        """
        Twill used regexp for searching content on web-page. Use ``flat=True``
        to search content on web-page by standart Python ``what in html``
        expression.

        If this expression was not True (was not found on page) it's raises
        ``TwillAssertionError`` as in ``twill.commands.find`` method.

        Specify ``count`` to test that ``what`` occurs ``count`` times in the
        content of the web-page.

        You could escape ``what`` text by standart ``django.utils.html.escape``
        function if call method with ``escape=True``, like::

            self.find('Text with "quotes"', escape=True)

        Set ``plain_text`` to True to search in text of page disregarding html
        tags (i. e. parsed by html parser).

        """
        if not isinstance(what, basestring):
            what = unicode(what)

        #FIXME: real errors regarding whitespace will be missed.
        what = re.sub('(\r\n)|\r', '\n', what.strip())
        if collapse_whitespace:
            what = re.sub('\n+', '\n', what)
            what = re.sub(' +', ' ', what)

        if escape:
            what = real_escape(what)

        if isinstance(what, str):
            what = what.decode('UTF-8')

        html = self.get_browser().get_html().decode('UTF-8')
        html = re.sub('((\r\n)|\r|\n)+', '\n', html)
        if plain_text:
            soup = BeautifulSoup(html,
                                 convertEntities=BeautifulSoup.XHTML_ENTITIES)
            if not flat and not count:
                return soup.find(text=what)
            page_content = []
            for element in soup.body.recursiveChildGenerator():
                if isinstance(element, unicode):
                    page_content.append(element)
                elif str(element) == '<br />':
                    page_content.append('\n')
            page_content = ''.join(page_content)
            if flat:
                real_count = page_content.count(what)
            else:
                real_count = len(re.findall(what, page_content))
        else:
            if not flat and not count:
                return self._find(what.encode('UTF-8'), flags)
            real_count = html.count(what)

        if count is not None and count != real_count:
            raise TwillAssertionError('Matched to %s %d times, not %d ' \
                                      'times.' % (what, real_count, count))
        elif real_count == 0:
            raise TwillAssertionError('No match to %s' % what)

        return True