| 1 | """ |
|---|
| 2 | Ported to Python 3. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | import re |
|---|
| 6 | |
|---|
| 7 | unknown_rwcap = u"lafs://from_the_future_rw_\u263A".encode('utf-8') |
|---|
| 8 | unknown_rocap = u"ro.lafs://readonly_from_the_future_ro_\u263A".encode('utf-8') |
|---|
| 9 | unknown_immcap = u"imm.lafs://immutable_from_the_future_imm_\u263A".encode('utf-8') |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | def assert_soup_has_favicon(testcase, soup): |
|---|
| 13 | """ |
|---|
| 14 | Using a ``TestCase`` object ``testcase``, assert that the passed in |
|---|
| 15 | ``BeautifulSoup`` object ``soup`` contains the tahoe favicon link. |
|---|
| 16 | """ |
|---|
| 17 | links = soup.find_all(u'link', rel=u'shortcut icon') |
|---|
| 18 | testcase.assertTrue( |
|---|
| 19 | any(t[u'href'] == u'/icon.png' for t in links), soup) |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | def assert_soup_has_tag_with_attributes(testcase, soup, tag_name, attrs): |
|---|
| 23 | """ |
|---|
| 24 | Using a ``TestCase`` object ``testcase``, assert that the passed |
|---|
| 25 | in ``BeatufulSoup`` object ``soup`` contains a tag ``tag_name`` |
|---|
| 26 | (unicode) which has all the attributes in ``attrs`` (dict). |
|---|
| 27 | """ |
|---|
| 28 | tags = soup.find_all(tag_name) |
|---|
| 29 | for tag in tags: |
|---|
| 30 | if all(v in tag.attrs.get(k, []) for k, v in attrs.items()): |
|---|
| 31 | # we found every attr in this tag; done |
|---|
| 32 | return tag |
|---|
| 33 | testcase.fail( |
|---|
| 34 | u"No <{}> tags contain attributes: {}".format(tag_name, attrs) |
|---|
| 35 | ) |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | def assert_soup_has_tag_with_attributes_and_content(testcase, soup, tag_name, content, attrs): |
|---|
| 39 | """ |
|---|
| 40 | Using a ``TestCase`` object ``testcase``, assert that the passed |
|---|
| 41 | in ``BeatufulSoup`` object ``soup`` contains a tag ``tag_name`` |
|---|
| 42 | (unicode) which has all the attributes in ``attrs`` (dict) and |
|---|
| 43 | contains the string ``content`` (unicode). |
|---|
| 44 | """ |
|---|
| 45 | assert_soup_has_tag_with_attributes(testcase, soup, tag_name, attrs) |
|---|
| 46 | assert_soup_has_tag_with_content(testcase, soup, tag_name, content) |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | def _normalized_contents(tag): |
|---|
| 50 | """ |
|---|
| 51 | :returns: all the text contents of the tag with whitespace |
|---|
| 52 | normalized: all newlines removed and at most one space between |
|---|
| 53 | words. |
|---|
| 54 | """ |
|---|
| 55 | return u" ".join(tag.text.split()) |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | def assert_soup_has_tag_with_content(testcase, soup, tag_name, content): |
|---|
| 59 | """ |
|---|
| 60 | Using a ``TestCase`` object ``testcase``, assert that the passed |
|---|
| 61 | in ``BeatufulSoup`` object ``soup`` contains a tag ``tag_name`` |
|---|
| 62 | (unicode) which contains the string ``content`` (unicode). |
|---|
| 63 | """ |
|---|
| 64 | tags = soup.find_all(tag_name) |
|---|
| 65 | for tag in tags: |
|---|
| 66 | if content in tag.contents: |
|---|
| 67 | return |
|---|
| 68 | |
|---|
| 69 | # make these "fuzzy" options? |
|---|
| 70 | for c in tag.contents: |
|---|
| 71 | if content in c: |
|---|
| 72 | return |
|---|
| 73 | |
|---|
| 74 | if content in _normalized_contents(tag): |
|---|
| 75 | return |
|---|
| 76 | testcase.fail( |
|---|
| 77 | u"No <{}> tag contains the text '{}'".format(tag_name, content) |
|---|
| 78 | ) |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | def assert_soup_has_text(testcase, soup, text): |
|---|
| 82 | """ |
|---|
| 83 | Using a ``TestCase`` object ``testcase``, assert that the passed in |
|---|
| 84 | ``BeautifulSoup`` object ``soup`` contains the passed in ``text`` anywhere |
|---|
| 85 | as a text node. |
|---|
| 86 | """ |
|---|
| 87 | testcase.assertTrue( |
|---|
| 88 | soup.find_all(string=re.compile(re.escape(text))), |
|---|
| 89 | soup) |
|---|