From fdeb1f6497ac4c086257f9c671101daf8a8f0b80 Mon Sep 17 00:00:00 2001 From: Alvaro Laserna Date: Fri, 28 Jul 2023 17:10:57 +0200 Subject: [PATCH 1/2] adding support for appium 2.0 --- tests/appium_tests.py | 6 +++-- testui/support/appium_driver.py | 43 ++++++++++++++++++++++----------- testui/support/testui_driver.py | 8 ++++-- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/tests/appium_tests.py b/tests/appium_tests.py index 5cbf002..8f43ca7 100644 --- a/tests/appium_tests.py +++ b/tests/appium_tests.py @@ -15,8 +15,10 @@ def selenium_driver(self): .set_logger() .set_chrome_driver() .set_soft_assert(True) + # .set_appium_url("http://localhost:4723") .set_appium_driver() ) + driver.configuration.save_full_stacktrace yield driver driver.quit() @@ -29,9 +31,9 @@ def test_screenshot_methods(self, selenium_driver: TestUIDriver): ) selenium_driver.start_recording_screen() time.sleep(1) - selenium_driver.stop_recording_and_compare("./resources/comp.png", fps_reduction=30, keep_image_as="v_image.png") + selenium_driver.stop_recording_and_compare("./resources/comp.png", fps_reduction=30, keep_image_as="./logs/v-image.png") 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.click_by_image("./resources/comp.png") selenium_driver.raise_errors() diff --git a/testui/support/appium_driver.py b/testui/support/appium_driver.py index 404944d..3cd43ec 100644 --- a/testui/support/appium_driver.py +++ b/testui/support/appium_driver.py @@ -395,6 +395,7 @@ def start_selenium_driver( def __local_run(url, desired_caps, use_port, udid, log_file): + appium_version_2 = False if url is None: port = use_port bport = use_port + 1 @@ -409,7 +410,7 @@ def __local_run(url, desired_caps, use_port, udid, log_file): os.getenv("PYTEST_XDIST_WORKER").split("w")[1] ) bport += int(os.getenv("PYTEST_XDIST_WORKER").split("w")[1]) * 2 - logger.log(f"running: appium -p {port.__str__()} -bp {bport.__str__()}") + logger.log(f"running: appium -p {port.__str__()}") if udid is None: desired_caps = __set_android_device(desired_caps, device) logger.log(f'setting device for automation: {desired_caps["udid"]}') @@ -420,9 +421,16 @@ def __local_run(url, desired_caps, use_port, udid, log_file): file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file) else: file_path = os.path.join(log_dir, log_file) + # Check Appium Version + command = ["appium", "-p", port.__str__(), "-bp", bport.__str__()] + result = subprocess.run(["appium", "-v"], stdout=subprocess.PIPE).stdout + if result.decode('utf-8').startswith("2."): + # for Appium version > 2.0.0 + appium_version_2 = True + command = ["appium", "-p", port.__str__()] with open(file_path, "wb") as out: process = subprocess.Popen( - ["appium", "-p", port.__str__(), "-bp", bport.__str__()], + command, stdout=out, stderr=subprocess.STDOUT, ) @@ -437,17 +445,18 @@ def __local_run(url, desired_caps, use_port, udid, log_file): out.close() break out.close() - return ( - f"http://localhost:{port.__str__()}/wd/hub", - desired_caps, - process, - file_path, - ) - return url, desired_caps, None + if appium_version_2: + url = f"http://localhost:{port.__str__()}" + else: + url = f"http://localhost:{port.__str__()}/wd/hub" + return url, desired_caps, process, file_path + + return url, desired_caps, None, None def __local_run_ios(url, desired_caps, use_port, udid, log_file): process = None + appium_version_2 = False if url is None: port = use_port + 100 device = 0 @@ -468,6 +477,11 @@ def __local_run_ios(url, desired_caps, use_port, udid, log_file): file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file) else: file_path = os.path.join(log_dir, log_file) + # Check Appium Version + result = subprocess.run(["appium", "-v"], stdout=subprocess.PIPE).stdout + if result.decode('utf-8').startswith("2."): + # for Appium version > 2.0.0 + appium_version_2 = True with open(file_path, "wb") as out: process = subprocess.Popen( ["appium", "-p", port.__str__()], @@ -487,11 +501,12 @@ def __local_run_ios(url, desired_caps, use_port, udid, log_file): out.close() break out.close() - return ( - f"http://localhost:{port.__str__()}/wd/hub", - desired_caps, - file_path, - ) + if appium_version_2: + url = f"http://localhost:{port.__str__()}" + else: + url = f"http://localhost:{port.__str__()}/wd/hub" + return url, desired_caps, file_path + return url, desired_caps, process diff --git a/testui/support/testui_driver.py b/testui/support/testui_driver.py index a90b064..5e50e3b 100644 --- a/testui/support/testui_driver.py +++ b/testui/support/testui_driver.py @@ -223,6 +223,10 @@ def find_image_match( current_time = now.strftime("%Y-%m-%d%H%M%S") image_name = f"{self.device_udid}{current_time}.png" image_path = self.save_screenshot(image_name) + comparison = os.path.join( + self.__configuration.screenshot_path, + comparison + ) found, p = ImageRecognition( image_path, comparison, threshold, self.device_name, self.configuration.screenshot_path ).compare(image_match) @@ -457,9 +461,9 @@ def stop_recording_and_compare( """ now = datetime.now() current_time = now.strftime("%Y-%m-%d%H%M%S") + log_dir = self.__configuration.screenshot_path video_name = f"{self.device_udid}{current_time}.mp4" - self.stop_recording_screen(video_name) - log_dir = "./logs" + self.stop_recording_screen(os.path.join(log_dir, video_name)) if self.__configuration.screenshot_path != "": log_dir = self.__configuration.screenshot_path found = ImageRecognition( From 2aebedfc8c075c867db91c54171f1d7ae4af184a Mon Sep 17 00:00:00 2001 From: Alvaro Laserna Date: Mon, 31 Jul 2023 14:21:28 +0200 Subject: [PATCH 2/2] remove bootstrap port and fix log_dir path var --- tests/appium_tests.py | 1 - testui/support/appium_driver.py | 32 +++++++++++--------------------- testui/support/testui_driver.py | 2 -- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/tests/appium_tests.py b/tests/appium_tests.py index 8f43ca7..2fd4e42 100644 --- a/tests/appium_tests.py +++ b/tests/appium_tests.py @@ -15,7 +15,6 @@ def selenium_driver(self): .set_logger() .set_chrome_driver() .set_soft_assert(True) - # .set_appium_url("http://localhost:4723") .set_appium_driver() ) driver.configuration.save_full_stacktrace diff --git a/testui/support/appium_driver.py b/testui/support/appium_driver.py index 3cd43ec..241f345 100644 --- a/testui/support/appium_driver.py +++ b/testui/support/appium_driver.py @@ -395,7 +395,6 @@ def start_selenium_driver( def __local_run(url, desired_caps, use_port, udid, log_file): - appium_version_2 = False if url is None: port = use_port bport = use_port + 1 @@ -421,16 +420,9 @@ def __local_run(url, desired_caps, use_port, udid, log_file): file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file) else: file_path = os.path.join(log_dir, log_file) - # Check Appium Version - command = ["appium", "-p", port.__str__(), "-bp", bport.__str__()] - result = subprocess.run(["appium", "-v"], stdout=subprocess.PIPE).stdout - if result.decode('utf-8').startswith("2."): - # for Appium version > 2.0.0 - appium_version_2 = True - command = ["appium", "-p", port.__str__()] with open(file_path, "wb") as out: process = subprocess.Popen( - command, + ["appium", "-p", port.__str__()], stdout=out, stderr=subprocess.STDOUT, ) @@ -445,10 +437,12 @@ def __local_run(url, desired_caps, use_port, udid, log_file): out.close() break out.close() - if appium_version_2: + # Check Appium Version + result = subprocess.run(["appium", "-v"], stdout=subprocess.PIPE).stdout + url = f"http://localhost:{port.__str__()}/wd/hub" + if result.decode('utf-8').startswith("2."): + # for Appium version > 2.0.0 url = f"http://localhost:{port.__str__()}" - else: - url = f"http://localhost:{port.__str__()}/wd/hub" return url, desired_caps, process, file_path return url, desired_caps, None, None @@ -456,7 +450,6 @@ def __local_run(url, desired_caps, use_port, udid, log_file): def __local_run_ios(url, desired_caps, use_port, udid, log_file): process = None - appium_version_2 = False if url is None: port = use_port + 100 device = 0 @@ -477,11 +470,6 @@ def __local_run_ios(url, desired_caps, use_port, udid, log_file): file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file) else: file_path = os.path.join(log_dir, log_file) - # Check Appium Version - result = subprocess.run(["appium", "-v"], stdout=subprocess.PIPE).stdout - if result.decode('utf-8').startswith("2."): - # for Appium version > 2.0.0 - appium_version_2 = True with open(file_path, "wb") as out: process = subprocess.Popen( ["appium", "-p", port.__str__()], @@ -501,10 +489,12 @@ def __local_run_ios(url, desired_caps, use_port, udid, log_file): out.close() break out.close() - if appium_version_2: + # Check Appium Version + url = f"http://localhost:{port.__str__()}/wd/hub" + result = subprocess.run(["appium", "-v"], stdout=subprocess.PIPE).stdout + if result.decode('utf-8').startswith("2."): + # for Appium version > 2.0.0 url = f"http://localhost:{port.__str__()}" - else: - url = f"http://localhost:{port.__str__()}/wd/hub" return url, desired_caps, file_path return url, desired_caps, process diff --git a/testui/support/testui_driver.py b/testui/support/testui_driver.py index 5e50e3b..ca1b7ff 100644 --- a/testui/support/testui_driver.py +++ b/testui/support/testui_driver.py @@ -464,8 +464,6 @@ def stop_recording_and_compare( log_dir = self.__configuration.screenshot_path video_name = f"{self.device_udid}{current_time}.mp4" self.stop_recording_screen(os.path.join(log_dir, video_name)) - if self.__configuration.screenshot_path != "": - log_dir = self.__configuration.screenshot_path found = ImageRecognition( video_name, comparison, threshold, device_name=self.device_name,