airtest.core.win.win module

require_app(func)[源代码]
class Windows(handle=None, dpifactor=1, **kwargs)[源代码]

基类:Device

Windows 客户端。

property uuid
connect(handle=None, **kwargs)[源代码]

连接到一个windows窗口并且把窗口置前

参数:

**kwargs – optional arguments

返回:

None

shell(cmd)[源代码]

在subprocess里运行命令行

参数:

cmd – 需要运行的命令行

抛出:

subprocess.CalledProcessError – when command returns non-zero exit status

返回:

命令行的输出内容作为bytes string返回

snapshot(filename=None, quality=10, max_size=None)[源代码]

截取一张图片并且保存到ST.LOG_DIR文件夹中

参数:
  • filename – 截图的文件名,默认为{time}.jpg

  • quality – The image quality, integer in range [1, 99]

  • max_size – the maximum size of the picture, e.g 1200

返回:

截图的内容

keyevent(keyname, **kwargs)[源代码]

执行一个按键响应

引用

https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html

参数:
  • keyname – key event

  • **kwargs – optional arguments

返回:

None

text(text, **kwargs)[源代码]

Input text

参数:
  • text – 待输入的字符串

  • **kwargs – optional arguments

返回:

None

key_press(key)[源代码]

模拟一个按下按键的事件。

发送键盘扫描码至计算机来告知哪个按键被按下。一些游戏使用DirectInput设备,只响应键盘扫描码,而不是虚拟键码。可以用key_press()方法来模拟发送键盘扫描码,而不是上述发送虚拟键码的keyevent()方法。

参数:

key – 一个字符串来表示哪个按键将被按下。可用的选择有:{‘ESCAPE’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘0’, ‘-’, ‘=’, ‘BACKSPACE’, ‘TAB’, ‘Q’, ‘W’, ‘E’, ‘R’, ‘T’, ‘Y’, ‘U’, ‘I’, ‘O’, ‘P’, ‘[’, ‘]’, ‘ENTER’, ‘LCTRL’, ‘A’, ‘S’, ‘D’, ‘F’, ‘G’, ‘H’, ‘J’, ‘K’, ‘L’, ‘;’, “’”, ‘`’, ‘LSHIFT’, ‘BACKSLASH’, ‘Z’, ‘X’, ‘C’, ‘V’, ‘B’, ‘N’, ‘M’, ‘,’, ‘.’, ‘/’, ‘RSHIFT’, ‘*’, ‘LALT’, ‘SPACE’, ‘CAPS_LOCK’, ‘F1’, ‘F2’, ‘F3’, ‘F4’, ‘F5’, ‘F6’, ‘F7’, ‘F8’, ‘F9’, ‘F10’, ‘NUM_LOCK’, ‘SCROLL_LOCK’, ‘NUMPAD_7’, ‘NUMPAD_8’, ‘NUMPAD_9’, ‘NUMPAD_-’, ‘NUMPAD_4’, ‘NUMPAD_5’, ‘NUMPAD_6’, ‘NUMPAD_+’, ‘NUMPAD_1’, ‘NUMPAD_2’, ‘NUMPAD_3’, ‘NUMPAD_0’, ‘NUMPAD_.’, ‘F11’, ‘F12’, ‘PRINT_SCREEN’, ‘PAUSE’, ‘NUMPAD_ENTER’, ‘RCTRL’, ‘NUMPAD_/’, ‘RALT’, ‘HOME’, ‘UP’, ‘PAGE_UP’, ‘LEFT’, ‘RIGHT’, ‘END’, ‘DOWN’, ‘PAGE_DOWN’, ‘INSERT’, ‘DELETE’, ‘LWINDOWS’, ‘RWINDOWS’, ‘MENU’}.

key_release(key)[源代码]

模拟一个释放按键的事件。

发送键盘扫描码至计算机来告知哪个按键被释放。一些游戏使用DirectInput设备,只响应键盘扫描码,而不是虚拟键码。可以用key_release()方法来模拟发送键盘扫描码。一般情况下key_release()方法与所释放按键的key_press()方法搭配使用。

参数:

key – 一个字符串来表示哪个按键将被释放。

touch(pos, **kwargs)[源代码]

执行鼠标点击操作

引用

https://pywinauto.readthedocs.io/en/latest/code/pywinauto.mouse.html

参数:
  • pos – 点击位置的坐标

  • **kwargs – optional arguments

示例

>>> from airtest.core.api import connect_device
>>> dev = connect_device("Windows:///")
>>> dev.touch((100, 100))  # absolute coordinates
>>> dev.touch((0.5, 0.5))  # relative coordinates
返回:

None

double_click(pos)[源代码]
swipe(p1, p2, duration=0.8, steps=5, button='left')[源代码]

执行拖动操作(鼠标按下并且释放)

参数:
  • p1 – 起始点坐标

  • p2 – 终点坐标

  • duration – 执行滑动操作的时间间隔

  • steps – 滑动操作的步数

  • button – mouse button to press, ‘left’, ‘right’ or ‘middle’, default is ‘left’

示例

>>> from airtest.core.api import connect_device
>>> dev = connect_device("Windows:///")
>>> dev.swipe((100, 100), (200, 200), duration=0.5)
>>> dev.swipe((0.1, 0.1), (0.2, 0.2), duration=0.5)
返回:

None

mouse_move(pos)[源代码]

模拟一个移动鼠标的事件。

已知的bug:

因为pywinauto库存在的bug,用户可能在使用此方法时遇到移动后位置与目标位置的x和y坐标有1个像素点偏差的情况。

参数:

pos – 一个(x, y)的tuple, 其中x和y分别表示目标位置在屏幕上的x和y坐标。

mouse_down(button='left')[源代码]

模拟一个按下鼠标按键的事件。

参数:

button – 一个字符串来表示将按下哪个鼠标按键。有以下的鼠标按键选项:{‘left’, ‘middle’, ‘right’}。

mouse_up(button='left')[源代码]

模拟一个释放鼠标按键的事件。

一般情况下mouse_up()方法与所释放鼠标按键的mouse_down()方法搭配使用。

参数:

button – 一个字符串来表示将释放哪个鼠标按键。

start_app(path, *args, **kwargs)[源代码]

启动应用

参数:
返回:

None

stop_app(pid)[源代码]

关闭应用

参数:

pid – 需要被关闭应用的process ID

返回:

None

set_foreground()[源代码]

将窗口切换到最前

返回:

None

set_focus()

将窗口切换到最前

返回:

None

get_rect()[源代码]

获取窗口的边界矩形的尺寸

返回:

win32structures.RECT

get_title()[源代码]

获取窗口标题

返回:

窗口标题

get_pos()[源代码]

获取窗口位置的坐标

返回:

窗口左上角坐标(left, top)

move(pos)[源代码]

移动窗口到指定坐标

参数:

pos – 要移动到的目标位置坐标 (x, y)

返回:

None

kill()[源代码]

杀死应用进程

返回:

None

set_clipboard(text)[源代码]

Set clipboard content

参数:

text – text to be set to clipboard

示例

>>> from airtest.core.api import connect_device
>>> dev = connect_device("Windows:///")
>>> dev.set_clipboard("hello world")
>>> print(dev.get_clipboard())
'hello world'
>>> dev.paste()  # paste the clipboard content
返回:

None

get_clipboard()[源代码]

Get clipboard content

返回:

clipboard content

paste()[源代码]

Perform paste action

返回:

None

property focus_rect
get_current_resolution()[源代码]
get_ip_address()[源代码]

获取应用的外部IP地址。

返回:

ip address

返回类型:

:py:obj:’str’

start_recording(max_time=1800, output=None, fps=10, snapshot_sleep=0.001, orientation=0, max_size=None, *args, **kwargs)[源代码]

Start recording the device display

参数:
  • max_time – maximum screen recording time, default is 1800

  • output – ouput file path

  • mode – the backend write video, choose in [“ffmpeg”] ffmpeg: ffmpeg-python backend, higher compression rate.

  • fps – frames per second will record

  • snapshot_sleep – sleep time for each snapshot.

  • orientation – 1: portrait, 2: landscape, 0: rotation.

  • max_size – max size of the video frame, e.g.800, default is None. Smaller sizes lead to lower system load.

返回:

path of video file

返回类型:

save_path

示例

Record 30 seconds of video and export to the current directory test.mp4:

>>> from airtest.core.api import connect_device, sleep
>>> dev = connect_device("Windows:///")
>>> save_path = dev.start_recording(output="test.mp4")
>>> sleep(30)
>>> dev.stop_recording()
>>> print(save_path)

You can specify max_size to limit the video’s maximum width/length. Smaller video sizes result in lower CPU load.

>>> dev.start_recording(output="test.mp4", max_size=800)

备注

1 Don’t resize the app window duraing recording, the recording region will be limited by first frame. 2 If recording still working after app crash, it will continuing write last frame before the crash.

stop_recording()[源代码]

Stop recording the device display. Recoding file will be kept in the device.