"Zoom in" on rendered HTML that the test client returns

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# python
import re
from cStringIO import StringIO
# django
from django.test import TestCase


class BaseTestCase(TestCase):

    def _zoom_html(self, html, css_selector):

        try:
            from lxml.html import parse
            from lxml import etree
            from lxml.cssselect import CSSSelector
        except ImportError:
            return html

        parser = etree.HTMLParser()
        tree = etree.parse(StringIO(html), parser)
        page = tree.getroot()

        if isinstance(css_selector, basestring):
            selector = CSSSelector(css_selector)
        else:
            raise ValueError("css_select must be a string")

        html_chunks = []
        for part in selector(page):
            html_chunks.append(etree.tostring(part, pretty_print=True))

        return ''.join(html_chunks)

    def _strip_html(self, html):
        return re.sub('</?\w.*?>', '', html)

More like this

  1. filter for extracting a number of paragraphs from any HTML code by rafadev 2 years, 10 months ago
  2. Effective content caching for mass-load site using redirect feature by nnseva 2 years, 9 months ago
  3. Dynamically add css-classes to formfields by Lacour 2 years, 9 months ago
  4. Load response.content in browser (for debugging) by tin_nqn 1 year, 11 months ago
  5. Another pygments for ReST by limodou 7 years, 1 month ago

Comments

(Forgotten your password?)