diff --git a/airtest/core/api.py b/airtest/core/api.py index 5227e517..91cec8f5 100644 --- a/airtest/core/api.py +++ b/airtest/core/api.py @@ -4,16 +4,18 @@ """ import os import time - +import win32con from six.moves.urllib.parse import parse_qsl, urlparse - from airtest.core.cv import Template, loop_find, try_log_screen from airtest.core.error import TargetNotFoundError from airtest.core.settings import Settings as ST from airtest.utils.compat import script_log_dir from airtest.core.helper import (G, delay_after_operation, import_device_cls, logwrap, set_logdir, using, log) +import win32gui +from airtest.utils.logger import get_logger +LOGGING = get_logger(__name__) """ Device Setup APIs @@ -70,6 +72,36 @@ def connect_device(uri): return dev +def connect_windows_device(classname, name, timeout=30, interval=1): + """ + Return window handle by window name to connect to Windows client + Toggle window handles to set the wait time + The find window handle tool is recommended + :param classname:The window class name + :param name:The window name + :param timeout: + :param interval + :return: + + >>>connect_windows_device(classname="YodaoMainWndClass", name="网易有道词典") + >>>click(Template(r"./pic/tpl1606730579419.png", target_pos=5)) + """ + LOGGING.info('Connecting the specified window handle......') + total_time = timeout + while timeout > 0: + handles = win32gui.FindWindow(classname, name) + # Returns a handle value of 0 when no window is found + if handles == 0: + time.sleep(interval) + timeout -= interval + LOGGING.info(f'Try to connect to the window handle{total_time - timeout}......') + else: + LOGGING.info(f'The specified window handle was successfully connected---{handles}') + auto_setup(__file__, logdir=True, devices=[f"Windows:///{handles}"]) + win32gui.ShowWindow(handles, win32con.SW_SHOWNORMAL) + break + + def device(): """ Return the current active device. @@ -360,6 +392,7 @@ def touch(v, times=1, **kwargs): delay_after_operation() return pos + click = touch # click is alias of touch @@ -594,7 +627,7 @@ def wait(v, timeout=None, interval=0.5, intervalfunc=None): @logwrap -def exists(v): +def exists(v, timeout=ST.FIND_TIMEOUT_TMP): """ Check whether given target exists on device screen @@ -614,7 +647,7 @@ def exists(v): """ try: - pos = loop_find(v, timeout=ST.FIND_TIMEOUT_TMP) + pos = loop_find(v, timeout=timeout) except TargetNotFoundError: return False else: