当前位置:网站首页>UI自動化測試框架搭建 —— 編寫一個APP自動化
UI自動化測試框架搭建 —— 編寫一個APP自動化
2022-06-28 04:32:00 【TEST_二 黑】
先來看看效果
測試遊記,
test_home_android_text用例執行
視頻號
前言
APP自動化測試和Web自動化測試的不同之處在於它的環境搭建更複雜,不過環境搭建之後,具體的操作步驟是類似的。都是定比特元素+操作元素
之前有一篇使用WEditor開發APP自動化測試脚本可以拿來進行APP自動化的定比特工具
這次介紹另一款更强大的工具「Sonic」
搭建Sonic

由於需要連接的設備不多,這次采用「快速搭建」
下載官方提供的docker-compose.yml文件
官方為了數據的持久化采用了本地的mysql數據庫,但這樣比較麻煩,所以直接修改一下
加了一下
db:
image: mysql
command: --character-set-server=utf8 --collation-server=utf8_general_ci
volumes:
- mysql_db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: Sonic!@#123
MYSQL_DATABASE: sonic
MYSQL_ROOT_HOST: '%'
ports:
- "3307:3306"
networks:
- sonic-network
我這邊主機的ip為172.28.253.211,把它改成你自己的IP就可以了
version: '3'
services:
db:
image: mysql
command: --character-set-server=utf8 --collation-server=utf8_general_ci
volumes:
- mysql_db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: Sonic!@#123
MYSQL_DATABASE: sonic
MYSQL_ROOT_HOST: '%'
ports:
- "3307:3306"
networks:
- sonic-network
sonic-server-simple:
image: "sonicorg/sonic-server-simple:v1.3.1-beta"
depends_on:
- db
environment:
#以下為MySql配置,localhost請替換為自己MySql服務的ipv4地址
- MYSQL_HOST=172.28.253.211
- MYSQL_PORT=3307
- MYSQL_DATABASE=sonic
- MYSQL_USERNAME=root
- MYSQL_PASSWORD=Sonic!@#123
#在服務器部署的話,localhost改為服務器ip,port更改為sonic-server-simple暴露的port(一般不變)
- SONIC_API_HOST=172.28.253.211
- SONIC_API_PORT=8094
- SONIC_NETTY_PORT=8095
#token加密的key值
- SECRET_KEY=sonic
#身份驗證token有效天數
- EXPIRE_DAY=14
#前端頁面訪問地址,不填默認為http://localhost:3000
- CLIENT_HOST=http://172.28.253.211:3000
#文件保留天數(指測試過程產生的文件,包括圖片、錄像等等)
- FILE_KEEP_DAY=60
#測試結果保留天數
- RESULT_KEEP_DAY=60
#以下均為Cron錶達式
#清理文件定時任務
- FILE_CRON=0 0 12 * * ?
#清理測試結果定時任務
- RESULT_CRON=0 0 12 * * ?
#發送日報定時任務
- DAY_CRON=0 0 10 * * ?
#發送周報定時任務
- WEEK_CRON=0 0 10 ? * Mon
networks:
- sonic-network
volumes:
- files:/keepFiles/
- files:/imageFiles/
- files:/recordFiles/
- files:/packageFiles/
- files:/logs/
ports:
- "8094:8094"
- "8095:8095"
sonic-client-web:
image: "sonicorg/sonic-client-web:v1.3.1-beta"
environment:
#在服務器部署的話,localhost改為服務器ip,port更改為sonic-server-simple暴露的port(一般不變)
- SONIC_API_HOST=172.28.253.211
- SONIC_API_PORT=8094
networks:
- sonic-network
ports:
- "3000:80"
volumes:
files:
mysql_db:
networks:
sonic-network:
driver: bridge
然後按照官網的說明run起來~
然後在插了手機的電腦上運行一個Agent
編寫自動化脚本
選擇一臺安卓設備進入
點擊「控件元素」-「獲取控件元素」就可以拿到元素定比特信息了

把xpath推薦的語句拿出來就可以了
把它放到代碼中
- page:
pageName: home_android
desc: 首頁_android
locators:
- {
desc: "掛號文案",type: "xpath",value: '//android.view.ViewGroup[contains(@content-desc,"掛號,全國7800+醫院")]', name: "registered_text" }
同理拿到問診和購藥的文案
- {
desc: "掛號文案",type: "xpath",value: '//android.view.ViewGroup[contains(@content-desc,"掛號,全國7800+醫院")]', name: "registered_text" }
- {
desc: "問診文案",type: "xpath",value: '//android.view.ViewGroup[contains(@content-desc,"問診,27萬醫生在線服務")]', name: "inquiry_text" }
- {
desc: "購藥文案",type: "xpath",value: '//android.view.ViewGroup[contains(@content-desc,"購藥,微醫自營購藥更安心")]', name: "buy_medicine_text" }
獲取設備序列號
鼠標移動到右上角就可以看到設備序列號了
編寫脚本
打開APP
編寫一個fixture夾具來打開APP
後面可以根據需求把sys、udid、app放到jenkinsfile中實現參數化構建
@pytest.fixture(scope='module')
def home_android():
home = HomePage(file_name="home_android")
with allure.step(f"打開微醫APP"):
home.open_phone(sys='android', udid="8688dab6", app='wy')
home.click(home.home_index) # 進入首頁Tab
time.sleep(2)
yield home
home.close()
調試的時候修改打開手機對應的url
def open_phone(self, sys='android', udid="8688dab6", app='wys', apk=None):
""" 移動端打開操作 http://appium.io/docs/en/writing-running-appium/caps/ """
url = "http://172.28.57.33:4723/wd/hub"
desired_caps = {
"udid": udid}
if sys == 'android':
desired_caps['platformName'] = "Android"
if app == 'wys':
# 打開微醫生APP
desired_caps["appPackage"] = "com.greenline.yihuantong"
desired_caps["appActivity"] = ".home.WelcomeActivity"
elif app == 'wy':
# 打開微醫APP
desired_caps["appPackage"] = "com.greenline.guahao"
desired_caps["appActivity"] = ".home.WelcomeActivity"
desired_caps["skipServerInstallation"] = True
desired_caps["automationName"] = "UiAutomator2"
desired_caps["noReset"] = True
desired_caps["newCommandTimeout"] = 3600
elif sys == 'ios':
desired_caps['platformName'] = "iOS"
if app == 'wys':
# 打開微醫生APP
desired_caps["bundleId"] = "com.minkanginfo.guahao"
elif app == 'wy':
# 打開微醫APP
desired_caps["bundleId"] = "com.lvxian.guahao"
desired_caps["automationName"] = "XCUITest"
if apk:
desired_caps1 = copy.deepcopy(desired_caps)
desired_caps1["appPackage"] = "com.android.settings"
desired_caps1["appActivity"] = ".Settings"
driver = app_webdriver.Remote(url, desired_caps1)
if app == 'wys':
self.install_apk(driver, "com.greenline.yihuantong", apk)
elif app == 'wy':
self.install_apk(driver, "com.greenline.guahao", apk)
driver.quit()
print(desired_caps)
self.driver = app_webdriver.Remote(url, desired_caps)
self.wait_for(10)
return self.driver
注意這個函數內部根據實際打開的APP來進行修改或調整
根據需求修改
appPackage
appActivity
使用上面這個url的時候需要在對應的電腦上打開Appium,端口指定為默認端口4723
踩坑
自動化安裝包
在給小米手機自動安裝APP的時候老是有彈框,可以通過下面方式修改:
設置 -> 授權管理 -> 右上角設置按鈕 -> USB安裝管理 ->關閉
如果授權管理頁面的右上角沒有設置按鈕
在開發者選項中 -> 啟動MIUI優化 ->關閉
再返回到授權頁面,就可以看到 右上角的設置按鈕了
關閉USB安裝管理後,通過USB安裝就不會有確認彈窗了
反複提示需要安裝uiautomator2
使用Appium控制部分安卓手機時,重複提示需要安裝uiautomator2
desired_caps[“skipServerInstallation”] = True
編寫用例主體
用例內容和上次一樣,不過這次換成了打開APP
@compose(feature="微醫APP", story="首頁", title='主入口下方文案校驗')
@pytest.mark.parametrize("way", ["registered_text", "inquiry_text", "buy_medicine_text"])
def test_home_android_text(home_android, way):
""" 按鈕下方文案測試 * 掛號 * 問診 * 購藥 """
ele = getattr(home_android, way)
with allure.step(f"查看{ele.desc}"):
text = home_android.get_attribute(ele, "content-desc")
if way == 'registered_text':
assert text == '掛號,全國7800+醫院'
elif way == 'inquiry_text':
assert text == '問診,27萬醫生在線服務'
elif way == 'buy_medicine_text':
assert text == '購藥,微醫自營購藥更安心'
使用參數化的方式測試三個地方的文案是否符合要求
運行與查看結果
運行之後,直接在Sonic平臺上查看運行的效果
代碼見:https://gitee.com/zx660644/uitest/tree/first_android_test
IOS操作也同理
最後感謝每一個認真閱讀我文章的人,下面這個自動化網盤鏈接也是我費了幾天時間整理的非常全面的,對提啥鞥希望也能幫助到有需要的你!
這些資料,對於做【軟件測試】想進階的朋友來說應該是最全面最完整的備戰倉庫,這個倉庫也陪伴我走過了最艱難的路程,希望也能幫助到你!
凡事要趁早,特別是技術行業,一定要提昇技術功底。希望對大家有所幫助…….
边栏推荐
- Analyse complète annuelle du marché chinois de l'audio en 2022
- Matlab exercises -- exercises related to symbolic operation
- 【Linux】【Mysql】ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
- Multithreading and high concurrency II: detailed introduction to volatile and CAS
- Multi project design and development · introduction to class library project
- MSc 307 (88) (2010 FTPC code) Part 2 smoke and toxicity test
- Multithreading and high concurrency six: source code analysis of thread pool
- first. Net core MVC project
- 易周金融 | Q1手机银行活跃用户规模6.5亿;理财子公司布局新兴领域
- Une seule pile dans l'ordre inverse avec des fonctions récursives et des opérations de pile
猜你喜欢

Games104 operation 2-colorgrading

Bitlock recovery occurs in win 10, and the blue screen error code is 0x1600007e

The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?

27年,微软IE结束了!

华为9年经验的软件测试总监工作感悟—写给还在迷茫的朋友

The second round of free public classes of the red team is coming ~ 8:00 tomorrow night!

测试开发必备技能:安全测试漏洞靶场实战

How to apply for ASTM E108 flame retardant test for photovoltaic panels?

How to clean the nozzle of Epson l3153 printer

Tiktok actual battle ~ take off the blogger
随机推荐
Design a stack with getmin function
测试开发必备技能:安全测试漏洞靶场实战
一文详解|增长那些事儿
03 summary of various additions, updates and deletions of mongodb documents
Conversion between decimal and BCD codes in C language
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
《性能之巅第2版》阅读笔记(二)--CPU监测
Go language -select statement
The SQL of filincdc always reports this error when there are multiple tables. How can I solve it
2022年中国音频市场年度综合分析
leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
僅用遞歸函數和棧操作逆序一個棧
27 years, Microsoft IE is over!
Another option for ERP upgrade, MES system
10: 00 interview, came out at 10:02, the question is really too
Learning about DC-DC step-down chip of sy8120i (12V reduced to 3.3V)
Little knowledge about function templates --
Building log analysis system with elk (II) -- deployment and installation
RT-Thread 双向链表(学习笔记)
Matlab exercises -- routine operation of matrix