31
31
class HtmlAssertMixin :
32
32
"""Custom assertions for HTML containing strings."""
33
33
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 .
37
37
"""
38
38
39
39
self .assertIsValidHtmlDocument (value )
@@ -48,7 +48,19 @@ def assertHtmlElementTextContent(self, value, tag_name, expected_text, trim_newl
48
48
tag_close = '' .join (['</' , tag_name , '>' ])
49
49
tag_close_index = value .index (tag_close , tag_open_index_after )
50
50
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 )
52
64
if trim_newlines :
53
65
actual_text = actual_text .strip ('\n ' )
54
66
self .assertEqual (actual_text , expected_text )
@@ -57,15 +69,16 @@ def assertIsValidHtmlDocument(self, value):
57
69
"""Check that the input string contains a valid HTML document.
58
70
"""
59
71
60
- assert isinstance (value , type (u"" ))
72
+ assert isinstance (value , type (u"" )), "input string was not utf8"
61
73
62
74
error = None
63
75
try :
64
- assert value .startswith ("<!DOCTYPE html" )
76
+ has_doctype = value .startswith ("<!DOCTYPE html" )
77
+ assert has_doctype , "no valid document opener"
65
78
end_html_tag_idx = value .rfind ('</html>' )
66
79
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"
68
81
except Exception as exc :
69
82
error = exc
70
83
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