def swipe(v1, v2=None, vector=None, **kwargs): """ Perform the swipe action on the device screen. There are two ways of assigning the parameters * ``swipe(v1, v2=Template(...))`` # swipe from v1 to v2 * ``swipe(v1, vector=(x, y))`` # swipe starts at v1 and moves along the vector. :param v1: the start point of swipe, either a Template instance or absolute coordinates (x, y) :param v2: the end point of swipe, either a Template instance or absolute coordinates (x, y) :param vector: a vector coordinates of swipe action, either absolute coordinates (x, y) or percentage of screen e.g.(0.5, 0.5) :param **kwargs: platform specific `kwargs`, please refer to corresponding docs :raise Exception: general exception when not enough parameters to perform swap action have been provided :return: Origin position and target position :platforms: Android, Windows, iOS :Example: >>> swipe(Template(r"tpl1606814865574.png"), vector=[-0.0316, -0.3311]) >>> swipe((100, 100), (200, 200)) Custom swiping duration and number of steps(Android and iOS):: >>> # swiping lasts for 1 second, divided into 6 steps >>> swipe((100, 100), (200, 200), duration=1, steps=6) """ if isinstance(v1, Template): pos1 = loop_find(v1, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos1 = v1 if v2: if isinstance(v2, Template): pos2 = loop_find(v2, timeout=ST.FIND_TIMEOUT_TMP) else: pos2 = v2 elif vector: if vector[0] <= 1 and vector[1] <= 1: w, h = G.DEVICE.get_current_resolution() vector = (int(vector[0] * w), int(vector[1] * h)) pos2 = (pos1[0] + vector[0], pos1[1] + vector[1]) else: raise Exception("no enough params for swipe") G.DEVICE.swipe(pos1, pos2, **kwargs) delay_after_operation() return pos1, pos2
def wait(v, timeout=None, interval=0.5, intervalfunc=None): """ Wait to match the Template on the device screen :param v: target object to wait for, Template instance :param timeout: time interval to wait for the match, default is None which is ``ST.FIND_TIMEOUT`` :param interval: time interval in seconds to attempt to find a match :param intervalfunc: called after each unsuccessful attempt to find the corresponding match :raise TargetNotFoundError: raised if target is not found after the time limit expired :return: coordinates of the matched target :platforms: Android, Windows, iOS :Example: >>> wait(Template(r"tpl1606821804906.png")) # timeout after ST.FIND_TIMEOUT >>> # find Template every 3 seconds, timeout after 120 seconds >>> wait(Template(r"tpl1606821804906.png"), timeout=120, interval=3) You can specify a callback function every time the search target fails:: >>> def notfound(): >>> print("No target found") >>> wait(Template(r"tpl1607510661400.png"), intervalfunc=notfound) """ timeout = timeout or ST.FIND_TIMEOUT pos = loop_find(v, timeout=timeout, interval=interval, intervalfunc=intervalfunc) return pos
def exists(v): """ Check whether given target exists on device screen :param v: target to be checked :return: False if target is not found, otherwise returns the coordinates of the target :platforms: Android, Windows, iOS :Example: >>> if exists(Template(r"tpl1606822430589.png")): >>> touch(Template(r"tpl1606822430589.png")) Since ``exists()`` will return the coordinates, we can directly click on this return value to reduce one image search:: >>> pos = exists(Template(r"tpl1606822430589.png")) >>> if pos: >>> touch(pos) """ try: pos = loop_find(v, timeout=ST.FIND_TIMEOUT_TMP) except TargetNotFoundError: return False else: return pos
def double_click(v): if isinstance(v, Template): pos = loop_find(v, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos = v G.DEVICE.double_click(pos) delay_after_operation()
def retry_exists(): try: logging.debug("img template threshold: {}".format(self.threshold)) pos = loop_find(self, timeout=ST.FIND_TIMEOUT_TMP, threshold=threshold) except TargetNotFoundError: return False else: return pos
def waitVanish(v, timeout=None, interval=0.5, intervalfunc=None): timeout = timeout or ST.FIND_TIMEOUT pos = loop_find(v, timeout=timeout, interval=interval, intervalfunc=intervalfunc) vanish_pos = loop_find_vanish(v, timeout=timeout, interval=interval, intervalfunc=intervalfunc) return pos
def swipe(v1, v2=None, vector=None, **kwargs): """ Perform the swipe action on the device screen. There are two ways of assigning the parameters * ``swipe(v1, v2=Template(...))`` # swipe from v1 to v2 * ``swipe(v1, vector=(x, y))`` # swipe starts at v1 and moves along the vector. :param v1: the start point of swipe, either a Template instance or absolute coordinates (x, y) :param v2: the end point of swipe, either a Template instance or absolute coordinates (x, y) :param vector: a vector coordinates of swipe action, either absolute coordinates (x, y) or percentage of screen e.g.(0.5, 0.5) :param **kwargs: platform specific `kwargs`, please refer to corresponding docs :raise Exception: general exception when not enough parameters to perform swap action have been provided :return: None :platforms: Android, Windows, iOS """ if isinstance(v1, Template): pos1 = loop_find(v1, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos1 = v1 if v2: if isinstance(v2, Template): pos2 = loop_find(v2, timeout=ST.FIND_TIMEOUT_TMP) else: pos2 = v2 elif vector: if vector[0] <= 1 and vector[1] <= 1: w, h = G.DEVICE.get_current_resolution() vector = (int(vector[0] * w), int(vector[1] * h)) pos2 = (pos1[0] + vector[0], pos1[1] + vector[1]) else: raise Exception("no enough params for swipe") G.DEVICE.swipe(pos1, pos2, **kwargs) delay_after_operation()
def hover(v): """ Perform mouse move and stop """ if isinstance(v, Template): pos = loop_find(v, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos = v G.DEVICE.hover(pos) delay_after_operation() return pos
def right_click(v): """ Perform right click """ if isinstance(v, Template): pos = loop_find(v, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos = v G.DEVICE.right_click(pos) delay_after_operation() return pos
def must_exists(image, resolution=(2160, 1080), timeout=ST.FIND_TIMEOUT_TMP, threshold=None, interval=1, **kwargs): return loop_find(Template(resource_path(f"image/{image}.png"), resolution=resolution, record_pos=RECORD_POS.get(image), **kwargs), timeout=timeout, threshold=threshold, interval=interval)
def exists(v): """ Check whether given target exists on device screen :param v: target to be checked :return: False if target is not found, otherwise returns the coordinates of the target :platforms: Android, Windows, iOS """ try: pos = loop_find(v, timeout=ST.FIND_TIMEOUT_TMP) except TargetNotFoundError: return False else: return pos
def assert_not_exists(v, msg=""): """ Assert target does not exist on device screen :param v: target to be checked :param msg: short description of assertion, it will be recorded in the report :raise AssertionError: if assertion fails :return: None. :platforms: Android, Windows, iOS """ try: pos = loop_find(v, timeout=ST.FIND_TIMEOUT_TMP) raise AssertionError("%s exists unexpectedly at pos: %s, message: %s" % (v, pos, msg)) except TargetNotFoundError: pass
def assert_exists(v, msg=""): """ Assert target exists on device screen :param v: target to be checked :param msg: short description of assertion, it will be recorded in the report :raise AssertionError: if assertion fails :return: coordinates of the target :platforms: Android, Windows, iOS """ try: pos = loop_find(v, timeout=ST.FIND_TIMEOUT, threshold=ST.THRESHOLD_STRICT) return pos except TargetNotFoundError: raise AssertionError("%s does not exist in screen, message: %s" % (v, msg))
def wait(v, timeout=None, interval=0.5, intervalfunc=None): """ Wait to match the Template on the device screen :param v: target object to wait for, Template instance :param timeout: time interval to wait for the match, default is None which is ``ST.FIND_TIMEOUT`` :param interval: time interval in seconds to attempt to find a match :param intervalfunc: called after each unsuccessful attempt to find the corresponding match :raise TargetNotFoundError: raised if target is not found after the time limit expired :return: coordinates of the matched target :platforms: Android, Windows, iOS """ timeout = timeout or ST.FIND_TIMEOUT pos = loop_find(v, timeout=timeout, interval=interval, intervalfunc=intervalfunc) return pos
def touch(v, times=1, **kwargs): """ Perform the touch action on the device screen :param v: target to touch, either a Template instance or absolute coordinates (x, y) :param times: how many touches to be performed :param kwargs: platform specific `kwargs`, please refer to corresponding docs :return: None :platforms: Android, Windows, iOS """ if isinstance(v, Template): pos = loop_find(v, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos = v for _ in range(times): G.DEVICE.touch(pos, **kwargs) time.sleep(0.05) delay_after_operation()
def double_click(v): """ Perform double click :param v: target to touch, either a ``Template`` instance or absolute coordinates (x, y) :return: finial position to be clicked :Example: >>> double_click((100, 100)) >>> double_click(Template(r"tpl1606730579419.png")) """ if isinstance(v, Template): pos = loop_find(v, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos = v G.DEVICE.double_click(pos) delay_after_operation() return pos
def touch(v, **kwargs): """ Perform the touch action on the device screen :param v: target to touch, either a Template instance or absolute coordinates (x, y) :param kwargs: platform specific `kwargs`, please refer to corresponding docs :return: None :platforms: Android, Windows, iOS """ if isinstance(v, Template): try: pos = loop_find(v, timeout=ST.FIND_TIMEOUT) except TargetNotFoundError: raise else: try_log_screen() pos = v G.DEVICE.touch(pos, **kwargs) delay_after_operation()
def touch(v, times=1, **kwargs): """ Perform the touch action on the device screen :param v: target to touch, either a ``Template`` instance or absolute coordinates (x, y) :param times: how many touches to be performed :param kwargs: platform specific `kwargs`, please refer to corresponding docs :return: finial position to be clicked :platforms: Android, Windows, iOS :Example: Click absolute coordinates:: >>> touch((100, 100)) Click the center of the picture(Template object):: >>> touch(Template(r"tpl1606730579419.png", target_pos=5)) Click 2 times:: >>> touch((100, 100), times=2) Under Android and Windows platforms, you can set the click duration:: >>> touch((100, 100), duration=2) Right click(Windows):: >>> touch((100, 100), right_click=True) """ if isinstance(v, Template): pos = loop_find(v, timeout=ST.FIND_TIMEOUT) else: try_log_screen() pos = v for _ in range(times): G.DEVICE.touch(pos, **kwargs) time.sleep(0.05) delay_after_operation() return pos