Skip to content

Commit 51ddb9f

Browse files
committed
bring over improvements to hmtlsupp from another branch
1 parent 8fc5607 commit 51ddb9f

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

tests/support/htmlsupp.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
class HtmlAssertMixin:
3232
"""Custom assertions for HTML containing strings."""
3333

34-
def assertHtmlElementTextContent(self, value, tag_name, expected_text, trim_newlines=True):
35-
"""Locate the first occurrence of a tag within an HTML input string
36-
and assert that any contained string equals what was supplied.
34+
def assertHtmlElement(self, value, tag_name):
35+
"""Check that an occurrence of the specifid tag within an HTML input
36+
string can be found. Returns the textual content of the first match.
3737
"""
3838

3939
self.assertIsValidHtmlDocument(value)
@@ -48,7 +48,19 @@ def assertHtmlElementTextContent(self, value, tag_name, expected_text, trim_newl
4848
tag_close = ''.join(['</', tag_name, '>'])
4949
tag_close_index = value.index(tag_close, tag_open_index_after)
5050

51-
actual_text = value[tag_open_index_after:tag_close_index]
51+
return value[tag_open_index_after:tag_close_index]
52+
53+
def assertHtmlElementTextContent(self, value, tag_name, expected_text, trim_newlines=True):
54+
"""Check there is an occurrence of a tag within an HTML input string
55+
and check the text it encloses equals exactly the expecatation.
56+
"""
57+
58+
self.assertIsValidHtmlDocument(value)
59+
60+
# TODO: this is a definitively stop-gap way of finding a tag within the HTML
61+
# and is used purely to keep this initial change to a reasonable size.
62+
63+
actual_text = self.assertHtmlElement(value, tag_name)
5264
if trim_newlines:
5365
actual_text = actual_text.strip('\n')
5466
self.assertEqual(actual_text, expected_text)
@@ -57,15 +69,16 @@ def assertIsValidHtmlDocument(self, value):
5769
"""Check that the input string contains a valid HTML document.
5870
"""
5971

60-
assert isinstance(value, type(u""))
72+
assert isinstance(value, type(u"")), "input string was not utf8"
6173

6274
error = None
6375
try:
64-
assert value.startswith("<!DOCTYPE html")
76+
has_doctype = value.startswith("<!DOCTYPE html")
77+
assert has_doctype, "no valid document opener"
6578
end_html_tag_idx = value.rfind('</html>')
6679
maybe_document_end = value[end_html_tag_idx:].rstrip()
67-
self.assertEqual(maybe_document_end, '</html>')
80+
assert maybe_document_end == '</html>', "no valid document closer"
6881
except Exception as exc:
6982
error = exc
7083
if error:
71-
raise AssertionError("failed to verify input string as HTML: %s", str(exc))
84+
raise AssertionError("failed to verify input string as HTML: %s", str(error))

0 commit comments

Comments
 (0)