33
33
34
34
from tests .support .suppconst import TEST_BASE
35
35
36
+ HTML_TAG = '<html>'
37
+ MARKER_CONTENT_BEGIN = '<!-- Begin UI container -->'
38
+ MARKER_CONTENT_END = '<!-- End UI container -->'
36
39
TEST_SNAPSHOTS_DIR = os .path .join (TEST_BASE , "snapshots" )
37
40
38
41
try :
42
45
raise
43
46
44
47
48
+ def _html_content_only (value ):
49
+ """For a given HTML input extract only the portion that corresponds to the
50
+ page content. This is somewhat convoluted due to having to work around an
51
+ inability to move the comment markers to enclose only the content.
52
+ """
53
+
54
+ assert value .find (HTML_TAG ) > - 1 , "value does not appear to be HTML"
55
+ content_start_index = value .find (MARKER_CONTENT_BEGIN )
56
+ assert content_start_index > - 1 , "unable to locate beginning of content"
57
+ # set the index after the content marker
58
+ content_start_index += len (MARKER_CONTENT_BEGIN )
59
+ # we now need to remove the container div inside it ..first find it
60
+ content_start_inner_div = value .find ('<div' , content_start_index )
61
+ # reset the content start to exclude up the end of the container div
62
+ content_start_index = value .find ('>' , content_start_inner_div ) + 1
63
+
64
+ content_end_index = value .find (MARKER_CONTENT_END )
65
+ assert content_end_index > - 1 , "unable to locate end of content"
66
+
67
+ return value [content_start_index :content_end_index ].strip ()
68
+
69
+
45
70
def _delimited_lines (value ):
46
71
"""Break a value by newlines into lines suitable for diffing."""
47
72
@@ -73,14 +98,14 @@ def _force_refresh_snapshots():
73
98
74
99
75
100
class SnapshotAssertMixin :
76
- """Custom assertions alowing the use of snapshots within tests."""
101
+ """Custom assertions allowing the use of snapshots within tests."""
77
102
78
- def assertSnapshot (self , actual_content , extension = None ):
79
- """Load a snapshot corresponding to the named test and check that its
80
- content, which is th expectatoin, matches what was actually given. In
81
- the case a snapshot does not exist it is saved on first invocation."""
103
+ def _snapshotsupp_compare_snapshot (self , extension , actual_content ):
104
+ """Helper which actually loads the snapshot from a file on disk and
105
+ does the comparison.
82
106
83
- assert extension is not None
107
+ In the case a snapshot does not exist it is saved on first invocation.
108
+ """
84
109
85
110
file_name = '' .join ([self ._testMethodName , "." , extension ])
86
111
file_path = os .path .join (TEST_SNAPSHOTS_DIR , file_name )
@@ -106,3 +131,21 @@ def assertSnapshot(self, actual_content, extension=None):
106
131
)
107
132
raise AssertionError (
108
133
"content did not match snapshot\n \n %s" % ('' .join (udiff ),))
134
+
135
+ def assertSnapshot (self , actual_content , extension = None ):
136
+ """Load a snapshot corresponding to the named test and check that what
137
+ it contains, which is the expectation, matches what was actually given.
138
+ """
139
+
140
+ assert extension is not None
141
+
142
+ self ._snapshotsupp_compare_snapshot (extension , actual_content )
143
+
144
+ def assertSnapshotOfHtmlContent (self , actual_content ):
145
+ """Load a snapshot corresponding to the named test and check that what
146
+ it contains, which is the expectation, matches against the portion of
147
+ what was actually given that corresponds to the output HTML content.
148
+ """
149
+
150
+ actual_content = _html_content_only (actual_content )
151
+ self ._snapshotsupp_compare_snapshot ('html' , actual_content )
0 commit comments