IOS自动化

hzf666 / 2023-08-31 / 原文

1-环境搭建

  • 需要的环境
    image
    image
    image
    image

2-使用模拟器进行自动化测试

2.1 运行IOS程序到模拟器

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

2.2查看IOS元素特征

  • 步骤
    1. 打开 appium
    2. 启动 appium
    3. 左上角菜单栏选择 appium - new session window
      image
      4.填写对应的启动参数,并启动
      image

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 中无法使用,真机并未提及。(Appium 1.8 + 模拟器和真机的 iOS 11 是可以使用的。)工作中,如果碰到这种情况,就用 execute_script 的方法实现即可。
  2. 在模拟器中可以连接电脑的键盘进行输入,但是如果连接,appium 将无法工作,所以需要消选 Connect Hardware Keyboard选中模拟器 - 左上角 Hardware - Keyboard - 消选 Connect Hardware Keyboard
    image

3、使用真机进行自动化测试

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 的开发者信息
    image
  5. 配置 WebDriverAgentRunner 的开发者信息
  6. 配置 IntegrationApp 的开发者信息
  7. 修改 WebDriverAgentRunner 的 Product Bundle Identifier
    image
  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/appiumxcuitest-driver
    回车
    将旧项目换个名字,当做备份
    将配置好的项目放在这个目录下

3.3运行自动化脚本

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