appium会话启动为何能启动测试app
这是因为appium运行环境需要sdk( 安卓app的自动化 appium server依赖Android sdk),通过sdk连接上设备,从而可以启动测试app,具体可以看下面的日志分析
获取appium运行日志
想获取到日志,当然首先要(1)打开夜神模拟器,adb connect连接上模拟器(2)打开appium(3)启动检查器会话:1)检查器会话启动可以通过手动启动appium服务->启动检查器会话。2)也可以通过之前录制好的脚本eclipse运行这段代码
这里我是eclipse执行代码打开会话器
启动检查器会话打印日志如下(部分日志)
日志分析如下
(1)启动Appium Server,并监听4723本地端口
[2020-03-15 03:55:32][Appium] Welcome to Appium v1.14.0
[2020-03-15 03:55:32][Appium] Non-default server args:
[2020-03-15 03:55:32][Appium] address: 127.0.0.1
[2020-03-15 03:55:32][Appium] logTimestamp: true
[2020-03-15 03:55:32][Appium] allowInsecure: {
[2020-03-15 03:55:32][Appium] }
[2020-03-15 03:55:32][Appium] denyInsecure: {
[2020-03-15 03:55:32][Appium] }
[2020-03-15 03:55:32][Appium] Appium REST http interface listener started on 127.0.0.1:4723
(2)Appium Client向Appium server发送http请求创建一个session,在请求中将Desired Capablities参数 以一个键值对的JSON格式 通过post请求发送到Appium服务器
[2020-03-15 03:55:41][HTTP] --> POST /wd/hub/session
[2020-03-15 03:55:41][HTTP] {"desiredCapabilities":{"appActivity":"com.lemon.lemonban.activity.WelcomeActivity","appPackage":"com.lemon.lemonban","platformName":"Android","deviceName":"127.0.0.1:62001"},"capabilities":{"alwaysMatch":{"appium:appActivity":"com.lemon.lemonban.activity.WelcomeActivity","appium:appPackage":"com.lemon.lemonban","appium:deviceName":"127.0.0.1:62001","platformName":"android"},"firstMatch":[{}]}}
[2020-03-15 03:55:41][W3C] Calling AppiumDriver.createSession() with args: [{"appActivity":"com.lemon.lemonban.activity.WelcomeActivity","appPackage":"com.lemon.lemonban","platformName":"Android","deviceName":"127.0.0.1:62001"},null,{"alwaysMatch":{"appium:appActivity":"com.lemon.lemonban.activity.WelcomeActivity","appium:appPackage":"com.lemon.lemonban","appium:deviceName":"127.0.0.1:62001","platformName":"android"},"firstMatch":[{}]}]
(3)Appium server收到请求后,创建一个新的session,此session id用于和appium client通讯(那这个session对象由谁创建的呢?最终由appium Driver来创建)
[2020-03-15 03:55:41][Appium] Appium v1.14.0 creating new AndroidUiautomator2Driver (v1.33.1) session
[2020-03-15 03:55:41][Appium] Capabilities:
[2020-03-15 03:55:41][Appium] platformName: android
[2020-03-15 03:55:41][Appium] appActivity: com.lemon.lemonban.activity.WelcomeActivity
[2020-03-15 03:55:41][Appium] appPackage: com.lemon.lemonban
[2020-03-15 03:55:41][Appium] deviceName: 127.0.0.1:62001
[2020-03-15 03:55:41][BaseDriver] W3C capabilities {"alwaysMatch":{"platformNa... and MJSONWP desired capabilities {"appActivity":"com.lemon.l... were provided
[2020-03-15 03:55:41][BaseDriver] Creating session with W3C capabilities: {"alwaysMatch":{"platformNa...
[2020-03-15 03:55:41][BaseDriver] Session created with session id: ac0f923d-ecc9-4dad-bd44-d1611df683f5
(4)检查adb是否存在:通过配置的环境变量ANDROID_HOME去固定路径下查找
[2020-03-15 03:55:41][ADB] Using 'adb.exe' from 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe'
(5)查找当前连接的设备,通过【adb devices】命令
查询到一个设备连接,使用这个设备并设置udid(唯一标识符)如果在测试脚本中有设置设备的udid,则在logcat中会显示其设备的udid
[2020-03-15 03:55:41][AndroidDriver] Retrieving device list
[2020-03-15 03:55:41][ADB] Trying to find a connected android device
[2020-03-15 03:55:41][ADB] Getting connected devices...
[2020-03-15 03:55:42][ADB] 1 device(s) connected
[2020-03-15 03:55:42][AndroidDriver] Using device: 127.0.0.1:62001
[2020-03-15 03:55:42][ADB] Using 'adb.exe' from 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe'
[2020-03-15 03:55:42][ADB] Setting device id to 127.0.0.1:62001
(6)获取Android版本
[2020-03-15 03:55:42][ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell getprop ro.build.version.sdk'
[2020-03-15 03:55:42][ADB] Current device property 'ro.build.version.sdk': 22
[2020-03-15 03:55:42][ADB] Device API level: 22
通过执行 adb shell getprop ro.build.version.sdk,我们也可以看到Android Api level =22

通过下图可以知道,API level=22,对应的安卓系统版本是5.1

(7)检查设备是否能够正常响应adb命令
执行 adb wait-for-device等待设备处于连接的状态下再进行adb命令
检查设备是否能够正常响应adb命令,即通过adb shell echo ping,打印出"ping"则认为设备正常,这里响应有个超时时间(默认是5秒)
[2020-03-15 03:55:42][ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 wait-for-device'
[2020-03-15 03:55:43][ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell echo ping'
通过cmd执行上面日志中命令执行看一下

(8)在设备上安装io.appium.settings,并检查是否安装
执行命令adb shell dumpsys package io.appium.settings获取io.appium.setting是否已安装
[2020-03-15 03:55:43][AndroidDriver] Pushing settings apk to device...
[2020-03-15 03:55:43][ADB] Getting install status for io.appium.settings
[2020-03-15 03:55:43][ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell dumpsys package io.appium.settings'
[2020-03-15 03:55:44][ADB] 'io.appium.settings' is installed
[2020-03-15 03:55:44][ADB] Getting package info for 'io.appium.settings'
[2020-03-15 03:55:44][ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell dumpsys package io.appium.settings'
[2020-03-15 03:55:44][ADB] Using 'aapt.exe' from 'D:\androidsdk\android-sdk-windows-appium\build-tools\28.0.3\aapt.exe'
[2020-03-15 03:55:45][ADB] The version name of the installed 'io.appium.settings' is greater or equal to the application version name ('2.14.1' >= '2.14.1')
[2020-03-15 03:55:45][ADB] There is no need to install/upgrade 'C:\Users\TF\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\io.appium.settings\apks\settings_apk-debug.apk'
[2020-03-15 03:55:45][ADB] Getting IDs of all 'io.appium.settings' processes
将appium中的settings apk推送到模拟器中安装,下图可以可看到模拟器中第三方安装apk中就有setting apk(这个apk主要用来辅助appium进行一些设置的工作)

(9)端口转发
[ADB] Forwarding system: 8200 to device: 6790
[ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 forward tcp\:8200 tcp\:6790'
4723端口是appium服务启动的端口(检查器会话访问的端口)
8200端口是appium服务接收到检查器会话发送的请求,通过8200端口和模拟器建立通讯链接
6790端口是模拟器和appium服务建立连接的端口

appium和发送请求的客户端可以不在同一台pc上-》即:appium和模拟器安装在同一台pc上,我们的代码在另一台pc上,脚本中 URL remoteUrl = new URL("http://localhost:4723/wd/hub");
localhost填写appium所在pc的ip即可
(10)检测模拟器中是否安装你要测试的app
[ADB] Getting install status for com.lemon.lemonban
[ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell dumpsys package com.lemon.lemonban'
[ADB] 'com.lemon.lemonban' is installed
(11)执行命令-》停止运行app :shell am force-stop com.lemon.lemonban
执行命令-》清楚app数据:shell pm clear com.lemon.lemonban
注意:(1)模拟器如果正在运行你的app,当检查器会话重新连接上设备,会将当前的运行的app给停止掉即退出(2)然后清除app本地数据(所以测试微信时要注意数据会被清掉)
[ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell am force-stop com.lemon.lemonban'
[ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell pm clear com.lemon.lemonban'
[AndroidDriver] Performed fast reset on the installed 'com.lemon.lemonban' application (stop and clear)
(12)执行命令 shell am start -W -n com.lemon.lemonban/com.lemon.lemonban.activity.WelcomeActivity -S启动测试app
[ADB] Running 'D:\androidsdk\android-sdk-windows-appium\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62001 shell am start -W -n com.lemon.lemonban/com.lemon.lemonban.activity.WelcomeActivity -S'
启动app时不清除数据
脚本里加noReset参数,值为true,则不清除,false则清除

网友评论