Skip to content

Commit

Permalink
Add: Both android and win support clicking or sliding according to re…
Browse files Browse the repository at this point in the history
…lative coordinate proportions, such as touch((0.5, 0.5)).

(cherry picked from commit f6d97ee)
  • Loading branch information
yimelia committed Dec 26, 2023
1 parent 258930b commit e550fa9
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 8 deletions.
64 changes: 64 additions & 0 deletions airtest/core/android/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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'):
Expand Down Expand Up @@ -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)):
Expand All @@ -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):
Expand Down
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, 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
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 = 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()
Expand Down
2 changes: 2 additions & 0 deletions airtest/core/win/win.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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]))
Expand Down
38 changes: 38 additions & 0 deletions tests/test_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions tests/test_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest
import numpy
import win32api
from testconf import try_remove
from .testconf import try_remove

import time
import os
Expand Down Expand Up @@ -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))
Expand Down
29 changes: 28 additions & 1 deletion tests/test_yosemite.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

0 comments on commit e550fa9

Please sign in to comment.