1、appium工作原理
- 啟動appium server
- appium client 傳送Desired Capabilities資訊給appium server,appium server返回一個session id給client
- appium client傳送指令給appium server,appium server把命令傳送到移動裝置上去執行
- 移動端裝置將執行結果返回給appium server,appium server再返回給client
desired capabilities
desired capabilities是什麼,它的作用是告訴appium server被測試apk相關的資訊,移動裝置相關的資訊。直接上官網的解釋,地址:
https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
Desired Capabilities are keys and values encoded in a JSON object, sent by Appium clients to the server when a new automation session is requested. They tell the Appium drivers all kinds of important things about how you want your test to work. Each Appium client builds capabilities in a way specific to the client's language, but at the end of the day, they are sent over to Appium as JSON objects.
2、準備工作
- 啟動安卓模擬器,並開啟開發者模式
- 啟動appium server
PS C:\Users\StephenWang> appium -a 127.0.0.1 -p 4723
[Appium] Welcome to Appium v1.13.0
[Appium] Non-default server args:
[Appium] address: 127.0.0.1
[Appium] Appium REST http interface listener started on 127.0.0.1:4723
下載錘子日曆apk,v1.6
下載地址:http://www.paopaoche.net/android/77944.html- 檢視模擬器是否連線成功
adb devices
如果裝置未連線,嘗試手動連線
#夜神模擬器連線命令
adb connect 127.0.0.1:62001
# 逍遙模擬器
adb connect 127.0.0.1:21503
3、第一個appium指令碼
在模擬器上安裝錘子日曆並啟動。
獲取包名和啟動的activity
aapt dump badging C:\appium\chuizirili_ppc.apk
package: name='com.smartisan.calendar' versionCode='20161127' versionName='1.7.2'
uses-permission:'android.permission.GET_ACCOUNTS'
uses-permission:'android.permission.MANAGE_ACCOUNTS'
uses-permission:'android.permission.INTERNET'
uses-permission:'android.permission.VIBRATE'
uses-permission:'android.permission.READ_CONTACTS'
uses-permission:'android.permission.READ_CALENDAR'
uses-permission:'android.permission.WRITE_CALENDAR'
uses-permission:'android.permission.WAKE_LOCK'
uses-permission:'android.permission.RECEIVE_BOOT_COMPLETED'
uses-permission:'android.permission.USE_CREDENTIALS'
uses-permission:'android.permission.READ_SYNC_SETTINGS'
uses-permission:'com.google.android.googleapps.permission.GOOGLE_AUTH.mail'
uses-permission:'android.permission.AUTHENTICATE_ACCOUNTS'
uses-permission:'android.permission.ACCESS_COARSE_LOCATION'
uses-permission:'android.permission.ACCESS_FINE_LOCATION'
uses-permission:'android.permission.READ_SYNC_STATS'
uses-permission:'com.smartisan.permission.READ_ACCOUNT'
uses-permission:'android.permission.ACCESS_NETWORK_STATE'
uses-permission:'android.permission.READ_EXTERNAL_STORAGE'
uses-permission:'android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission:'android.permission.READ_PROFILE'
uses-permission:'android.permission.CALL_PHONE'
uses-permission:'android.permission.READ_PHONE_STATE'
uses-permission:'android.permission.ACCESS_WIFI_STATE'
uses-permission:'android.permission.CHANGE_WIFI_STATE'
uses-permission:'android.permission.CHANGE_CONFIGURATION'
uses-permission:'android.permission.GET_TASKS'
uses-permission:'android.permission.MOUNT_UNMOUNT_FILESYSTEMS'
uses-permission:'android.permission.WRITE_CONTACTS'
uses-permission:'android.permission.READ_CALENDAR'
uses-permission:'android.permission.WRITE_SYNC_SETTINGS'
uses-permission:'android.permission.WRITE_SETTINGS'
sdkVersion:'14'
targetSdkVersion:'19'
application: label='Smartisan Calendar' icon='res/mipmap-xhdpi-v4/ic_launcher_calendar.png'
launchable activity name='com.android.calendar.AllInOneActivity'label='' icon=''
uses-feature:'android.hardware.location'
uses-feature:'android.hardware.location.gps'
uses-feature:'android.hardware.location.network'
uses-feature:'android.hardware.wifi'
uses-feature:'android.hardware.telephony'
uses-feature:'android.hardware.touchscreen'
main
other-activities
search
other-receivers
other-services
supports-screens: 'small' 'normal' 'large' 'xlarge'
supports-any-density: 'true'
locales: '--_--' 'ca' 'da' 'fa' 'ja' 'nb' 'be' 'de' 'af' 'bg' 'th' 'fi' 'hi' 'vi' 'sk' 'uk' 'el' 'nl' 'pl' 'sl' 'tl' 'am' 'in' 'ko' 'ro' 'ar' 'fr' 'hr' 'sr' 'tr' 'cs' 'es' 'ms' 'et' 'it' 'lt' 'pt' 'hu' 'ru' 'zu' 'lv' 'sv' 'iw' 'sw' 'fr_CA' 'lo_LA' 'en_GB' 'et_EE' 'ka_GE' 'km_KH' 'zh_HK' 'hy_AM' 'zh_CN' 'en_IN' 'mn_MN' 'es_US' 'pt_PT' 'zh_TW' 'ms_MY'
densities: '120' '160' '240' '320' '480'
從上面可知,package: name='com.smartisan.calendar' ,aunchable activity name='com.android.calendar.AllInOneActivity'
編寫指令碼
from time import sleep
#從appium中匯入webdriver
from appium import webdriver
desired_apability = {}
#平臺名稱:Android/IOS
desired_apability["platformName"] = "Android"
#系統版本
desired_apability["platformVersion"] ="4.4.2"
#裝置名稱
desired_apability["deviceName"] = "127.0.0.1:62001"
#是否重置會話
desired_apability["noReset"] = True
#啟動的Activity
desired_apability["appActivity"] = "com.android.calendar.AllInOneActivity"
#包名
desired_apability["appPackage"] = "com.smartisan.calendar"
#apk的地址
desired_apability["app"] = r"C:\appium\chuizirili_ppc.apk"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_apability)
#睡眠5s
sleep(5)
print("Install %s and start sucess" %desired_apability["appPackage"])
driver.quit()
執行結果
執行指令碼,開啟模擬器觀察效果,