使用 Airtest 進行微信小程序自動化測試 (Android & iOS)

使用Airtest進行Android與iOS的微信小程序自動化測試

1. 簡介

隨着微信小程序的逐漸普及,針對微信小程序的測試需求也逐漸豐富起來了。如今針對小程序的自動化測試手段比較不完善,針對的平臺比較具現化。python

本文介紹一下使用Airtest測試框架進行微信小程序自動化測試的方法,並分別介紹在Android和iOS下對詳細的使用步驟。android

2. 需求

這裏咱們以星巴克的小程序爲例git

image

咱們須要github

1.打開關閉小程序
2.查看咖啡信息並試圖購買

針對這幾個需求,咱們使用Airtest對小程序進行測試web

3.Airtest簡介

Airtest的簡要介紹能夠看這裏面試

https://www.oschina.net/p/airtest小程序

http://airtest.netease.com/微信小程序

此次咱們使用Airtest-Ide,poco,iOS-Tagent,這幾個組件完成咱們的任務,這些內容都可以在下面找到api

https://github.com/AirtestProject瀏覽器

4.開始Android測試

這裏使用系統WebView內核:

使用微信聊天框輸入此網址打開

http://debugtbs.qq.com/

或者掃描二維碼

選擇強制使用系統內核,而後重啓微信便可:

注意: Android版本的小程序的 使用的 騰訊TBS瀏覽器內核 暫時沒法使用 咱們的工具來訪問界面元素

如今咱們可使用系統內核進行工做!

不過咱們即將有新的解決方案來支持TBS內核,敬請期待。

4.1 打開小程序任務

首先打開Airtest-ide

image

先進行Android設備的鏈接

選擇connect進行鏈接

在poco輔助窗這裏選擇Android模式,便可看到原生的ui結構等信息

下面進行操做的錄製:

選擇poco錄製模式,進行操做的錄製:

接下來能夠看到生成的代碼狀況

略做調整,並執行代碼

# -*- encoding=utf8 -*- __author__ = "suyuchen" from airtest.core.api import * auto_setup(__file__) from poco.drivers.android.uiautomation import AndroidUiautomationPoco poco = AndroidUiautomationPoco() poco("android.support.v7.widget.RecyclerView").child("android.widget.RelativeLayout")[0].child("android.widget.FrameLayout").child("com.tencent.mm:id/gd").click() sleep(1) snapshot() poco("com.tencent.mm:id/l0").child("android.widget.ImageButton").click() snapshot() 

能夠看到執行效果很是不錯

4.2 查看咖啡信息併購買的腳本

# -*- encoding=utf8 -*- __author__ = "suyuchen" from airtest.core.api import * from poco.drivers.android.uiautomation import AndroidUiautomationPoco auto_setup(__file__) poco = AndroidUiautomationPoco() poco("android.support.v7.widget.RecyclerView").child("android.widget.RelativeLayout")[0].child("android.widget.FrameLayout").child("com.tencent.mm:id/gd").click() while not poco("有你真好").exists(): poco.scroll(direction='vertical', percent=0.3, duration=1.0) snapshot() poco("有你真好").click() poco("當季特飲").click() assert(poco("使用須知:").exists()) poco("android.view.ViewGroup").child("android.widget.FrameLayout").child("android.widget.FrameLayout")[1].child("").child("").child("")[3].child("")[1].click() poco("android.view.ViewGroup").child("android.widget.FrameLayout").child("android.widget.FrameLayout")[1].child("").child("")[2].child("")[1].click() poco("\r 購買\r").click() 

執行效果不錯

5.開始iOS測試

Airtest 是跨平臺的測試框架,固然是對iOS支持的,大致的代碼會是同樣的,可是iOS和Android的ui結構和名稱會有一些平臺相關的不一樣,可是大部分會是相同的,下面展現使用iOS進行相同功能的測試步驟。

5.1 打開小程序任務

首先打開Airtest-ide

image

先進行iOS設備的鏈接,iOS的設備鏈接須要部署iOS-Tagent

https://github.com/AirtestProject/IOS-Tagent

選擇connect進行鏈接

在poco輔助窗這裏選擇iOS模式,便可看到原生的ui結構等信息

下面進行操做的錄製:

image

選擇poco錄製模式,進行操做的錄製:

iOS上webView內的元素識別的不如Android準確,有時候須要藉助圖像識別進行處理

image

運行結果:

image

效果不錯

5.2 查看並試圖購買咖啡功能

下面試着編寫更加複雜的功能

image

能夠看到總體代碼幾乎都是相同的

下面看運行效果:

image

看到運行效果很是好!

6. 小結

本文介紹了使用Airtest測試小程序的方法,歡迎使用Airtest 進行微信小程序測試

相關文章
相關標籤/搜索