diff --git a/airtest/core/android/android.py b/airtest/core/android/android.py index 3b33e287..9bb0f904 100644 --- a/airtest/core/android/android.py +++ b/airtest/core/android/android.py @@ -27,6 +27,7 @@ from airtest.core.settings import Settings as ST from airtest.aircv.screen_recorder import ScreenRecorder, resize_by_max, get_max_size +from airtest.utils.snippet import get_absolute_coordinate from airtest.utils.logger import get_logger LOGGING = get_logger(__name__) @@ -99,6 +100,39 @@ def touch_proxy(self): input_event=self.input_event) return self._touch_proxy + @touch_proxy.setter + def touch_proxy(self, touch_method): + """ + Specify a touch method, if the method fails to initialize, try to use other methods instead + + 指定一个触摸方案,如果该方法初始化失败,则尝试使用其他方法代替 + + Args: + touch_method: "MINITOUCH" or Minitouch() object + + Returns: + TouchProxy object + + Raises: + TouchMethodNotSupportedError when the connection fails + + Examples: + >>> dev = Android() + >>> dev.touch_proxy = "MINITOUCH" + + >>> from airtest.core.android.touch_methods.minitouch import Minitouch + >>> minitouch = Minitouch(dev.adb) + >>> dev.touch_proxy = minitouch + + """ + if self._screen_proxy: + self._screen_proxy.teardown() + self._touch_proxy = TouchProxy.auto_setup(self.adb, + default_method=touch_method, + ori_transformer=self._touch_point_by_orientation, + size_info=self.display_info, + input_event=self.input_event) + @property def touch_method(self): """ @@ -116,6 +150,29 @@ def touch_method(self): """ return self.touch_proxy.method_name + @touch_method.setter + def touch_method(self, name): + """ + Specify the touch method for the device, but this is not recommended + 为设备指定触摸方案,但不建议这样做 + + Just to be compatible with some old codes + 仅为了兼容一些旧的代码 + + Args: + name: "MINITOUCH" or Minitouch() object + + Returns: + None + + Examples: + >>> dev = Android() + >>> dev.touch_method = "MINITOUCH" + + """ + warnings.warn("No need to manually specify touch_method, airtest will automatically specify a suitable touch method, when airtest>=1.1.2") + self.touch_proxy = name + @property def cap_method(self): """ @@ -536,9 +593,11 @@ def touch(self, pos, duration=0.01): None """ + pos = get_absolute_coordinate(pos, self) self.touch_proxy.touch(pos, duration) def double_click(self, pos): + pos = get_absolute_coordinate(pos, self) self.touch(pos) time.sleep(0.05) self.touch(pos) @@ -558,6 +617,8 @@ def swipe(self, p1, p2, duration=0.5, steps=5, fingers=1): None """ + p1 = get_absolute_coordinate(p1, self) + p2 = get_absolute_coordinate(p2, self) self.touch_proxy.swipe(p1, p2, duration=duration, steps=steps, fingers=fingers) def pinch(self, center=None, percent=0.5, duration=0.5, steps=5, in_or_out='in'): @@ -593,6 +654,7 @@ def swipe_along(self, coordinates_list, duration=0.8, steps=5): None """ + coordinates_list = [get_absolute_coordinate(pos, self) for pos in coordinates_list] self.touch_proxy.swipe_along(coordinates_list, duration=duration, steps=steps) def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50)): @@ -609,6 +671,8 @@ def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, of Returns: None """ + tuple_from_xy = get_absolute_coordinate(tuple_from_xy, self) + tuple_to_xy = get_absolute_coordinate(tuple_to_xy, self) self.touch_proxy.two_finger_swipe(tuple_from_xy, tuple_to_xy, duration=duration, steps=steps, offset=offset) def logcat(self, *args, **kwargs): diff --git a/airtest/core/api.py b/airtest/core/api.py index 9e93f19a..a26bb87e 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, get_absolute_coordinate +from airtest.utils.snippet import parse_device_uri 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 = get_absolute_coordinate(v1, G.DEVICE) + pos1 = v1 if v2: if isinstance(v2, Template): pos2 = loop_find(v2, timeout=ST.FIND_TIMEOUT_TMP) else: - pos2 = get_absolute_coordinate(v2, G.DEVICE) + pos2 = v2 elif vector: if vector[0] <= 1 and vector[1] <= 1: w, h = G.DEVICE.get_current_resolution() diff --git a/airtest/core/win/win.py b/airtest/core/win/win.py index a8377528..a56f1701 100644 --- a/airtest/core/win/win.py +++ b/airtest/core/win/win.py @@ -21,6 +21,7 @@ from airtest.aircv.screen_recorder import ScreenRecorder, resize_by_max, get_max_size from airtest.core.device import Device from airtest.core.settings import Settings as ST +from airtest.utils.snippet import get_absolute_coordinate from airtest.utils.logger import get_logger LOGGING = get_logger(__name__) @@ -461,6 +462,7 @@ def kill(self): self.app.kill() def _action_pos(self, pos): + pos = get_absolute_coordinate(pos, self) if self.app: pos = self._windowpos_to_screenpos(pos) pos = (int(pos[0]), int(pos[1])) diff --git a/tests/test_android.py b/tests/test_android.py index 3eb4aa96..c34fc616 100644 --- a/tests/test_android.py +++ b/tests/test_android.py @@ -158,6 +158,13 @@ def test_touch(self): self.android.touch_method = i self.android.touch((100, 100)) + def test_touch_percentage(self): + for i in (TOUCH_METHOD.ADBTOUCH, TOUCH_METHOD.MINITOUCH, TOUCH_METHOD.MAXTOUCH): + self.android.touch_method = i + self.android.touch((0.5, 0.5)) + time.sleep(2) + self.android.keyevent("BACK") + def test_swipe(self): for i in (TOUCH_METHOD.ADBTOUCH, TOUCH_METHOD.MINITOUCH, TOUCH_METHOD.MAXTOUCH): self.android.touch_method = i @@ -170,6 +177,16 @@ def test_swipe(self): with self.assertRaises(Exception): self.android.swipe((100, 100), (300, 300), fingers=3) + def test_swipe_percentage(self): + for i in (TOUCH_METHOD.ADBTOUCH, TOUCH_METHOD.MINITOUCH, TOUCH_METHOD.MAXTOUCH): + self.android.touch_method = i + self.android.swipe((0.6, 0.5), (0.4, 0.5)) + self.android.swipe((0.3, 0.5), (0.6, 0.5), fingers=1) + self.android.swipe((0.1, 0.1), (0.3, 0.3), fingers=2) + + with self.assertRaises(Exception): + self.android.swipe((0.1, 0.1), (0.3, 0.3), fingers=3) + def test_get_top_activity(self): self._install_test_app() self.android.start_app(PKG) @@ -206,6 +223,16 @@ def test_swipe_along(self): with self.assertRaises(Exception): self.android.swipe_along(coordinates_list) + def test_swipe_along_percentage(self): + coordinates_list = [(0.1, 0.3), (0.7, 0.3), (0.1, 0.7), (0.8, 0.8)] + for i in (TOUCH_METHOD.MINITOUCH, TOUCH_METHOD.MAXTOUCH): + self.android.touch_method = i + self.android.swipe_along(coordinates_list) + self.android.swipe_along(coordinates_list, duration=3, steps=10) + self.android.touch_method = TOUCH_METHOD.ADBTOUCH + with self.assertRaises(Exception): + self.android.swipe_along(coordinates_list) + def test_two_finger_swipe(self): for i in (TOUCH_METHOD.MINITOUCH, TOUCH_METHOD.MAXTOUCH): self.android.touch_method = i @@ -217,6 +244,17 @@ def test_two_finger_swipe(self): with self.assertRaises(Exception): self.android.two_finger_swipe((100, 100), (200, 200)) + def test_two_findger_swipe_percentage(self): + for i in (TOUCH_METHOD.MINITOUCH, TOUCH_METHOD.MAXTOUCH): + self.android.touch_method = i + self.android.two_finger_swipe((0.1, 0.1), (0.2, 0.2)) + self.android.two_finger_swipe((0.1, 0.1), (0.2, 0.2), duration=3, steps=10) + self.android.two_finger_swipe((0.1, 0.1), (0.2, 0.2), offset=(-0.02, 0.1)) + self.android.two_finger_swipe((0.1, 0.1), (0.2, 0.2), offset=(-0.2, 0.1)) + self.android.touch_method = TOUCH_METHOD.ADBTOUCH + with self.assertRaises(Exception): + self.android.two_finger_swipe((0.1, 0.1), (0.2, 0.2)) + def test_disconnect(self): self.android.snapshot() self.android.touch((100, 100)) diff --git a/tests/test_win.py b/tests/test_win.py index 20fae8af..653c107c 100644 --- a/tests/test_win.py +++ b/tests/test_win.py @@ -3,7 +3,7 @@ import unittest import numpy import win32api -from testconf import try_remove +from .testconf import try_remove import time import os @@ -36,7 +36,8 @@ def test_text(self): self.windows.text("abc") def test_touch(self): - self.windows.touch((11, 11)) + self.windows.touch((100, 100)) + # self.windows.touch((0.5, 0.5)) def test_swipe(self): self.windows.swipe((11, 11), (100, 100)) diff --git a/tests/test_yosemite.py b/tests/test_yosemite.py index a245feca..b1716268 100644 --- a/tests/test_yosemite.py +++ b/tests/test_yosemite.py @@ -1,5 +1,5 @@ # encoding=utf-8 -from airtest.core.android.android import ADB, Javacap, YosemiteIme +from airtest.core.android.android import ADB, Javacap, YosemiteIme, YosemiteExt from airtest.aircv.utils import string_2_img from numpy import ndarray import unittest @@ -68,5 +68,32 @@ def test_end(cls): cls.ime.end() +class TestYosemiteExt(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.adb = ADB() + devices = cls.adb.devices() + if not devices: + raise RuntimeError("At lease one adb device required") + cls.adb.serialno = devices[0][0] + cls.yosemite = YosemiteExt(cls.adb) + + def test_change_lang(self): + self.yosemite.change_lang("ja") + self.yosemite.change_lang("zh") + + def test_clipboard(self): + text1 = "test clipboard" + self.yosemite.set_clipboard(text1) + self.assertEqual(self.yosemite.get_clipboard(), text1) + + # test escape special char + text2 = "test clipboard with $pecial char #@!#%$#^&*()'" + self.yosemite.set_clipboard(text2) + self.assertEqual(self.yosemite.get_clipboard(), text2) + + + if __name__ == '__main__': unittest.main()