Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions demo/scroll.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Black Viewport Demo</title>
</head>
<body>
<div id="scrollable" style="height: 600px; background-color: blue; overflow-y: scroll">
<div style="height: 2000px; position:relative">
<div id="underscroll" style="height: 100px; width:100px; background-color: green; bottom:0px; position:absolute; bottom: 0px;"></div>
</div>
</div>
</body>
</html>
15 changes: 15 additions & 0 deletions demo/viewport.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Black Viewport Demo</title>
</head>
<body>
<div style="height: 2000px; width: 100%; background-color: brown; position:relative;">

<div id="inside" style="height: 100px; width:100px; background-color: red; position:absolute; top:0px;"></div>
<div id="outside" style="height: 100px; width:100px; background-color: blue; position:absolute; bottom:0px;"></div>

</div>
</body>
</html>
20 changes: 17 additions & 3 deletions needle/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ def set_viewport_size(cls, width, height):

cls.driver.set_window_size(width + delta, height)

def assertScreenshot(self, element_or_selector, file, threshold=0):
def assertScreenshot(self, element_or_selector, file, ignore=[], threshold=0):
"""assert-style variant of compareScreenshot context manager

compareScreenshot() can be considerably more efficient for recording baselines by avoiding the need
to load pages before checking whether we're actually going to save them. This function allows you
to continue using normal unittest-style assertions if you don't need the efficiency benefits
"""

with self.compareScreenshot(element_or_selector, file, threshold=threshold):
with self.compareScreenshot(element_or_selector, file, ignore=ignore, threshold=threshold):
pass

@contextmanager
def compareScreenshot(self, element_or_selector, file, threshold=0):
def compareScreenshot(self, element_or_selector, file, ignore = [], threshold=0):
"""
Assert that a screenshot of an element is the same as a screenshot on disk,
within a given threshold.
Expand All @@ -149,12 +149,26 @@ def compareScreenshot(self, element_or_selector, file, threshold=0):
If a string, then assumed to be the filename for the screenshot,
which will be appended with ``.png``. Otherwise assumed to be
a file object for the baseline image.
:param ignore:
Either a CSS selector as a string or a
:py:class:`~needle.driver.NeedleWebElement` object that represents
the element to be ignored in the screenshot comparison (element will disappear from image).
:param threshold:
The threshold for triggering a test failure.
"""

yield # To allow using this method as a context manager

# Hiding element to be ignored in screenshot comparisons
if len(ignore) > 0:
for element_to_be_ignored in ignore:
if not isinstance(element_to_be_ignored, NeedleWebElement):
element = self.driver.find_element_by_css_selector(element_to_be_ignored)
else:
element = element_to_be_ignored
self.driver.execute_script('arguments[0].style.visibility="hidden";', element)


if not isinstance(element_or_selector, NeedleWebElement):
element = self.driver.find_element_by_css_selector(element_or_selector)
else:
Expand Down