You are on page 1of 7

iOS 自动化测试

学习目标:
1. 能够搭建 iOS 自动化测试所需要的环境
2. 能够使用模拟器进行 iOS 自动化测试
3. 能够使用真机进行 iOS 自动化测试

一. 环境搭建
应用场景

想要进行 iOS 自动化测试,必须进行环境的搭建。

需要的环境


macOS 系统电脑
10.13.6


Xcode

10.1
打开 app store 搜索 Xcode 并下载


待测试的 iOS 项目

找开发人员
-黑

appium Desktop

1.10

https://github.com/appium/appium-desktop/releases/download/v1.10.0/Appium-1.10.
0.dmg

python
3.6.1

https://www.python.org/ftp/python/3.6.1/python-3.6.1-macosx10.6.pkg

pycharm

Professional 2018.2.2
https://www.jetbrains.com/pycharm/download

node.js
10.5.0
https://npm.taobao.org/mirrors/node/v10.15.0/node-v10.15.0.pkg

cnpm
5.2.0

npm install -g cnpm --registry=https://registry.npm.taobao.org

ios-deploy 依赖库

cnpm install -g ios-deploy

brew

黑马程序员-软件测试
1.9.1

ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)

libimobiledevice 依赖库

brew install --HEAD libimobiledevice

可能出现的问题

报错:Requested 'libusbmuxd >= 1.1.0' but version of libusbmuxd is 1.0.10

解决方法:

brew update
brew uninstall --ignore-dependencies libimobiledevice
brew uninstall --ignore-dependencies usbmuxd
brew install --HEAD usbmuxd


brew unlink usbmuxd
brew link usbmuxd


brew install --HEAD libimobiledevice

carthage 依赖库


brew install carthage
-黑

iOS 系统手机

iOS 12.1.2
Apple ID

https://appleid.apple.com/account#!&page=create

WebDriverAgent

https://github.com/facebook/WebDriverAgent

点击 download zip
配置 WebDriverAgent 的内容会在真机自动化是进行讲解

二. 使用模拟器进行自动化测试
2.1 运行 iOS 程序到模拟器

步骤

1. 使用 Xcode 打开要运行的程序
2. 选择将要运行的 程序 和 模拟器设备
3. 快捷键 command + r 运行

2.2 查看 iOS 元素特征

步骤

1. 打开 appium

2. 启动 appium
3. 左上角菜单栏选择 appium - new session window

黑马程序员-软件测试




-黑

4. 填写对应的启动参数,并启动



2.3 编写和运行自动化脚本

前置代码

黑马程序员-软件测试
from appium import webdriver

desired_caps = dict()
desired_caps['platformName'] = 'iOS'
desired_caps['platformVersion'] = '12.1'
desired_caps['deviceName'] = 'iPhone 8'
desired_caps['app'] = 'com.itcast.HMiOSTest'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

需求

1. 点击按钮 进入列表
2. 获取所有列表的文字内容并输入
3. 滑动一次
4. 点击 20
5. 清空文本框内容


6. 在文本框中输入 “hello”
7. 单击返回按钮


核心代码

time.sleep(1) 程

driver.find_element_by_id("进入列表").click()
-黑

time.sleep(1)

eles = driver.find_elements_by_class_name("XCUIElementTypeStaticText")
for i in eles:

print(i.text)

time.sleep(1)

# 无法使用

# driver.swipe(100, 2000, 100, 1000)


driver.execute_script("mobile: swipe", {"direction": "up"})

time.sleep(1)

driver.find_element_by_xpath("//*[@name='20']").click()

time.sleep(1)

text_view = driver.find_element_by_class_name("XCUIElementTypeTextField")
text_view.clear()

time.sleep(1)

text_view.send_keys("hello!!!")

time.sleep(1)

driver.find_element_by_xpath("//XCUIElementTypeButton[@name='Back']").click()

黑马程序员-软件测试
time.sleep(1)

注意点

1. 官方已经明确表示 swipe 等 API 在模拟器 7.x - 8.x 中无法使用,真机并未提及。

网上有人说真机并没有这个问题,但并没有表明版本。真机 iOS 12.1.12 实测不行。

本人曾经使用过 Appium 1.8 + 模拟器和真机的 iOS 11 是可以使用的。

工作中,如果碰到这种情况,就用 execute_script 的方法实现即可

2. 在模拟器中可以连接电脑的键盘进行输入,但是如果连接,appium 将无法工作,所以需要
消选 Connect Hardware Keyboard

选中模拟器 - 左上角 Hardware - Keyboard - 消选 Connect Hardware Keyboard





-黑



三. 使用真机进行自动化测试
3.1 运行 iOS 程序到真机

步骤

1. 在 Xcode 中登录自己的 Apple ID


2. 配置开发者信息
3. 选择将要运行的 程序 和 设备
4. 快捷键 command + r 运行
5. 在手机中进入 设置 - 通用 - 设备管理 - 自己Apple ID - 信任程序
6. 重新 command + r 运行

3.2 配置 WebDriverAgent

步骤

黑马程序员-软件测试
1. 进入到下载的 WebDriverAgent 项目下

2. 输入命令 ./Scripts/bootstrap.sh

3. 启动 WebDriverAgent.xcodeproj

4. 配置 WebDriverAgentLib 的开发者信息



5. 配置 WebDriverAgentRunner 的开发者信息

6. 配置 IntegrationApp 的开发者信息

7. 修改 WebDriverAgentRunner 的 Product Bundle Identifier

-黑



8. 修改 IntegrationApp 的 Product Bundle Identifier


9. 数据线连接真机

10. 选择将要运行的 WebDriverAgentRunner 和 真机设备

11. 使用 command + u 运行

稍等之后会在log中出来一个 url 地址
在浏览器中打开这个地址,如果显示一串 json 数据即为正确连接手机
并且,真机会多一个程序
12. 将配置好的 WebDriverAgent 项目替换到 appium 的 WebDriverAgent 项目
打开 finder
快捷键 command + shift + g

黑马程序员-软件测试
输入路径 /Applications/Appium.app/Contents/Resources/app/node_modules/appium-
xcuitest-driver
回车
将旧项目换个名字,当做备份
将配置好的项目放在这个目录下

3.3 运行自动化脚本

步骤

1. 修改对应的 platformVersion、deviceName
2. 查看 udid 并增加为启动参数
3. 运行即可

附录. 参考资料
https://gochannel.org/links/link/snapshot/4307


https://testerhome.com/topics/6962

https://blog.csdn.net/weixin_35825766/article/details/80655296


https://blog.csdn.net/yjs_13597468160/article/details/80635761

https://testerhome.com/topics/14911

https://www.jianshu.com/p/1ec36db7d551


https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.
-黑

md#bugs-and-workarounds



黑马程序员-软件测试

You might also like