Airtest ——实践汇总(控件加载不出来的解决方案)
1. Pymysql(No module named ‘cryptography’)
pip install cryptography
pip install paramiko
把 cryptography 与pymysql 放入 airtest路径
注释:mac 环境,Airtest 默认安装位置,/应用程序/AirtestIDE/Contents/MacOS/
python3 默认安装位置,/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages
2. 安装,卸载,包信息,唤醒
方法一(原生adb):
以mac 为例,首先找到airtest adb 的路径,例如:/Applications/AirtestIDE.app/Contents/MacOS/airtest/core/android/static/adb/mac/。以下操作均为adb路径+命令(adb路径简写为adb)
1. 查看所连接的设备
os.system('adb version')
2. 查看所有包名
os.system('adb shell pm list package')
3. 安装
os.popen("adb install -r D:\BaiduYunDownload\kaoyan3.1.0.apk")
4. 卸载
os.system('adb uninstall com.fenbi.android.zenglish')
5. 启动app
def call_adb(str):
rec = os.system("/Applications/AirtestIDE.app/Contents/MacOS/airtest/core/android/static/adb/mac/adb %s" % str)
return rec
call_adb("shell dumpsys activity activities") # 找package+actity
(2) 找到cmp
(3) call_adb("shell am start -n com.fenbi.android.zenglish/com.fenbi.android.zebraenglish.activity.portal.WelcomeActivity") #package+actity
方法二:
from airtest.core.android.android import Android
command = Android()
print("当前设备:%s"% command.get_default_device())
print("uuid:%s"% command.uuid)
print("手机里所有的app:%s"% command.list_app())
print("启动app:%s"% command.start_app('com.tencent.mtt'))
print("停止app:%s"% command.stop_app('com.tencent.mtt'))
print("清除app数据:%s"% command.clear_app('com.tencent.mtt'))
print("安装app:%s"% command.install_app('/Users/zd/Downloads/zenglish-3.13.0-fenbi-test.apk'))
print("卸载app:%s"% command.uninstall_app('com.tencent.mtt'))
print("唤醒:%s"% command.wake()) #执行速度慢,建议使用 os.system(r"D:\softDetail\AirtestIDE-win-1.2.14\AirtestIDE\airtest\core\android\static\adb\windows\adb.exe -s LVSSSKZL9DR4ZLWC shell input keyevent KEYCODE_WAKEUP")
print("home:%s"% command.home())
print("手机是否灭屏返回值:%s"% command.is_screenon())
print("手机里的app是否存在:%s"% command.check_app('com.tencent.mtt'))
方法三:
from airtest.core.api import *
stop_app('com.fenbi.android.zenglish')
start_app('com.fenbi.android.zenglish')
clear_app('com.fenbi.android.zenglish')
install('/Users/zd/Downloads/zenglish-3.13.0-fenbi-test.apk')
uninstall('com.fenbi.android.zenglish')
wake()
home()
1. 点击并长按
# coding=utf-8
from poco.drivers.unity3d import UnityPoco
poco = UnityPoco()
poco('btn_start').click()poco('basic').click()poco('star_single').long_click()poco('star_single').long_click(duration=5)
poco(text='分享').click()
2. 存在
obj = poco(text='分享',type='android.widget.RadioButton')
if obj.exists(): #True
print('返回Ture')
else:
print('返回False')
3. 选择
- 属性
poco(text='分享',type='android.widget.RadioButton')
- 遍历子代
rec = poco("android.view.ViewGroup").offspring("android.widget.TextView")
print('%%%%%%%%%%%s'%len(rec))
for i in range(len(rec)):
print(rec[i].get_text())
4. 获得属性
print(obj.get_text())
print(obj.attr('text'))
print(obj.attr('package'))
5. 拖动
poco('star').drag_to(poco('shell'))
6. 滑动
obj.swipe([0, 0.8],duration=3)
obj.swipe('up')
obj.swipe('down')
obj.swipe('left')
obj.swipe('right')
obj = poco("android.widget.TextView")
xy = poco.get_screen_size()
x = xy[0]
y = xy[1]
print(x,y)
swipe((0.9*x,0.5*y),(0.1*x,0.5*y),duration=1) #向左
swipe((0.1*x,0.5*y),(0.9*x,0.5*y),duration=1) #向右
swipe((x/2,y*0.3),(x/2,y*0.9)) # 从上到下
6. 放大,缩小
from airtest.core.api import *
poco('android:id/content').pinch('in',percent=0.2)
poco('android:id/content').pinch('out',percent=0.2)
7. focus
listView = poco('Scroll View')
listView.focus([0.5, 0.8]).drag_to(listView.focus([0.5, 0.2]))
image.focus('center').long_click()
image.focus([0.1, 0.1]).long_click()
8. wait
# coding=utf-8
from poco.drivers.unity3d import UnityPocofrom poco.exceptions import PocoTargetTimeout
poco = UnityPoco()
bomb_count = 0
while True:
blue_fish = poco('fish_emitter').child('blue')
yellow_fish = poco('fish_emitter').child('yellow')
bomb = poco('fish_emitter').child('bomb')
fish = poco.wait_for_any([blue_fish, yellow_fish, bomb])
if fish is bomb:
# skip the bomb and count to 3 to exit
bomb_count += 1
if bomb_count > 3:
return
else:
# otherwise click the fish to collect.
fish.click()
time.sleep(2.5)
案例二:
# coding=utf-8
import timefrom poco.drivers.unity3d import UnityPoco
poco = UnityPoco()
poco(text='wait UI 2').click()
blue_fish = poco('fish_area').child('blue')
yellow_fish = poco('fish_area').child('yellow')
shark = poco('fish_area').child('black')
poco.wait_for_all([blue_fish, yellow_fish, shark])
poco('btn_back').click()
time.sleep(2.5)
9. 生成测试报告
Ctr+L
10. 关于文件导入
from airtest.core.api import *
import os
print(os.path.abspath(os.path.dirname(os.getcwd())))
ST.PROJECT_ROOT =os.path.abspath(os.path.dirname(os.getcwd()))
using("untitled.air")
from ttest import *
11. 微信控件,有时加载不出来的解决方案
再次导入即可:
1 def replace_import(): 2 from poco.drivers.android.uiautomation import AndroidUiautomationPoco 3 poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)