appium+python自動化測試文檔
一.認識appium
1. 什么是appium
- appium是開源的移動端自動化測試框架;
- appium可以測試原生的、混合的、以及移動端的web項目;
- appium可以測試ios,android應用(當然了,還有firefox os);
- appium是跨平台的,可以用在osx,windows以及linux桌面系統上
2. appium的哲學
- 不需要為了自動化而且重新編譯或修改測試app;
- 不應該讓移動端自動化測試限定在某種語言和某個具體的框架;也就是說任何人都可以使用自己最熟悉最順手的語言以及框架來做移動端自動化測試;
- 不要為了移動端的自動化測試而重新發明輪子,重新寫一套驚天動地的api;也就是說webdriver協議里的api已經夠好了,拿來改進一下就可以了;
- 移動端自動化測試應該是開源的;
二.初步認識appium工作過程
- appium有C/S模式
- appium是基於webdriver協議對移動設備自動化api擴展而成的,所有具有和webdriver一樣的特性,比如多語言支持。
- webdriver是基於http協議的,第一連接會建立一個session會話,並通過post發送一個json告知服務端相關測試信息。
- 對於Android來說,4.2以后是基於UiAutomator框架實現查找注入事件的,4.2以前則是instrumentation框架的,並封裝成一個叫Selendroid提供服務。
- 客戶端只需要發送http請求實現通訊,意味着客戶端就是多語言支持的。
- appium服務端是node.js寫的,所以安裝那個平台都是先安裝node,然后npm install -g appium(需要FQ)。
三.環境搭建
需要安裝的軟件
- JDK:1.8.0_221 環境變量如上圖
- Python:3.7.4 環境變量如上圖,安裝完成會自動完成。
Cmd運行 python
3.node.js:10.16.3
1.安裝時會自動添加
2.安裝完成后在安裝路徑下新建兩個文件夾node_global、node_cache
3/CMD npm –v 有版本號顯示
4.android-sdk 當前最新 最好用SDK Manager.exe下載
Android SDK Tools, Android SDK Platform-Tools,Android SDK Bulid-tools
三個需要文件夾配置Path環境辨率如上圖
cmd 運行 adb version 會顯示版本號
cmd 運行 android 會執行 Android SDK Manager
5.Appium:1.13.0
官網進行下載安裝appium-installer.exe
6.Appium-docto
安裝 npm install -g appium-doctor
node_modules\.bin添加到系統環境變量Path中
運行appium-doctor 出現如下圖表示環境成功
7.Appium-Python-Client安裝步驟
pip install Appium-Python-Client
8.連接手機設備
確定打開開發者模式USB調試
adb device –l 查看連接狀態並且能夠查看 device:xxx
四.簡單使用Appium客戶端
前期准備
"platformName": "Android", 測試安卓
"platformVersion": "7", 安卓版本
"deviceName": "leo", adb devices –l 可獲取devices名稱
aapt dump badging + xxx.apk 可以獲取 appPackage,appActivity 2個數據
查看如下字樣獲取
package: name='cn.gloud.client.mobile'
launchable-activity: name='cn.gloud.client.mobile.init.InitActivity'
"appPackage": "cn.gloud.client.mobile",
"appActivity": "cn.gloud.client.mobile.init.InitActivity"
如下為啟動格來雲游戲實例:
1 from appium import webdriver 2 3 caps = {} 4 caps["platformName"] = "Android" 5 caps["platformVersion"] = "7" 6 caps["deviceName"] = "leo" 7 caps["appPackage"] = "cn.gloud.client.mobile" 8 caps["appActivity"] = "cn.gloud.client.mobile.init.InitActivity" 9 10 driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) 11 12 el1 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.HorizontalScrollView/android.widget.FrameLayout/android.widget.LinearLayout[2]/android.view.View") 13 el1.click() 14 el2 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.RelativeLayout[1]/android.widget.EditText") 15 el2.click() 16 el2.send_keys("賬號") 17 el3 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout[1]/android.widget.EditText") 18 el3.send_keys("密碼") 19 el4 = driver.find_element_by_id("cn.gloud.client.mobile:id/login_btn") 20 el4.click() 21 el5 = driver.find_element_by_id("cn.gloud.client.mobile:id/ad_close_img") 22 el5.click() 23 el6 = driver.find_element_by_id("cn.gloud.client.mobile:id/ad_close_img") 24 el6.click() 25 26 driver.quit()