3333
3434from tests .support .suppconst import TEST_BASE
3535
36+ HTML_TAG = '<html>'
37+ MARKER_CONTENT_BEGIN = '<!-- Begin UI container -->'
38+ MARKER_CONTENT_END = '<!-- End UI container -->'
3639TEST_SNAPSHOTS_DIR = os .path .join (TEST_BASE , "snapshots" )
3740
3841try :
4245 raise
4346
4447
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+
4570def _delimited_lines (value ):
4671 """Break a value by newlines into lines suitable for diffing."""
4772
@@ -73,14 +98,14 @@ def _force_refresh_snapshots():
7398
7499
75100class SnapshotAssertMixin :
76- """Custom assertions alowing the use of snapshots within tests."""
101+ """Custom assertions allowing the use of snapshots within tests."""
77102
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.
82106
83- assert extension is not None
107+ In the case a snapshot does not exist it is saved on first invocation.
108+ """
84109
85110 file_name = '' .join ([self ._testMethodName , "." , extension ])
86111 file_path = os .path .join (TEST_SNAPSHOTS_DIR , file_name )
@@ -106,3 +131,21 @@ def assertSnapshot(self, actual_content, extension=None):
106131 )
107132 raise AssertionError (
108133 "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