溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Airtest搭建批量執(zhí)行UI自動化腳本,并批量生成html 報告

發(fā)布時間:2020-07-17 08:04:18 來源:網(wǎng)絡(luò) 閱讀:1057 作者:nineteens 欄目:編程語言

  Airtest 是近幾年出的,幫助文檔很少,反復(fù)查看源幫助文檔和其他相關(guān)資料,編寫了一個安卓UI自動化的輕量小框架。

  使用語言:python3.6.8 開發(fā)工具:pycharm; python安裝airtest :

  pip install airtest

  框架結(jié)構(gòu)如下:

  1、框架簡單介紹

  air 是多個自動化腳本

  conf,lib 配置文件

  data 數(shù)據(jù)

  log 日志

  report html 報告

  XFDAirtestCase.py 入口方法,啟動器

  2、air 腳本的開發(fā)

  通過airtest工具,開發(fā)腳本,可以錄制,也可以自己編寫,請參考源文檔,這里不做多介紹

  ·http://airtest.netease.com/docs/cn/index.html

  3、配置文件,獲取文件所在目錄

  # -*- coding: utf-8 -*

  import os

  Androiddevice=["Android://127.0.0.1:5037/172.16.81.115:5555"]#連接安卓設(shè)備127.0.0.1:5037固定寫法172.16.81.115安卓真機的Ip

  airpath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'air')#腳本目錄

  logpath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'log')#日志目錄

  templatepath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'templates')#模板目錄

  reportpath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'report')#報告目錄

  datapath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data')#測試數(shù)據(jù)目錄

  4、入口腳本,啟動器

  from airtest.cli.runner import AirtestCase, run_script

  from argparse import *

  import shutil

  from conf.config import *

  from lib.log import logger

  from airtest.report.report import simple_report

  class XFDAirtestCase(AirtestCase):#繼承AirtestCase類

  def setUp(self):

  logger.info("案例開始執(zhí)行")

  super(XFDAirtestCase, self).setUp()#繼承父類的setup方法

  def tearDown(self):

  logger.info("案例執(zhí)行結(jié)束")

  super(XFDAirtestCase, self).tearDown()#繼承父類的tearDown方法

  def run_air(self, root_dir, device):#本方法主要是查找腳本文件,目錄文件,初始化AirtestCase所需要的參數(shù),執(zhí)行腳本,并生成報告

  for f in os.listdir(root_dir):#循環(huán)查找air所在的目錄

  if f.endswith(".air"):#以air結(jié)尾的文件

  airName = f

  script = os.path.join(root_dir, f)#腳本目錄

  logger.info(script)

  log = os.path.join(logpath +'\\'+ airName.replace('.air', ''))#日志目錄

  logger.info(log)

  if os.path.isdir(log):

  shutil.rmtree(log)#清空日志目錄文件

  else:

  os.makedirs(log)

  args = Namespace(device=device, log=log, recording=None, script=script)#初始化父類AirtestCase所需要的參數(shù)

  try:

  run_script(args, AirtestCase)#執(zhí)行air腳本文件

  except AssertionError:

  logger.info("案例執(zhí)行失敗")

  finally:

  #output_file = log + '\\' + airName.replace('.air', '') + '.html'

  output_file = reportpath + '\\' + airName.replace('.air', '') + '.html'#生成報告目錄

  simple_report(script, logpath=log, output=output_file)#生成報告的方法

  logger.info("案例執(zhí)行成功")

  if __name__ == '__main__':

  test = XFDAirtestCase()

  #device = ['Android://127.0.0.1:5037/172.16.81.115:5555']

  test.run_air(airpath, Androiddevice)

  5、執(zhí)行過程,以及執(zhí)行結(jié)果報告

  在python環(huán)境下執(zhí)行入口腳本,命令如下:python XFDAirtestCase.py 循環(huán)執(zhí)行air 文件下的air 腳本,以登錄為例,每個air腳本對應(yīng)生成一個html報告, 放到report 目錄中;html 中有詳細(xì)的點擊步驟,也有截圖,報告效果如下:

  6、搭建框架中遇到的問題

  (1)airtest adb 與python中的adb 沖突問題,

  解決方案:把\tools\adb39 中的adb 拷貝到 pyhon 的\Lib\site-packages\airtest\core\android\static\adb\windows 目錄下

  (2) 入口腳本 運行問題,

  直接運行XFDAirtestCase.py 報錯:錯誤如下:

  Error

  Traceback (most recent call last):

  File "D:\python36\lib\site-packages\airtest\cli\runner.py", line 26, in setUpClass

  cls.args = args

  NameError: name 'args' is not defined

  解決: 通過python 命令直接執(zhí)行 進到該項目下 直接執(zhí)行

  python XFDAirtestCase.py


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI