diff --git a/.gitignore b/.gitignore index a27713e..0d1e73d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,pycharm,macos # Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode,pycharm,macos -appium_logs +logs ### macOS ### # General .DS_Store diff --git a/tests/selenium_tests.py b/tests/selenium_tests.py index bad63d3..821d69f 100644 --- a/tests/selenium_tests.py +++ b/tests/selenium_tests.py @@ -31,7 +31,7 @@ def test_template_matching(self, selenium_driver: TestUIDriver): "https://github.com/testdevlab/Py-TestUI#image-recognition" ) selenium_driver.find_image_match( - "resources/comp.png", 0.9, True, image_match="image.png" + "resources/comp.png", 0.9, True, image_match="./logs/image.png" ) selenium_driver.raise_errors() diff --git a/testui/support/appium_driver.py b/testui/support/appium_driver.py index 371dcef..404944d 100644 --- a/testui/support/appium_driver.py +++ b/testui/support/appium_driver.py @@ -1,7 +1,9 @@ import atexit +import datetime import os import subprocess import threading +import time from pathlib import Path from time import sleep @@ -411,26 +413,20 @@ def __local_run(url, desired_caps, use_port, udid, log_file): if udid is None: desired_caps = __set_android_device(desired_caps, device) logger.log(f'setting device for automation: {desired_caps["udid"]}') - root_dir = ( - os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) - + "/" - ) - Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True) + log_dir = os.path.join("./logs", "appium_logs") + Path(log_dir).mkdir(parents=True, exist_ok=True) file_path: str if log_file == "appium-stdout.log": - file = f"appium_logs/testui-{udid}-" + log_file + file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file) else: - file = f"appium_logs/{log_file}" - with open(root_dir + file, "wb") as out: + file_path = os.path.join(log_dir, log_file) + with open(file_path, "wb") as out: process = subprocess.Popen( ["appium", "-p", port.__str__(), "-bp", bport.__str__()], stdout=out, stderr=subprocess.STDOUT, ) atexit.register(process.kill) - file_path = root_dir + file while True: sleep(0.5) out = open(file_path) @@ -465,26 +461,20 @@ def __local_run_ios(url, desired_caps, use_port, udid, log_file): os.getenv("PYTEST_XDIST_WORKER").split("w")[1] ) logger.log(f"running: appium -p {port.__str__()}") - root_dir = ( - os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) - + "/" - ) - Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True) + log_dir = os.path.join("./logs", "appium_logs") + Path(log_dir).mkdir(parents=True, exist_ok=True) file_path: str if log_file == "appium-stdout.log": - file = f"appium_logs/testui-{udid}-" + log_file + file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file) else: - file = f"appium_logs/{log_file}" - with open(root_dir + file, "wb") as out: + file_path = os.path.join(log_dir, log_file) + with open(file_path, "wb") as out: process = subprocess.Popen( ["appium", "-p", port.__str__()], stdout=out, stderr=subprocess.STDOUT, ) atexit.register(process.kill) - file_path = root_dir + file if udid is None: desired_caps = __set_ios_device(desired_caps, device) while True: diff --git a/testui/support/helpers.py b/testui/support/helpers.py index d649e8b..7f3c102 100644 --- a/testui/support/helpers.py +++ b/testui/support/helpers.py @@ -10,8 +10,6 @@ def error_with_traceback(exception): ) line: str for line in traceback.extract_stack().format(): - if line.__contains__(root_dir) and not line.__contains__( - "traceback.extract_stack()" - ): + if root_dir in line and not "traceback.extract_stack()" in line: exception += logger.bcolors.FAIL + line + logger.bcolors.ENDC return exception diff --git a/testui/support/logger.py b/testui/support/logger.py index cc18fce..a27f159 100644 --- a/testui/support/logger.py +++ b/testui/support/logger.py @@ -2,6 +2,7 @@ from datetime import datetime from pathlib import Path +LOG_DIR = "./logs" class bcolors: """ @@ -145,19 +146,13 @@ def __file_log(log_file="stdout.log"): :param log_file: String :return: String """ - root_dir = ( - os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) - + "/" - ) - Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True) + Path(LOG_DIR).mkdir(parents=True, exist_ok=True) file_name: str if log_file == "stdout.log": - file_name = f"appium_logs/TEST_UI-{log_file}" + file_name = os.path.join(LOG_DIR, f"TEST_UI-{log_file}") else: - file_name = f"appium_logs/{log_file}" - return root_dir + file_name + file_name = os.path.join(LOG_DIR, log_file) + return file_name def __file_tests(log_file="report_cases.txt"): @@ -166,11 +161,5 @@ def __file_tests(log_file="report_cases.txt"): :param log_file: String :return: String """ - root_dir = ( - os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) - + "/" - ) - Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True) - return root_dir + log_file + Path(LOG_DIR).mkdir(parents=True, exist_ok=True) + return os.path.join(LOG_DIR, log_file) diff --git a/testui/support/parallel.py b/testui/support/parallel.py index dd01d01..0b0f0b7 100644 --- a/testui/support/parallel.py +++ b/testui/support/parallel.py @@ -89,30 +89,38 @@ def remove_logs(): """ Will remove the logs from the previous execution. """ - root_dir = os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) + log_dir = "./logs" i = 0 try: - for filename in os.listdir(root_dir + "/report_screenshots"): + for filename in os.listdir(os.path.join(log_dir, "report_screenshots")): file_path = os.path.join( - root_dir + "/report_screenshots/", filename + os.path.join(log_dir, "report_screenshots"), filename ) os.remove(file_path) if i == 0: logger.log("Cleaning report_screenshots folder...") i += 1 - except FileNotFoundError: + except (IsADirectoryError, PermissionError, FileNotFoundError): pass i = 0 try: - for filename in os.listdir(root_dir + "/appium_logs"): - file_path = os.path.join(root_dir + "/appium_logs/", filename) + for filename in os.listdir(os.path.join(log_dir, "appium_logs")): + file_path = os.path.join(log_dir, filename) os.remove(file_path) if i == 0: logger.log("Cleaning appium_logs folder...") i += 1 - except FileNotFoundError: + except (IsADirectoryError, PermissionError, FileNotFoundError): + pass + i = 0 + try: + for filename in os.listdir(log_dir): + file_path = os.path.join(log_dir, filename) + os.remove(file_path) + if i == 0: + logger.log("Cleaning logs folder...") + i += 1 + except (IsADirectoryError, PermissionError, FileNotFoundError): pass diff --git a/testui/support/testui_driver.py b/testui/support/testui_driver.py index 1c0adb7..a90b064 100644 --- a/testui/support/testui_driver.py +++ b/testui/support/testui_driver.py @@ -270,7 +270,7 @@ def get_dimensions(self): image_name = f"{self.device_udid}{current_time}.png" path_s = self.save_screenshot(image_name) dimensions = ImageRecognition( - original=path_s + original=path_s, path="" ).image_original_size() logger.log(f"Deleting screenshot: {path_s}") self.__delete_screenshot(path_s) @@ -297,21 +297,19 @@ def save_screenshot(self, image_name=""): """ config = self.__configuration - root_dir = config.screenshot_path + log_dir = config.screenshot_path if not config.screenshot_path: - root_dir = path.dirname( - path.dirname(path.dirname(path.abspath(__file__))) - ) - root_dir = path.join(root_dir, "report_screenshots") + log_dir = "./logs" + log_dir = path.join(log_dir, "report_screenshots") - Path(root_dir).mkdir(parents=True, exist_ok=True) + Path(log_dir).mkdir(parents=True, exist_ok=True) current_time = datetime.now().strftime("%Y-%m-%d%H%M%S") if not image_name: image_name = f"ERROR-{self.device_name}-{current_time}.png" - final_path = path.join(root_dir, image_name) + final_path = path.join(log_dir, image_name) self.get_driver().save_screenshot(final_path) @@ -328,9 +326,6 @@ def __delete_screenshot(cls, image_name): :param image_name: :return: """ - root_dir = os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) os.remove(image_name) def get_driver(self) -> WebDriver: @@ -436,9 +431,9 @@ def stop_recording_screen(self, file_name="testui-video.mp4"): """ file = self.get_driver().stop_recording_screen() decoded_string = base64.b64decode(file) - root_dir = self.configuration.screenshot_path - logger.log(f"Recording stopped in {os.path.join(root_dir, file_name)}") - with open(os.path.join(root_dir, file_name), "wb") as wfile: + log_dir = self.configuration.screenshot_path + logger.log(f"Recording stopped in {os.path.join(log_dir, file_name)}") + with open(os.path.join(log_dir, file_name), "wb") as wfile: wfile.write(decoded_string) def stop_recording_and_compare( @@ -464,18 +459,16 @@ def stop_recording_and_compare( current_time = now.strftime("%Y-%m-%d%H%M%S") video_name = f"{self.device_udid}{current_time}.mp4" self.stop_recording_screen(video_name) - root_dir = os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) + log_dir = "./logs" if self.__configuration.screenshot_path != "": - root_dir = self.__configuration.screenshot_path + log_dir = self.__configuration.screenshot_path found = ImageRecognition( video_name, comparison, threshold, device_name=self.device_name, - path=root_dir + path=log_dir ).compare_video(keep_image_as, frame_rate_reduction=fps_reduction) - os.remove(os.path.join(root_dir, video_name)) + os.remove(os.path.join(log_dir, video_name)) if not found and not not_found: if assertion: raise Exception( diff --git a/testui/support/testui_images.py b/testui/support/testui_images.py index ef5a561..de4e415 100644 --- a/testui/support/testui_images.py +++ b/testui/support/testui_images.py @@ -370,16 +370,12 @@ class ImageRecognition: """ def __init__( - self, original: str, comparison="", threshold=0.9, device_name="Device", path="" + self, original: str, comparison="", threshold=0.9, device_name="Device", path="./logs" ): self.__original = original self.__comparison = comparison self.__threshold = threshold self.__device_name = device_name - if path == "": - path = os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) self.__path = path def compare(self, image_match="", max_scale=2.0, min_scale=0.3):