Skip to content

Commit 7ef2a94

Browse files
authored
Full rewrite of tap element action (#659)
1 parent a644c34 commit 7ef2a94

1 file changed

Lines changed: 71 additions & 79 deletions

File tree

Framework/Built_In_Automation/Mobile/CrossPlatform/Appium/BuiltInFunctions.py

Lines changed: 71 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from Framework.Built_In_Automation.Mobile.iOS import iosOptions
3333
from selenium.webdriver.common.actions.action_builder import ActionBuilder
3434
from selenium.webdriver.common.actions.pointer_input import PointerInput
35-
# from appium.webdriver.common.touch_action import TouchAction
35+
from selenium.webdriver.common.actions import interaction
3636
# from appium.webdriver.common.multi_action import MultiAction
3737
from selenium.webdriver.common.action_chains import ActionChains
3838
from appium.webdriver.common.appiumby import AppiumBy
@@ -2674,16 +2674,27 @@ def Smart_Scroll_To_Element(data_set):
26742674

26752675

26762676

2677-
@logger
2678-
def Tap_Appium(data_set):
2679-
""" Execute "Tap" for an element
2680-
if optional parameter is provided for offset, we will take it from the center of the object and % center of the bound
2677+
def _tap_xy(driver, x, y, hold_sec=0.0):
2678+
actions = ActionChains(driver)
26812679

2682-
Example:
2683-
below example will offset by 25% to the right off the center of the element. If we select 100% it will go to the right edge of the bound. If you want to go left prove -25.
2680+
finger = PointerInput(interaction.POINTER_TOUCH, "finger")
2681+
actions.w3c_actions.devices = [finger]
26842682

2685-
x_offset:y_offset optional parameter 25:0
2683+
finger.create_pointer_move(duration=0, x=int(x), y=int(y), origin="viewport")
2684+
finger.create_pointer_down(button=0)
2685+
2686+
if hold_sec and hold_sec > 0:
2687+
finger.create_pause(float(hold_sec))
2688+
2689+
finger.create_pointer_up(button=0)
2690+
2691+
actions.perform()
26862692

2693+
2694+
@logger
2695+
def Tap_Appium(data_set):
2696+
"""Execute "Tap" for an element.
2697+
Optional offset: x_offset:y_offset in % from element center (e.g., 25:0).
26872698
"""
26882699

26892700
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
@@ -2693,99 +2704,80 @@ def Tap_Appium(data_set):
26932704
return "passed"
26942705

26952706
try:
2696-
2697-
x_offset = False
2698-
y_offset = False
26992707
offset = False
2708+
x_offset = 0
2709+
y_offset = 0
2710+
hold_sec = 0
27002711

27012712
for row in data_set:
27022713
if (
27032714
"option" in str(row[1]).lower().strip()
27042715
and "x_offset:y_offset" in str(row[0]).lower().strip()
27052716
):
27062717
offset = True
2707-
x_offset = ((str(row[2]).lower().strip()).split(":")[0]).strip()
2708-
y_offset = ((str(row[2]).lower().strip()).split(":")[1]).strip()
2718+
parts = str(row[2]).strip().split(":")
2719+
x_offset = int(parts[0].strip())
2720+
y_offset = int(parts[1].strip())
2721+
if row[0].lower().strip() == "hold time":
2722+
hold_sec = float(row[2].strip())
27092723

27102724
Element = LocateElement.Get_Element(data_set, appium_driver)
27112725
if Element == "zeuz_failed":
2712-
CommonUtil.ExecLog(
2713-
sModuleInfo, "Unable to locate your element with given data.", 3
2714-
)
2726+
CommonUtil.ExecLog(sModuleInfo, "Unable to locate your element with given data.", 3)
27152727
return "zeuz_failed"
2716-
else:
2717-
try:
2718-
if Element.is_enabled():
27192728

2720-
if offset == True:
2721-
try:
2722-
CommonUtil.ExecLog(
2723-
sModuleInfo,
2724-
"Tapping the element based on offset using TouchAction",
2725-
1,
2726-
)
2727-
start_loc = Element.location
2728-
height_width = Element.size
2729-
2730-
start_x = int((start_loc)["x"])
2731-
start_y = int((start_loc)["y"])
2732-
2733-
ele_width = int((height_width)["width"])
2734-
ele_height = int((height_width)["height"])
2735-
2736-
# calculate center of the elem
2737-
center_x = start_x + (ele_width / 2)
2738-
center_y = start_y + (ele_height / 2)
2739-
# we need to divide the width and height by 2 as we are offseting from the center not the full
2740-
total_x_offset = (int(x_offset) / 100) * (ele_width / 2)
2741-
total_y_offset = (int(y_offset) / 100) * (ele_height / 2)
2742-
2743-
x_cord_to_tap = center_x + total_x_offset
2744-
y_cord_to_tap = center_y + total_y_offset
2745-
TouchAction(appium_driver).tap(
2746-
None, x_cord_to_tap, y_cord_to_tap, 1
2747-
).perform()
2748-
CommonUtil.ExecLog(
2749-
sModuleInfo,
2750-
"Tapped on element by offset successfully",
2751-
1,
2752-
)
2753-
return "passed"
2729+
try:
2730+
if not Element.is_enabled():
2731+
CommonUtil.ExecLog(sModuleInfo, "Element not enabled. Unable to click.", 3)
2732+
return "zeuz_failed"
27542733

2755-
except:
2756-
# CommonUtil.TakeScreenShot(sModuleInfo)
2757-
CommonUtil.ExecLog(
2758-
sModuleInfo,
2759-
"Element is enabled. Unable to tap based on offset.",
2760-
3,
2761-
)
2762-
return "zeuz_failed"
2763-
else:
2734+
if offset:
2735+
CommonUtil.ExecLog(sModuleInfo, "Tapping the element based on offset using W3C Actions", 1)
27642736

2765-
action = TouchAction(appium_driver)
2766-
action.tap(Element).perform()
2767-
CommonUtil.ExecLog(
2768-
sModuleInfo, "Tapped on element successfully", 1
2769-
)
2770-
return "passed"
2771-
else:
2772-
# CommonUtil.TakeScreenShot(sModuleInfo)
2773-
CommonUtil.ExecLog(
2774-
sModuleInfo, "Element not enabled. Unable to click.", 3
2775-
)
2776-
return "zeuz_failed"
2777-
except Exception:
2778-
errMsg = "Could not select/click your element."
2779-
return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
2737+
start_loc = Element.location
2738+
size = Element.size
2739+
2740+
start_x = int(start_loc["x"])
2741+
start_y = int(start_loc["y"])
2742+
ele_width = int(size["width"])
2743+
ele_height = int(size["height"])
2744+
2745+
center_x = start_x + (ele_width / 2)
2746+
center_y = start_y + (ele_height / 2)
2747+
2748+
# Offset is from center; 100% means edge from center (half-width/half-height)
2749+
total_x_offset = (x_offset / 100.0) * (ele_width / 2.0)
2750+
total_y_offset = (y_offset / 100.0) * (ele_height / 2.0)
2751+
2752+
x_cord_to_tap = center_x + total_x_offset
2753+
y_cord_to_tap = center_y + total_y_offset
2754+
2755+
_tap_xy(appium_driver, x_cord_to_tap, y_cord_to_tap, hold_sec=hold_sec)
2756+
2757+
CommonUtil.ExecLog(sModuleInfo, "Tapped on element by offset successfully", 1)
2758+
return "passed"
2759+
2760+
# No offset: tap center of element (works cross-platform, no TouchAction)
2761+
CommonUtil.ExecLog(sModuleInfo, "Tapping the element using W3C Actions", 1)
2762+
2763+
rect = Element.rect # {'x','y','width','height'}
2764+
x = rect["x"] + rect["width"] / 2
2765+
y = rect["y"] + rect["height"] / 2
2766+
2767+
_tap_xy(appium_driver, x, y, hold_sec=hold_sec)
2768+
CommonUtil.ExecLog(sModuleInfo, "Tapped on element successfully", 1)
2769+
return "passed"
2770+
2771+
except Exception as e:
2772+
errMsg = "Could not select/click your element."
2773+
return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
27802774

27812775
except Exception:
27822776
errMsg = "Unable to tap."
27832777
return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
27842778

27852779

27862780

2787-
2788-
27892781
@logger
27902782
def Seek_Progress_Bar(data_set):
27912783
""" This Function will set the progress bar of an element

0 commit comments

Comments
 (0)