Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple Python script used for comparing two folders containing screenshots after an Espresso/Spoon run. One folder contains your base screenshots the other folder your newly screenshots. Use this as your last regression resort to see if Espresso overlooked something.
import glob
import os
import sys
from subprocess import Popen, PIPE
# Script that will compare screenshots to a set of previous screenshots
# first arg: full path to base screenshots
# second arg: full path to spoon output dir
# Presumes you have `ImageMagick` compare tool on your path
# Exit codes
# 1: Supplied arguments are not directories
# 2: Folders have different amounts of files (for comparison we ask them to be the same)
old_folder_path = "old/" # sys.argv[1]
spoon_path = "new/" # sys.argv[2]
# Tolerance allowance between screenshots
# Folder where different images will be stored
# { "testClass$$methodName" : "fileName" }
def get_screenshot_dictionary(path):
local_dict = dict()
for pngFile in glob.iglob(os.path.join(path, "*", "*", "*.png")):
class_name_method_name_file_name = [path_piece for path_piece in pngFile.split("/")[-3:]]
key = "%s$$%s" % (class_name_method_name_file_name[0], class_name_method_name_file_name[1])
local_dict[key] = class_name_method_name_file_name[2]
return local_dict
def check_if_argument_is_directory():
return os.path.isdir(old_folder_path) and os.path.isdir(spoon_path)
def has_same_amount_files(old_dict, new_dict):
return len(old_dict) == len(new_dict)
def get_file_name_for(path, dic, class_name_with_method_name):
class_name, method_name = class_name_with_method_name.split("$$")
abs_path = '/'.join([path, class_name, method_name, dic[class_name_with_method_name]])
full_path = "%s/%s" % (os.path.dirname(os.path.abspath(__file__)), abs_path)
return full_path
def do_image_diffing(old_dict, new_dict):
for classNameWithMethodName, fileName in old_dict.items():
if classNameWithMethodName in new_dict:
old_file = get_file_name_for(old_folder_path, old_dict, classNameWithMethodName)
new_file = get_file_name_for(spoon_path, new_dict, classNameWithMethodName)
diff_file = "%s%s" % (DIFF_SAVE_IMG_FOLDER, classNameWithMethodName)
if is_a_file(old_file) and is_a_file(new_file):
diff = Popen(
'-highlight-color', 'red',
'-compose', 'src',
'-channel', 'RGBA',
'-metric', 'PSNR',
'-fuzz', '100%',
old_file, new_file, diff_file], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()[1]
print diff
if diff == "inf\n":
# <editor-fold desc="Helper functions">
def is_a_file(full_file_path):
return os.path.isfile(full_file_path)
def remove_file(full_file_path):
def make_folder(with_name):
if not os.path.exists(with_name):
# </editor-fold>
if __name__ == "__main__":
if check_if_argument_is_directory():
old_screenshot_dict = get_screenshot_dictionary(old_folder_path)
new_screenshot_dict = get_screenshot_dictionary(spoon_path)
if has_same_amount_files(old_screenshot_dict, new_screenshot_dict):
do_image_diffing(old_screenshot_dict, new_screenshot_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment