Skip to content

Instantly share code, notes, and snippets.

@timrijckaert
Last active December 19, 2022 11:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timrijckaert/829bd4c7673cff1dd63cd122c7ddfd7e to your computer and use it in GitHub Desktop.
Save timrijckaert/829bd4c7673cff1dd63cd122c7ddfd7e to your computer and use it in GitHub Desktop.
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.
#!/usr/bin/python
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
FUZZINESS_LEVEL = "5%"
# Folder where different images will be stored
DIFF_SAVE_IMG_FOLDER = "dff/"
# { "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):
make_folder(DIFF_SAVE_IMG_FOLDER)
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(
['compare',
'-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":
remove_file(diff_file)
# <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):
os.remove(full_file_path)
def make_folder(with_name):
if not os.path.exists(with_name):
os.makedirs(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)
else:
sys.exit(2)
else:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment