diff --git a/README.md b/README.md index f25922f..f9568c8 100644 --- a/README.md +++ b/README.md @@ -277,8 +277,7 @@ screenshot/bigger one ### Drivers: The testui_driver.py declares the TestUIDriver class which implements methods -from the Elements class. It also implements methods such as "touch_actions" -inherited from Selenium WebDriver TouchActions class. +from the Elements class. The appium_driver.py declares the NewDriver class which implements TestUIDriver. It also implements the desired capabilities such as the location of the .apk., diff --git a/requirements.txt b/requirements.txt index 61313db..2490526 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,11 @@ # dev pylint==3.0.3 -black==24.2.0 +black==24.3.0 build # lib pytest<=8.0.1 -Appium-Python-Client~=3.1.1 +Appium-Python-Client~=4.0.0 opencv-python~=4.8.1 geckodriver-autoinstaller==0.1.0 pytest-xdist~=2.5.0 diff --git a/tests/appium_tests.py b/tests/appium_tests.py index 7ffc08a..85b3638 100644 --- a/tests/appium_tests.py +++ b/tests/appium_tests.py @@ -24,11 +24,12 @@ def selenium_driver(self): @pytest.mark.signup def test_screenshot_methods(self, selenium_driver: TestUIDriver): logger.log_test_name("T92701: Create an account") + print(selenium_driver.device_udid) selenium_driver.get_dimensions() selenium_driver.navigate_to( "https://github.com/testdevlab/Py-TestUI#image-recognition" ) - selenium_driver.e("css", "[data-content=\"README\"]").wait_until_visible() + selenium_driver.click_by_image("./resources/comp.png", threshold=0.6, ratio=0.5, webview=True) selenium_driver.start_recording_screen() time.sleep(1) selenium_driver.stop_recording_and_compare( @@ -40,5 +41,6 @@ def test_screenshot_methods(self, selenium_driver: TestUIDriver): selenium_driver.find_image_match( "./resources/comp.png", 0.6, True, image_match="./logs/image.png" ) - selenium_driver.click_by_image("./resources/comp.png", threshold=0.6) + + time.sleep(110) selenium_driver.raise_errors() diff --git a/testui/elements/testui_element.py b/testui/elements/testui_element.py index 6486cd9..7fe2041 100644 --- a/testui/elements/testui_element.py +++ b/testui/elements/testui_element.py @@ -4,7 +4,6 @@ import os from typing import List -from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webelement import WebElement from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By @@ -513,25 +512,11 @@ def press_hold_for(self, milliseconds=1000): err = None while time.time() < start + timeout: try: - self.get_element() - try: - is_browser: str = self.testui_driver.context - if "NATIVE" in is_browser: - browser = False - else: - browser = True - except Exception: - browser = True - if not browser: - ta = TouchAction(self.driver) - ta.press(self.get_element()).wait( - milliseconds - ).release().perform() - else: - ta = ActionChains(self.driver) - ta.click_and_hold(self.get_element()).pause( - milliseconds // 1000 - ).release().perform() + actions = self.driver.actions() + actions.w3c_actions.pointer_action.click_and_hold(self.get_element()) + actions.w3c_actions.pointer_action.pause(milliseconds // 1000) + actions.w3c_actions.pointer_action.release() + actions.perform() self.__put_log( f'{self.device_name}: element "{self.locator_type}: ' @@ -561,8 +546,7 @@ def click_by_coordinates(self, x, y): try: self.get_element() time.sleep(1) - ta = TouchAction(self.driver) - ta.tap(x=x, y=y).perform() + self.testui_driver.click(x, y) self.__put_log( f'{self.device_name}: element "x={x}: y={y}" clicked after ' f"{time.time() - start}s" @@ -740,11 +724,15 @@ def swipe( end_x = location2.x if end_y is None: end_y = location2.y - action = TouchAction(self.driver) - action.press(x=start_x, y=start_y).wait(duration).move_to( - x=end_x, y=end_y - ).release() - action.perform() + + actions = self.driver.actions() + actions.w3c_actions.pointer_action.move_to_location(x=start_x, y=start_y) + actions.w3c_actions.pointer_action.pointer_down() + if duration: + actions.w3c_actions.pointer_action.pause(duration) + actions.w3c_actions.pointer_action.move_to_location(x=end_x, y=end_y) + actions.w3c_actions.pointer_action.release() + actions.perform() else: if end_x is None: end_x = location.x diff --git a/testui/support/testui_driver.py b/testui/support/testui_driver.py index c18a126..9363479 100644 --- a/testui/support/testui_driver.py +++ b/testui/support/testui_driver.py @@ -6,7 +6,6 @@ from os import path from pathlib import Path -from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import WebDriver from selenium.webdriver import ActionChains from selenium.common.exceptions import WebDriverException @@ -136,17 +135,6 @@ def remove_log_file(self, when_no_errors=True): logger.log_debug("Log file already removed") return self - @deprecated("This method is deprecated and will be removed in a future version. Use actions() instead") - def touch_actions(self) -> TouchAction: - """ - Deprecated function, soon to be removed, use actions instead. - - Will return a TouchAction object for the current driver. This is - meant for Appium Drivers only. - :return: TouchAction - """ - return TouchAction(self.driver) - def actions(self) -> ActionChains: """ Will return an ActionChains object for the current driver. @@ -272,13 +260,14 @@ def find_image_match( return found - def click_by_image(self, image: str, threshold=0.9, webview=False): + def click_by_image(self, image: str, threshold=0.9, webview=False, ratio=1): """ Will click on an element based on the image provided if it can be found within the current screen. - :param image: - :param threshold: - :param webview: + :param image: Image to compare with for the click + :param threshold: limit for comparison + :param webview: Mobile webview requires a shift in Y coordinates + :param ratio: click to image dimension ratio :return: TestUIDriver """ now = datetime.now() @@ -286,14 +275,17 @@ def click_by_image(self, image: str, threshold=0.9, webview=False): image_name = f"{self.device_udid}{current_time}.png" im_path = self.save_screenshot(image_name) x, y = get_point_match(im_path, image, threshold, self.device_name) - ta = TouchAction(self.__appium_driver) + x = int(x * ratio) + y = int(y * ratio) if webview: - y = y + 120 - ta.tap(x=x, y=y).perform() + y = y - 120 + self.click(x, y) logger.log( f"{self.device_name}: element with image {image}" - f"clicked on point ({x},{y})" + f" clicking on point ({x},{y})" ) + self.click(x, y) + self.__delete_screenshot(im_path) return self @@ -316,14 +308,17 @@ def get_dimensions(self): def click(self, x, y): """ - Will execute a touch action on the current screen based on the x and y + Will execute a click action on the current screen based on the x and y coordinates. :param x: :param y: :return: TestUIDriver """ - ta = TouchAction(self.__appium_driver) - ta.tap(x=x, y=y).perform() + actions = self.actions() + actions.w3c_actions.pointer_action.move_to_location(x=x, y=y) + actions.w3c_actions.pointer_action.click() + actions.perform() + logger.log(f'Clicked over "x={x}: y={y}"') return self