Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加一个自动连接WindowsApp的的接口方法,切换窗口自动等待连接 #1060

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions airtest/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -360,6 +392,7 @@ def touch(v, times=1, **kwargs):
delay_after_operation()
return pos


click = touch # click is alias of touch


Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down