diff --git a/airtest/core/api.py b/airtest/core/api.py index a26bb87e..9e93f19a 100644 --- a/airtest/core/api.py +++ b/airtest/core/api.py @@ -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 @@ -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) @@ -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 @@ -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() diff --git a/airtest/utils/snippet.py b/airtest/utils/snippet.py index 184d16eb..735753b9 100644 --- a/airtest/utils/snippet.py +++ b/airtest/utils/snippet.py @@ -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