美文网首页
Appium iOS、Android自动化测试+性能检测

Appium iOS、Android自动化测试+性能检测

作者: 云醉倚清风 | 来源:发表于2023-04-03 16:04 被阅读0次

测试系统说明:

  • mac OS:14.0

  • Xcode: 15.0.1

  • Appium Server:v2.2.1

  • Appium inspector:Appium-Inspector-mac-2023.3.1.dmg

  • Java: 17.0.7

  • iOS:17.1.1 iphone XR

  • npm:8.15.0

  • Android:12 华为P40

一:安装相关软件/插件

1:Appium Server桌面端

Install Appium - Appium Documentation

#安装
npm i --location=global appium
#安装成功
appium
#输出
[Appium] Welcome to Appium v2.2.1

需要安装iOS插件

appium driver install xcuitest

安装完注意重启Appium

2:HomeBrew相关插件

没有homeBrew参考 Homebrew国内如何自动安装

# 安装Node,Appium是一个node应用程序。
brew install node
# 安装appium
npm install -g appium
# 安装appium-doctor 检测iOS的Appium环境是否安装成功
npm install appium-doctor -g
# 检测
appium-doctor --ios

输出结果

mac@Mac-Pro % appium-doctor --ios              
WARN AppiumDoctor [Deprecated] Please use appium-doctor installed with "npm install @appium/doctor --location=global"
info AppiumDoctor Appium Doctor v.1.16.2
info AppiumDoctor ### Diagnostic for necessary dependencies starting ###
info AppiumDoctor  ✔ The Node.js binary was found at: /usr/local/bin/node
info AppiumDoctor  ✔ Node version is 16.17.0
info AppiumDoctor  ✔ Xcode is installed at: /Applications/Xcode.app/Contents/Developer
info AppiumDoctor  ✔ Xcode Command Line Tools are installed in: /Applications/Xcode.app/Contents/Developer
info AppiumDoctor  ✔ DevToolsSecurity is enabled.
info AppiumDoctor  ✔ The Authorization DB is set up properly.
info AppiumDoctor  ✔ Carthage was found at: /usr/local/bin/carthage. Installed version is: 0.39.0
info AppiumDoctor  ✔ HOME is set to: /Users/mac
info AppiumDoctor ### Diagnostic for necessary dependencies completed, no fix needed. ###
info AppiumDoctor 
info AppiumDoctor ### Diagnostic for optional dependencies starting ###
WARN AppiumDoctor  ✖ opencv4nodejs cannot be found.
WARN AppiumDoctor  ✖ ffmpeg cannot be found
WARN AppiumDoctor  ✖ mjpeg-consumer cannot be found.
WARN AppiumDoctor  ✖ set-simulator-location is not installed
WARN AppiumDoctor  ✖ idb and idb_companion are not installed
WARN AppiumDoctor  ✖ applesimutils cannot be found
WARN AppiumDoctor  ✖ ios-deploy cannot be found
info AppiumDoctor ### Diagnostic for optional dependencies completed, 7 fixes possible. ###
info AppiumDoctor 
info AppiumDoctor ### Optional Manual Fixes ###
info AppiumDoctor The configuration can install optionally. Please do the following manually:
WARN AppiumDoctor  ➜ Why opencv4nodejs is needed and how to install it: http://appium.io/docs/en/writing-running-appium/image-comparison/
WARN AppiumDoctor  ➜ ffmpeg is needed to record screen features. Please read https://www.ffmpeg.org/ to install it
WARN AppiumDoctor  ➜ mjpeg-consumer module is required to use MJPEG-over-HTTP features. Please install it with 'npm i -g mjpeg-consumer'.
WARN AppiumDoctor  ➜ set-simulator-location is needed to set location for Simulator. Please read https://github.com/lyft/set-simulator-location to install it
WARN AppiumDoctor  ➜ Why idb is needed and how to install it: https://github.com/appium/appium-idb
WARN AppiumDoctor  ➜ Why applesimutils is needed and how to install it: http://appium.io/docs/en/drivers/ios-xcuitest/
WARN AppiumDoctor  ➜ ios-deploy is used as a fallback command to install iOS applications to real device. Please read https://github.com/ios-control/ios-deploy/ to install it
info AppiumDoctor 
info AppiumDoctor ###
info AppiumDoctor 
info AppiumDoctor Bye! Run appium-doctor again when all manual fixes have been applied!
info AppiumDoctor 

比如我这里输出 Diagnostic for necessary dependencies completed, no fix needed.证明 我得必备插件已安装

3:WebDriverAgent

WebDriverAgent简称WDA。它是由Facebook推出的一款移动端测试框架。它是在iOS客户端实现了一个WebDriver的Server,借助这个server,我们可以远程控制iOS设备进行测试。通过它能够进行启动、杀死应用,点击或者滚动等操作。

# 选择appium的WebDriverAgent源clone
git clone https://github.com/appium/WebDriverAgent
  1. 打开WebDriverAgent工程,Targets下WebDriverAgentKLib和WebDriverAgentRunner都需要录入开发者账号

  2. 选择WebDriverAgentRunner开始不会安装app

  3. 点击Product->Test 发现WebDriverAgentRunner App已安装

  4. 手机上显示Automation Running Hold both volume buttons to stop

  5. 控制台输出ServerURLHere->http://172.16.xx.xx:8100<-ServerURLHere

  6. 浏览器输入http://172.16.xx.xx:8100/status

{
  "value": {
    "message": "WebDriverAgent is ready to accept commands",
    "state": "success",
    "os": {
      "testmanagerdVersion": 28,
      "name": "iOS",
      "sdkVersion": "16.1",
      "version": "16.2"
    },
    "ios": {
      "ip": "172.16.38.157"
    },
    "ready": true,
    "build": {
      "time": "Mar 31 2023 09:52:07",
      "productBundleIdentifier": "com.facebook.WebDriverAgentRunner"
    }
  },
  "sessionId": null
}

4:Appium inspector

Appium Inspector是一个图形用户界面(GUI)工具,允许用户检查移动应用程序中元素的属性和属性。它是Appium测试自动化框架的一部分,并可用于为iOS和Android应用程序创建自动化测试。

通过Appium Inspector,我们可以:

  • 检查应用程序中可用的UI元素
  • 查看每个元素的属性和属性值
  • 点击或输入文本来模拟用户与应用程序的交互
  • 使用Appium Inspector生成测试代码,以便在多个设备和平台上运行测试

下载地址

5:Appium-Python-Client

用来执行录制好的脚本的pip install Appium-Python-Client

注意这里我使用pip3安装的,发现无法找到对应的包...这个是基于python2.x的

二:iOS测试记录

理解主要工具之间的关系

  • Appium Server:提供服务,驱动WebDriverAgent

  • Appium Inspector:采集界面元素,用于生成自动化测试的代码

  • WebDriverAgent驱动iOS界面执行自动化测试

下载测试项目:git clone https://github.com/appium/ios-uicatalog

1:启动Appium Server

  • Hose:配置为本机Ip

  • Port:默认即可

#执行
appium
#输出
[Appium] Welcome to Appium v2.2.1
[Appium] Attempting to load driver uiautomator2...
[Appium] Requiring driver at /Users/mac/.appium/node_modules/appium-uiautomator2-driver
[Appium] Appium REST http interface listener started on http://0.0.0.0:4723
[Appium] You can provide the following URLs in your client code to connect to this server:
[Appium]    http://127.0.0.1:4723/ (only accessible from the same host)
[Appium]    http://172.16.39.9:4723/
[Appium]    http://172.27.190.4:4723/
[Appium] Available drivers:
[Appium]   - uiautomator2@2.32.3 (automationName 'UiAutomator2')
[Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use.

2:启动 WebDriverAgent

若已经启动则跳过,必须启动,否则Appium Inspector启动会报错

3:启动Appium Inspector

  • Remote Host:配置为本机Ip

  • Remote Port:默认即可

  • Remote Path:不填

  • ssl:不开启

Desired Capabilities配置:

```json
{
  "platformName": "iOS",
  "appium:platformVersion": "16.2",
  "appium:deviceName": "iphone XR",
  "appium:udid": "xx",  //真机的udid,模拟器的自行查找
 "appium:automationName": "XCUITest",
  "appium:bundleId": "com.example.apple-samplecode.UICatalog" //测试目标的bundleId
}

点击启动后。Appium Server控制台输出相关成功提示

[BaseDriver] W3C capabilities and MJSONWP desired capabilities were provided
[BaseDriver] Creating session with W3C capabilities: {
[BaseDriver]   "alwaysMatch": {
[BaseDriver]     "platformName": "iOS",
[BaseDriver]     "appium:platformVersion": "16.2",
[BaseDriver]     "appium:deviceName": "iphone XR",
[BaseDriver]     "appium:udid": "00008020-000139860C2A002E",
[BaseDriver]     "appium:bundleId": "com.example.apple-samplecode.UICatalog",
[BaseDriver]     "appium:includeSafariInWebviews": true,
[BaseDriver]     "appium:newCommandTimeout": 3600,
[BaseDriver]     "appium:connectHardwareKeyboard": true
[BaseDriver]   },
[BaseDriver]   "firstMatch": [
[BaseDriver]     {}
[BaseDriver]   ]
[BaseDriver] }
[BaseDriver] Session created with session id: 047a1367-2490-42e9-a518-3c20e0cb886b

此时Appium Inspector图形界面已出来

4:开始录制可执行脚本

image.png

点击顶部操作栏中的start Recording。选择图形界面可点击区域点击,Selected Element出现定位按钮Tap,点击记录。

el1 = driver.find_element(by=AppiumBy.XPATH, value="//XCUIElementTypeApplication[@name=\"UIKitCatalog\"]/XCUIElementTypeWindow/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther")
el1.click()
el2 = driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="Simple")
el2.click()

右上角选择自己熟悉的脚本语言。点击Show/Hide Boilerplate Code,Record出现:

# This sample code uses the Appium python client v2
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

# For W3C actions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput

caps = {}
caps["platformName"] = "iOS"
caps["appium:platformVersion"] = "16.2"
caps["appium:deviceName"] = "iphone XR"
caps["appium:udid"] = "xx"
caps["appium:bundleId"] = "com.example.apple-samplecode.UICatalog"
caps["appium:includeSafariInWebviews"] = True
caps["appium:newCommandTimeout"] = 3600
caps["appium:connectHardwareKeyboard"] = True

driver = webdriver.Remote("http://172.16.xx.xx:4723/wd/hub", caps)

el1 = driver.find_element(by=AppiumBy.XPATH, value="//XCUIElementTypeApplication[@name=\"UIKitCatalog\"]/XCUIElementTypeWindow/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther")
el1.click()
el2 = driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="Simple")
el2.click()

driver.quit()

保存好py文件后,执行python xx.py 在此之前确保WebDriverAgent程序启动。

5:优化WebDriverAgent启动

为避免每次启动都需要通过Xcode,可以添加如下脚本启动执行。

# 先安装这个,用来获取连接设备的UDID
brew install libimobiledevice
# 解锁钥匙串,签名应用,PASSWORD为mac开机密码
PASSWORD="你自己的开机密码" 
security unlock-keychain -p $PASSWORD ~/Library/Keychains/login.keychain

# 获取设备的UDID
UDID=$(idevice_id -l | head -n1)

# 运行测试
xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination "id=$UDID" USE_PORT=8100 test 

6:结合性能检测

这里使用python的tidevice库,可以获取iOS系统信息

import tidevice
from tidevice._perf import DataType

t = tidevice.Device()
perf = tidevice.Performance(t, [DataType.CPU, DataType.MEMORY])
def callback(_type: tidevice.DataType, value: dict):
    print("R:", _type.value, value)

perf.start("com.apple.Preferences", callback=callback)

结合点击相关事件,整理时间顺序即可得出操作相关的系统消耗

三:Android测试记录

需要安装Android插件

appium driver install uiautomator2

安装完注意重启Appium

1:启动Appium Server

配置与iOS一致

2:启动Appium Inspector

Desired Capabilities配置:

{
  "platformName": "Android",
  "appium:platformVersion": "12",
  "appium:deviceName": "NAB0220B05000915",
  "appium:appPackage": "com.xx.xx",
  "appium:appActivity": "com.xx.activity.WelcomeActivity",
"appium:automationName": "UiAutomator2"
}
  • deviceName:可通过命令adb devices查看

  • platformVersion:可在手机上查看,可通过命令adb shell getprop ro.build.version.release查看

  • appPackage:在app的buildgradle中查询,即appId

  • appActivity:在AndroidManifest.xml中可以看到<category android:name="android.intent.category.LAUNCHER" />有次声明的即为启动的入口

注意1:添加的时候platformVersion这几个字段会默认补充appium:

注意2:除非与包名一致可以写.xxxxx,否则需要前往此Activity,确认其包路径,比如我的包名是com.example.test,我的启动Activity是com.example.test.startActivity,那么我得appActivity可以配置为.startActivity,如果在其他包下就需要写全路径

注意3:当存在多个devices的时候,需要使用命令adb -s xx 执行。

[BaseDriver] Session created with session id: e38bc109-43e2-4eac-883d-747565814895
[UiAutomator2] Starting 'com.vecentek.demo' directly on the device

此时Appium Inspector图形界面已出现

3:开始录制可执行脚本

记录方式与iOS一致,导出自己熟悉的脚本语言

# This sample code uses the Appium python client v2
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time
# For W3C actions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput

caps = {}
caps["platformName"] = "Android"
caps["appium:platformVersion"] = "12"
caps["appium:deviceName"] = "NAB0220B05000915"
caps["appium:appPackage"] = "com.xx.xx"
caps["appium:appActivity"] = "com.xx.activity.WelcomeActivity"
caps["appium:ensureWebviewsHavePages"] = True
caps["appium:nativeWebScreenshot"] = True
caps["appium:newCommandTimeout"] = 3600
caps["appium:connectHardwareKeyboard"] = True

driver = webdriver.Remote("http://172.16.38.21:4723/wd/hub", caps)

#.....

driver.quit()

4:结合性能检测

这里需要用到python的subprocess库,能够很方便的执行adb命令,读取返回信息

import subprocess
import time

# adb -s NAB0220B05000915 shell dumpsys cpuinfo | grep com.example.myapp
adb_command = ['adb','-s', 'NAB0220B05000915', 'shell', 'dumpsys', 'cpuinfo', '|','grep','com.example.myapp']

while True:
    # 执行某个操作
    result = subprocess.run(adb_command, capture_output=True)

    # 获取命令输出
    output = result.stdout.decode('utf-8')
    print(output)

    # 暂停 1 秒钟
    time.sleep(1)

相关文章

网友评论

      本文标题:Appium iOS、Android自动化测试+性能检测

      本文链接:https://www.haomeiwen.com/subject/vczdddtx.html