Skip to content

Commit

Permalink
Add: Android's touch/swipe and other interfaces support relative coor…
Browse files Browse the repository at this point in the history
…dinates

(cherry picked from commit 3632469)
  • Loading branch information
yimelia committed Dec 26, 2023
1 parent bf99ece commit 258930b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions airtest/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from airtest.core.error import TargetNotFoundError
from airtest.core.settings import Settings as ST
from airtest.utils.compat import script_log_dir
from airtest.utils.snippet import parse_device_uri
from airtest.utils.snippet import parse_device_uri, get_absolute_coordinate
from airtest.core.helper import (G, delay_after_operation, import_device_cls,
logwrap, set_logdir, using, log)
# Assertions
Expand Down Expand Up @@ -361,8 +361,8 @@ def touch(v, times=1, **kwargs):
if isinstance(v, Template):
pos = loop_find(v, timeout=ST.FIND_TIMEOUT)
else:
pos = get_absolute_coordinate(v, G.DEVICE)
try_log_screen()
pos = v
for _ in range(times):
G.DEVICE.touch(pos, **kwargs)
time.sleep(0.05)
Expand All @@ -387,8 +387,8 @@ def double_click(v):
if isinstance(v, Template):
pos = loop_find(v, timeout=ST.FIND_TIMEOUT)
else:
pos = get_absolute_coordinate(v, G.DEVICE)
try_log_screen()
pos = v
G.DEVICE.double_click(pos)
delay_after_operation()
return pos
Expand Down Expand Up @@ -435,13 +435,13 @@ def swipe(v1, v2=None, vector=None, **kwargs):
raise
else:
try_log_screen()
pos1 = v1
pos1 = get_absolute_coordinate(v1, G.DEVICE)

if v2:
if isinstance(v2, Template):
pos2 = loop_find(v2, timeout=ST.FIND_TIMEOUT_TMP)
else:
pos2 = v2
pos2 = get_absolute_coordinate(v2, G.DEVICE)
elif vector:
if vector[0] <= 1 and vector[1] <= 1:
w, h = G.DEVICE.get_current_resolution()
Expand Down
10 changes: 10 additions & 0 deletions airtest/utils/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,13 @@ def escape_special_char(string):
str: The string with special characters escaped. e.g. 'testing \!\@\#\$\%\^\&\*\(\)_\+'
"""
return re.sub(r'([!@#\$%\^&\*\(\)_\+\\|;:"\'<>\?\{\}\[\]#\~\^ ])', r'\\\1', string)


def get_absolute_coordinate(coord, dev):
assert isinstance(coord, (tuple, list)) and len(coord) == 2, "Coordinates must be a tuple or list of length 2"
assert all(isinstance(i, (int, float)) for i in coord), "Coordinates must contain only numbers (int or float)"

if coord[0] <= 1 and coord[1] <= 1:
w, h = dev.get_current_resolution()
return (int(coord[0] * w), int(coord[1] * h))
return coord

0 comments on commit 258930b

Please sign in to comment.