1、appium工作原理

  1. 启动appium server
  2. appium client 发送Desired Capabilities信息给appium server,appium server返回一个session id给client
  3. appium client发送指令给appium server,appium server把命令发送到移动设备上去执行
  4. 移动端设备将执行结果返回给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
adb devices

appium入门篇(2)--desired capabilities-LMLPHP
如果设备未连接,尝试手动连接

#夜神模拟器连接命令
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()

运行结果

运行脚本,打开模拟器观察效果,
appium入门篇(2)--desired capabilities-LMLPHP

appium入门篇(2)--desired capabilities-LMLPHP

06-30 05:08