Airtest API精讲之keyevent()
作者:互联网
上期回顾:Airtest API精讲之wait(),exists()
以下基于
python3.8;airtestIDE1.2.11;airtest1.2.2;pocoui1.0.83
老规矩开场白,我们今天要讲的是Airtest框架的内容,不是Poco框架的,一般我们说Airtest,其实应该指的是Airtest Project,具体这些概念的关系是什么,可以看之前文章:Airtest Project——UI自动化利器介绍
今天我们讲一下Airtest中的keyevent()接口,它是在设备上执行keyevent按键事件,它包含2个参数:
-
keyname – 平台相关的按键名称
-
**kwargs – 平台相关的参数 kwargs
它支持Android、iOS、Windows三平台。
Android平台下的keyevent()
源码解析
先来看下源码(不想看源码的可以直接跳到后面的演示实例):
# 文件位置:your_python_path/site-packages/airtest/core/android/android.py
def keyevent(self, keyname, **kwargs):
"""
Perform keyevent on the device
Args:
keyname: keyevent name
**kwargs: optional arguments
Returns:
None
"""
self.adb.keyevent(keyname)
可以看到,Android的keyevent()其实就是直接调用了adb的keyevent()。
我们再来看下adb的keyevent():
# 文件位置:your_python_path/site-packages/airtest/core/android/adb.py
def keyevent(self, keyname):
"""
Perform `adb shell input keyevent` command on the device
Args:
keyname: key event name
Returns:
None
"""
self.shell(["input", "keyevent", keyname.upper()])
恩,是的,就是调用了adb的原始命令
adb shell input keyevent keycode
演示实例
# -*- encoding=utf8 -*-
__author__ = "测试工程师小站"
from airtest.core.api import *
auto_setup(__file__)
# 点HOME键
keyevent("HOME")
keyevent("3") # 也可以用数值来代表
# 点BACK键
# 如果你的脚本要同时兼容Android和iOS,则不要使用,因为iOS不支持
keyevent("BACK")
keyevent("4") # 也可以用数值来代表
# 循环删除输入框内容
# Airtest没有清空输入框的方法,所以可以模拟真实的手工操作,就是不停的按删除键
for i in range(20):
keyevent("KEYCODE_DEL") # 或keyevent("67")
所有keyevent的值:adb shell input keyevent键值大全
iOS平台下的keyevent()
源码解析
# 文件位置:your_python_path/site-packages/airtest/core/ios/ios.py
def keyevent(self, keyname, **kwargs):
"""
Perform keyevent on the device
Args:
keyname: home/volumeUp/volumeDown
**kwargs:
Returns:
"""
try:
keyname = KEY_EVENTS[keyname.lower()]
except KeyError:
raise ValueError("Invalid name: %s, should be one of ('home', 'volumeUp', 'volumeDown')" % keyname)
else:
self.press(keyname)
因为iOS的封闭性(很讨厌是不是),所以只支持HOME、音量加、音量减3个键。
Windows平台下的keyevent()
源码解析
先来看下源码(不想看源码的可以直接跳到后面的演示实例):
# 文件位置:your_python_path/site-packages/airtest/core/win/win.py
def keyevent(self, keyname, **kwargs):
"""
Perform a key event
References:
https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html
Args:
keyname: key event
**kwargs: optional arguments
Returns:
None
"""
self.keyboard.SendKeys(keyname)
可以看到Windows下是封装了第三方开源框架pywinauto中的方法。
Windows的按键码:
{SCROLLLOCK}, {VK_SPACE}, {VK_LSHIFT}, {VK_PAUSE}, {VK_MODECHANGE},
{BACK}, {VK_HOME}, {F23}, {F22}, {F21}, {F20}, {VK_HANGEUL}, {VK_KANJI},
{VK_RIGHT}, {BS}, {HOME}, {VK_F4}, {VK_ACCEPT}, {VK_F18}, {VK_SNAPSHOT},
{VK_PA1}, {VK_NONAME}, {VK_LCONTROL}, {ZOOM}, {VK_ATTN}, {VK_F10}, {VK_F22},
{VK_F23}, {VK_F20}, {VK_F21}, {VK_SCROLL}, {TAB}, {VK_F11}, {VK_END},
{LEFT}, {VK_UP}, {NUMLOCK}, {VK_APPS}, {PGUP}, {VK_F8}, {VK_CONTROL},
{VK_LEFT}, {PRTSC}, {VK_NUMPAD4}, {CAPSLOCK}, {VK_CONVERT}, {VK_PROCESSKEY},
{ENTER}, {VK_SEPARATOR}, {VK_RWIN}, {VK_LMENU}, {VK_NEXT}, {F1}, {F2},
{F3}, {F4}, {F5}, {F6}, {F7}, {F8}, {F9}, {VK_ADD}, {VK_RCONTROL},
{VK_RETURN}, {BREAK}, {VK_NUMPAD9}, {VK_NUMPAD8}, {RWIN}, {VK_KANA},
{PGDN}, {VK_NUMPAD3}, {DEL}, {VK_NUMPAD1}, {VK_NUMPAD0}, {VK_NUMPAD7},
{VK_NUMPAD6}, {VK_NUMPAD5}, {DELETE}, {VK_PRIOR}, {VK_SUBTRACT}, {HELP},
{VK_PRINT}, {VK_BACK}, {CAP}, {VK_RBUTTON}, {VK_RSHIFT}, {VK_LWIN}, {DOWN},
{VK_HELP}, {VK_NONCONVERT}, {BACKSPACE}, {VK_SELECT}, {VK_TAB}, {VK_HANJA},
{VK_NUMPAD2}, {INSERT}, {VK_F9}, {VK_DECIMAL}, {VK_FINAL}, {VK_EXSEL},
{RMENU}, {VK_F3}, {VK_F2}, {VK_F1}, {VK_F7}, {VK_F6}, {VK_F5}, {VK_CRSEL},
{VK_SHIFT}, {VK_EREOF}, {VK_CANCEL}, {VK_DELETE}, {VK_HANGUL}, {VK_MBUTTON},
{VK_NUMLOCK}, {VK_CLEAR}, {END}, {VK_MENU}, {SPACE}, {BKSP}, {VK_INSERT},
{F18}, {F19}, {ESC}, {VK_MULTIPLY}, {F12}, {F13}, {F10}, {F11}, {F16},
{F17}, {F14}, {F15}, {F24}, {RIGHT}, {VK_F24}, {VK_CAPITAL}, {VK_LBUTTON},
{VK_OEM_CLEAR}, {VK_ESCAPE}, {UP}, {VK_DIVIDE}, {INS}, {VK_JUNJA},
{VK_F19}, {VK_EXECUTE}, {VK_PLAY}, {VK_RMENU}, {VK_F13}, {VK_F12}, {LWIN},
{VK_DOWN}, {VK_F17}, {VK_F16}, {VK_F15}, {VK_F14}
~ is a shorter alias for {ENTER}
# 另外还有3个别名键
'+': {VK_SHIFT}
'^': {VK_CONTROL}
'%': {VK_MENU} a.k.a. Alt key
详情可以看:
https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html
演示实例
# -*- encoding=utf8 -*-
__author__ = "测试工程师小站"
from airtest.core.api import *
auto_setup(__file__, devices="Windows:///")
keyevent("{ENTER}") # 按回车键
keyevent("^a^c") # 全选并复制
keyevent("^v") # 粘贴
keyevent("'%{F4}") # 按ALT+F4
---------------------------------------------------------------------------------
关注微信公众号即可在手机上查阅,并可接收更多测试分享~
标签:keyevent,kwargs,精讲,VK,API,adb,Airtest,keyname 来源: https://www.cnblogs.com/songzhenhua/p/15641284.html