Skip to content

Commit

Permalink
save an image that highlights where the result was different
Browse files Browse the repository at this point in the history
  • Loading branch information
daleharvey committed Jan 31, 2012
1 parent 96c8dc4 commit 70f6f14
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions tests/selenium-screenshots/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import unittest
import os
import ImageDraw

def whoami():
return sys._getframe(1).f_code.co_name
Expand All @@ -25,9 +26,6 @@ def readConfig():
def find(browser, selector):
return browser.find_element_by_css_selector(selector)

def equal(im1, im2):
return ImageChops.difference(im1, im2).getbbox() is None

def wait(browser):
WebDriverWait(browser, 10).until(
lambda driver : driver.find_element_by_id("selenium"))
Expand All @@ -37,15 +35,33 @@ def get_and_wait(browser, url):
wait(browser)

def testImage(browser, name):
pathPass = './passing/%s.png' % name
pathResult = './results/%s.png' % name
# Let chromes scrollbar go away
time.sleep(1)
browser.save_screenshot(pathResult)
if os.path.isfile(pathPass):
passing = Image.open(pathPass)
result = Image.open(pathResult)
return equal(passing, result)
browser.save_screenshot('./results/%s.png' % name)
return compareImage(name)

def compareImage(name):

pathPass = './passing/%s.png' % name
pathResult = './results/%s.png' % name

if not os.path.isfile(pathPass):
return False

passing = Image.open(pathPass)
result = Image.open(pathResult)
difference = ImageChops.difference(result, passing).getbbox()

if difference is None:
return True

# Draw an rectangle to highlight the area in the result
# that is different from the passing image
draw = ImageDraw.Draw(result)
draw.rectangle(difference, outline="red")
del draw

result.save('./results/%s-diff.png' % name, "PNG")
return False

def emptyDir(folder):
Expand Down

0 comments on commit 70f6f14

Please sign in to comment.