Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/appium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
42 changes: 15 additions & 27 deletions testui/elements/testui_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}: '
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
41 changes: 18 additions & 23 deletions testui/support/testui_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -272,28 +260,32 @@ 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()
current_time = now.strftime("%Y-%m-%d%H%M%S")
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
Expand All @@ -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

Expand Down