Skip to content

Commit 655e1f3

Browse files
committed
Create Screenshot dir during runtime
1 parent fd089a0 commit 655e1f3

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

ImageCompare/imagecompare.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
import uuid
8484
import numpy as np
8585
from pathlib import Path
86-
from robot.libraries.BuiltIn import BuiltIn
86+
from robot.libraries.BuiltIn import BuiltIn, RobotNotRunningError
8787
import re
8888
from concurrent import futures
8989
from robot.api.deco import keyword, library
@@ -113,19 +113,6 @@ def __init__(self, **kwargs):
113113
self.screenshot_format = kwargs.pop('screenshot_format', 'jpg')
114114
if not (self.screenshot_format == 'jpg' or self.screenshot_format == 'png'):
115115
self.screenshot_format == 'jpg'
116-
117-
built_in = BuiltIn()
118-
try:
119-
self.OUTPUT_DIRECTORY = built_in.get_variable_value('${OUTPUT DIR}')
120-
self.reference_run = built_in.get_variable_value('${REFERENCE_RUN}', False)
121-
self.PABOTQUEUEINDEX = built_in.get_variable_value('${PABOTQUEUEINDEX}')
122-
os.makedirs(self.OUTPUT_DIRECTORY/self.SCREENSHOT_DIRECTORY, exist_ok=True)
123-
except:
124-
print("Robot Framework is not running")
125-
self.OUTPUT_DIRECTORY = Path.cwd()
126-
os.makedirs(self.OUTPUT_DIRECTORY / self.SCREENSHOT_DIRECTORY, exist_ok=True)
127-
self.reference_run = False
128-
self.PABOTQUEUEINDEX = None
129116

130117
@keyword
131118
def compare_images(self, reference_image, test_image, **kwargs):
@@ -156,8 +143,9 @@ def compare_images(self, reference_image, test_image, **kwargs):
156143
placeholder_file = kwargs.pop('placeholder_file', None)
157144
mask = kwargs.pop('mask', None)
158145
self.DPI = int(kwargs.pop('DPI', self.DPI))
146+
reference_run = BuiltIn().get_variable_value('${REFERENCE_RUN}', False)
159147

160-
if self.reference_run and (os.path.isfile(test_image) == True):
148+
if reference_run and (os.path.isfile(test_image) == True):
161149
shutil.copyfile(test_image, reference_image)
162150
print('A new reference file was saved: {}'.format(reference_image))
163151
return
@@ -240,21 +228,34 @@ def get_diff_rectangle(self, thresh):
240228

241229
def add_screenshot_to_log(self, image, suffix):
242230
screenshot_name = str(str(uuid.uuid1()) + suffix + '.{}'.format(self.screenshot_format))
243-
244-
if self.PABOTQUEUEINDEX is not None:
245-
rel_screenshot_path = str(self.SCREENSHOT_DIRECTORY / '{}-{}'.format(self.PABOTQUEUEINDEX, screenshot_name))
231+
PABOTQUEUEINDEX = BuiltIn().get_variable_value('${PABOTQUEUEINDEX}', None)
232+
if PABOTQUEUEINDEX is not None:
233+
rel_screenshot_path = str(self.SCREENSHOT_DIRECTORY / '{}-{}'.format(PABOTQUEUEINDEX, screenshot_name))
246234
else:
247235
rel_screenshot_path = str(self.SCREENSHOT_DIRECTORY / screenshot_name)
248-
249-
abs_screenshot_path = str(self.OUTPUT_DIRECTORY/self.SCREENSHOT_DIRECTORY/screenshot_name)
250-
236+
abs_screenshot_path = str(self.log_dir/self.SCREENSHOT_DIRECTORY/screenshot_name)
237+
self._create_directory(abs_screenshot_path)
251238
if self.screenshot_format == 'jpg':
252239
cv2.imwrite(abs_screenshot_path, image, [int(cv2.IMWRITE_JPEG_QUALITY), 70])
253240
else:
254241
cv2.imwrite(abs_screenshot_path, image)
255-
256242
print("*HTML* "+ "<a href='" + rel_screenshot_path + "' target='_blank'><img src='" + rel_screenshot_path + "' style='width:50%; height: auto;'/></a>")
257243

244+
def _create_directory(self, path):
245+
target_dir = os.path.dirname(path)
246+
if not os.path.exists(target_dir):
247+
os.makedirs(target_dir)
248+
249+
@property
250+
def log_dir(self):
251+
try:
252+
logfile = BuiltIn().get_variable_value("${LOG FILE}")
253+
if logfile == "NONE":
254+
return BuiltIn().get_variable_value("${OUTPUTDIR}")
255+
return os.path.dirname(logfile)
256+
except RobotNotRunningError:
257+
return os.getcwd()
258+
258259
def overlay_two_images(self, image, overlay, ignore_color=[255,255,255]):
259260
ignore_color = np.asarray(ignore_color)
260261
mask = ~(overlay==ignore_color).all(-1)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "robotframework-imagecompare"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
description = "A Robot Framework Library for image comparisons"
55
authors = ["Many Kasiriha <[email protected]>"]
66
license = "Apache 2.0"

0 commit comments

Comments
 (0)