實(shí)際測(cè)試應(yīng)用時(shí),測(cè)試場(chǎng)景特別多,直接寫(xiě)在腳本中會(huì)特別亂,所以我們采用每個(gè)測(cè)試用例寫(xiě)在一個(gè)方法里,然后直接執(zhí)行,注意點(diǎn)如下:
1.每一個(gè)case寫(xiě)在一個(gè)方法里,最后直接調(diào)用;
2.必須用try except進(jìn)行錯(cuò)誤處理,否則一旦斷言失敗就會(huì)一直循環(huán)到拋出錯(cuò)誤而且出錯(cuò)case后面的所有case都無(wú)法執(zhí)行;
3.每個(gè)方法上方要注釋case內(nèi)容,方便以后維護(hù);
4.鏈接跳轉(zhuǎn)需要時(shí)間反應(yīng),可在執(zhí)行某一操作后,使用sleep()等待幾秒,否則頁(yè)面還沒(méi)顯示就直接進(jìn)行下一操作會(huì)導(dǎo)致腳本報(bào)錯(cuò);
5.可以單獨(dú)選中某一部分,鼠標(biāo)右鍵,點(diǎn)擊只運(yùn)行選中代碼進(jìn)行調(diào)試.
6.case執(zhí)行過(guò)程中如果有頁(yè)面一直沒(méi)有響應(yīng),使用adb get-state獲取設(shè)備狀態(tài),如果中斷需要使用adb connect ip:port重新連接
具體腳本如下:
# -*- encoding=utf8 -*-
__author__ = "bigdog"
from airtest.core.api import *
auto_setup(__file__)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
#測(cè)試登錄云之家是否成功
def case1(phone):
try:
phone = str(phone)
poco(text="云之家").click()
sleep(3.0)
poco("com.kdweibo.client:id/password").click()
text("123456")
poco("com.kdweibo.client:id/btn_login").click()
assert_exists(Template(r"tpl1565442382395.png", record_pos=(-0.374, -0.216), resolution=(1080, 1920)), "登錄云之家成功")
except Exception as e:
print("登錄云之家失??!")
#測(cè)試退出云之家是否成功
def case2():
try:
touch(Template(r"tpl1565451602515.png", record_pos=(-0.41, -0.745), resolution=(1080, 1920)))
touch(Template(r"tpl1565443930299.png", threshold=0.4, record_pos=(0.243, -0.744), resolution=(1080, 1920)))
touch(Template(r"tpl1565443957885.png", record_pos=(0.008, 0.306), resolution=(1080, 1920)))
assert_exists(Template(r"tpl1565443982452.png", record_pos=(-0.306, -0.197), resolution=(1080, 1920)), "退出登錄成功")
except Exception as e:
print("退出登錄失敗")
#測(cè)試返回主頁(yè)是否成功
def case3():
try:
keyevent("HOME")
assert_exists(Template(r"tpl1565444200648.png", record_pos=(-0.12, 0.191), resolution=(1080, 1920)), "返回主頁(yè)成功")
except Exception as e:
print("返回主頁(yè)失敗")
#天下游打開(kāi)是否正常
def case4():
try:
poco(text="天下游").click()
assert_exists(Template(r"tpl1565451011067.png", record_pos=(-0.394, 0.671), resolution=(1080, 1920)), "請(qǐng)?zhí)顚?xiě)測(cè)試點(diǎn)")
except Exception as e:
print("打開(kāi)天下游失敗!")
#case執(zhí)行
case1(13112345678)
case2()
case3()
case4()
測(cè)試執(zhí)行成功后,就會(huì)生成測(cè)試報(bào)告,如下:

測(cè)試報(bào)告.png
)