appium1:环境搭建
一、安装appium
相关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 安装node环境,如果没有安装
brew install node
# 查看node版本
node -v
npm -v
# 安装appium,可以是UI版本,也可以是命令行版本,我安装的事命令行版本,方便自动测试脚本运行
npm install -g appium
# 安装驱动
# 查看驱动列表
appium driver list
# 安装iOS的驱动
appium driver install xcuitest
# 安装Carthage,是否安装都可以,不影响
brew install carthage
# 安装appium-doctor
npm install -g doctor
# 安装代理,方便端口代理
brew install libimobiledevice
# 查看安装后的代理版本
iproxy --version
# 转发代理,方便运行WebDriverAgent运行起来后可以直接访问 http://localhost:8100/status
iproxy 8100 8100
# 安装Java,如果没有安装
brew install openjdk
# 安装Java环境变量
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
# 配置JAVA_HOME
nano ~/.zshrc # 对于 Zsh 用户
# 将如下2行写入文件最后
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
# 然高配置文件生效
source ~/.zshrc # 对于 Zsh 用户
nano ~/.bash_profile # 对于 Bash 用户
# 将如下2行写入文件最后
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
# 然高配置文件生效
source ~/.bash_profile # 对于 Bash 用户
# 查看JAVA_HOME
echo $JAVA_HOME
2、安装appium-inspector
https://github.com/appium/appium-inspector/releases
3、安装WebDriverAgent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 下载代码
git clone https://github.com/appium/WebDriverAgent.git
# 手动执行
# 使用xcode打开下载的WebDriverAgent.xcodeproj
# 如果是运行真机,需要配置自己账号的bundleId才能运行
# scheme选择WebDriverAgentRunner
# product > Test 或者快捷键 cmd + U
# 获取设备列表,找到udid
xcrun simctl list devices | grep -E "Booted|Available|Shutdown"
# 另一种查看的方式
xcrun xctrace list devices
# 另一种查看方式,查看真是失败
idevice_id -l | head -n1
# 脚本执行WebDriverAgent的test
xcodebuild -project \
/Users/macbookpro/Documents/study/studyAppium/WebDriverAgent/WebDriverAgent.xcodeproj \
-scheme WebDriverAgentRunner \
-destination 'id=75C8D299-E013-469C-8119-8C9AA12AEC14' \
test
# 下载libarclite_hpinesimulator.a
https://github.com/kamyarelyasi/Libarclite-Files/blob/main/libarclite_iphonesimulator.a
# 拷贝到这个路径下,如果没有arc文件夹,创建arc文件夹
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc
# 下载测试目标APP的demo
https://github.com/appium/ios-uicatalog
# 在模拟器上运行UICatlog项目
# 运行好后获取到生成的xxx.app, Product > Show build folder in finder > Products/Debug-iphonesimulator/UICatalog.app
正式运行看看效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 运行appium
appium
# 允许网页查看器访问的开启方式
appium --log-level debug --allow-cors
# 手动开启WebDriverAgentRunner的test cmd+u
# 脚本自动开启WebDriverAgentRunner的test的方式
# 获取设备列表,找到udid
xcrun simctl list devices | grep -E "Booted|Available|Shutdown"
# 防止端口占用
kill -9 $(lsof -t -i :8100)
# 脚本执行WebDriverAgent的test
# 启动指定的udid的设备,模拟器或真机
xcodebuild -project \
/Users/macbookpro/Documents/study/studyAppium/WebDriverAgent/WebDriverAgent.xcodeproj \
-scheme WebDriverAgentRunner \
-destination 'id=75C8D299-E013-469C-8119-8C9AA12AEC14' \
test
# 启动指定名称、指定系统的设备
xcodebuild -project /Users/macbookpro/Documents/study/studyAppium/WebDriverAgent/WebDriverAgent.xcodeproj \
-scheme WebDriverAgentRunner \
-destination "id=$(xcrun xctrace list devices | grep -E '^iPhone 15 Pro .*17\.0' | head -n 1 | awk '{print $NF}' | tr -d '()')" \
test
# 开启端口代理
iproxy 8100 8100
# 网页查看器地址
https://inspector.appiumpro.com/
# 使用本地安装的查看器
# 启动appium inspector
# 配置inspector
# 模拟器需要从编译的模拟器里拿
# /xxx/xx/Library/Developer/Xcode/DerivedData/xxx/Build/Products/Debug-iphonesimulator/xxx.app
# 真机要从真机里拿
# /xxx/xx/Library/Developer/Xcode/DerivedData/xxx/Build/Products/Debug-iphoneos/xxx.app
# 或真实的线上的ipa
{
"platformName": "iOS", # 测试的平台
"automationName": "XCUITest", # 测试的驱动
"platformVersion": "17.0", # 我自己模拟器对应的iOS版本
"deviceName": "iPhone 15 Pro", # 我模拟器的名称
"app": "/Users/macbookpro/Documents/study/studyAppium/UICatalog.app"
}
# 如果是真机,需要配置
# 启动不成功可能端口被占用
lsof -i :8100
kill -9 pidNum
# 合起来使用
kill -9 $(lsof -t -i :8100)
遇到不少离奇问题 1、运行时指定的是真机的udid,运行启动了模拟器 原因:key应该是udid:xxxx
, 被我写成了uuid:xxx
2、提示架构不一样,运行不了 原因:把模拟器的的product和真机的混用了,模拟器和真机是独立的
3、app字段配置说明 如果没有安装需要自动安装测试的app,则配置app: /xx/xx/xx.app
或app: /xx/xx/xx.ipa
的绝对路经 如果已经安装了,可以直接配置bundleId,app: xx.xx.xx
iOS驱动: https://github.com/appium/appium-ios-device https://github.com/appium/appium-ios-device/blob/master/lib/services.js https://github.com/appium/appium-ios-device/blob/master/lib/installation-proxy/index.js const INSTALLATION_PROXY_SERVICE_NAME = ‘com.apple.mobile.installation_proxy’; /Users/xxx/.appium/node_modules/appium-xcuitest-driver/node_modules/appium-ios-device
https://github.com/appium/appium-xcuitest-driver/blob/master/lib/driver.js https://github.com/appium/appium-xcuitest-driver/blob/master/lib/real-device-management.js https://github.com/appium/appium-xcuitest-driver/blob/master/lib/simulator-management.js
app-management.js -> mobileInstallApp -> simulator-management.js -> real-device.js -
device.installApp(app) -> this.install(appPath, bundleId, opts) -> async install(appPath, …) ios-fs-helpers.js -> const afcService = await services.startAfcService(this.udid); -> writeStream.write(source) -> installOrUpgradeApplication -> const installationService = await services.startInstallationProxyService(this.udid); -> installationService.installApplication(bundlePathOnPhone, clientOptions, timeout); ``` /—————+ | APPMANAGEMENT | +—————+/
mobileInstallApp = commands.appManagementExtensions.mobileInstallApp; mobileIsAppInstalled = commands.appManagementExtensions.mobileIsAppInstalled; mobileRemoveApp = commands.appManagementExtensions.mobileRemoveApp; mobileLaunchApp = commands.appManagementExtensions.mobileLaunchApp; mobileTerminateApp = commands.appManagementExtensions.mobileTerminateApp; mobileActivateApp = commands.appManagementExtensions.mobileActivateApp; mobileKillApp = commands.appManagementExtensions.mobileKillApp; mobileQueryAppState = commands.appManagementExtensions.mobileQueryAppState; installApp = commands.appManagementExtensions.installApp; activateApp = commands.appManagementExtensions.activateApp; isAppInstalled = commands.appManagementExtensions.isAppInstalled; // @ts-ignore it must return boolean terminateApp = commands.appManagementExtensions.terminateApp; queryAppState = commands.appManagementExtensions.queryAppState; mobileListApps = commands.appManagementExtensions.mobileListApps; mobileClearApp = commands.appManagementExtensions.mobileClearApp; ```