文章目录
- [一、运行 iOS 程序到模拟器](#一、运行 iOS 程序到模拟器)
- [二、查看 iOS 元素特征](#二、查看 iOS 元素特征)
- 三、编写和运行自动化脚本
-
- [3.1 前置代码](#3.1 前置代码)
- [3.2 需求](#3.2 需求)
- [3.3 需求-示例](#3.3 需求-示例)
- 四、注意点
一、运行 iOS 程序到模拟器
1、使用 Xcode 打开要运行的程序。( Xcode 打开要测试的项目)
2、选择将要运行的 程序 和 模拟器设备。( HMiOSTest 和 iphone8)
3、快捷键 command + r 运行。(或者点击播放按钮: ▶) 在Xcode界面运行
实际上这个ios模拟器属于Xcode的一部分。
二、查看 iOS 元素特征
1、打开 appium
2、启动 appium
3、左上角菜单栏选择 appium --->new session window...
4、填写对应的启动参数,并启动。

三、编写和运行自动化脚本
3.1 前置代码
python
from appium import webdriver
desired_caps = dict()
desired_caps['platformName'] = 'iOS'
desired_caps['platformVersion'] = '12.1' # ios系统版本
desired_caps['deviceName'] = 'iPhone 8' # 这里面不能随便写,安卓可以
desired_caps['app'] = 'com.itcast.HMiOSTest' # 相当于安卓app的包名
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

3.2 需求
-
点击按钮 进入列表
-
获取所有列表的文字内容并输入
-
滑动一次
-
点击 20
-
清空文本框内容
-
在文本框中输入 "hello"
-
单击返回按钮
3.3 需求-示例
python
import time
from appium import webdriver
desired_caps = dict()
desired_caps['platformName'] = 'iOS'
desired_caps['platformVersion'] = '12.1.2'
desired_caps['deviceName'] = 'iPhone 6s Plus'
desired_caps['app'] = 'com.itcast.HMiOSTest'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 1、点击 进入列表
driver.find_element_by_id("进入列表").click()
# 2、在控制台输出所有的文字内容
"""
# 在 android 中只会查找到屏幕上所显示的控件
# 在 iOS 中都会找到控件,但是如果进行操作的时候,也同样需要展示在屏幕上:比如 4、点击20这个元素
"""
text_views = driver.find_elements_by_class_name("XCUIElementTypeStaticText")
for text_view in text_views:
print(text_view.text)
# 3、滑动一次
# driver.swipe(100, 2000, 100, 1000) # 这个方法有的情况不好用,时灵时不灵,用下面的方法
driver.execute_script("mobile: swipe", {"direction": "up"})
# 4、点击 20
driver.find_element_by_xpath("//*[@name='20']").click()
# 5、清空文本框
text_field = driver.find_element_by_class_name("XCUIElementTypeTextField")
text_field.clear()
# 6、输入 hello
text_field.send_keys("hello!!!")
# 7、点击 返回back
driver.find_element_by_xpath("//*[@name='Back']").click()
time.sleep(5)
driver.quit()
四、注意点
1、官方已经明确表示 swipe 等 API 在模拟器 7.x - 8.x 中无法使用,真机并未提及。
- 网上有人说真机并没有这个问题,但并没有表明版本。
- 真机 iOS 12.1.12 实测不行。
- 本人曾经使用过 Appium 1.8 + 模拟器和真机的 iOS 11 是可以使用的。
- 工作中,如果碰到这种情况,就用 execute_script 的方法实现即可。
- https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.md#bugs-and-workarounds
2、在IOS模拟器中可以连接电脑的键盘进行输入,但是如果连接,appium 将无法工作,所以需要消选 Connect Hardware Keyboard
方法:选中模拟器---> 左上角 Hardware---> Keyboard --->消选 Connect Hardware Keyboard
