python+appium+iOS 搭建Mac环境

可以参考以下几个链接内的搭建过程

第一次做ios的自动化挺折磨人的 wda的安装尽量找ios开发来协助几分钟的事自己捣鼓需要一阵子。环境搭建完成之后就是定位方式的不同了。
感谢文章内所有链接的原博主!!!

最好是先有个mac环境的电脑在进行ios测试
windows搭建ios环境尝试之后无果,有成功案例可以发在评论区或者私信一下 共同进步!
https://blog.youkuaiyun.com/AI_Green/article/details/132482614 mac下搭建
https://blog.youkuaiyun.com/u011466469/article/details/133135070 win虚拟机内搭建

定位工具

weditor这个工具安卓和ios都可以使用
https://blog.youkuaiyun.com/m0_67695717/article/details/130893108

由网易开发的Airtest的安装、配置、使用教程
https://blog.youkuaiyun.com/baobei0921/article/details/131779515
https://www.jianshu.com/p/43e7bab0260d

appium自带的Appium Inspector
https://blog.youkuaiyun.com/crayonjingjing/article/details/125750591

iOS自动化测试元素定位

以下链接都可以参考自身来使用
https://blog.youkuaiyun.com/wangmcn1984/article/details/78963178
https://blog.youkuaiyun.com/okcross0/article/details/135771688
https://www.ewbang.com/community/article/details/997898119.html
一些定位方法内的value可以删除不用,不在意运行速度的话可以统统用xpath大法!

1、accessibility_id
iOS自动化测试元素定位的accessibility_id主要使用元素的label或name(两个属性的值都一样)属性进行定位。

driver.find_element_by_accessibility_id("我的功能")
driver.find_element(MobileBy.ACCESSIBILITY_ID,  "我的功能")
driver.find_element(by=AppiumBy.ACCESSIBILITY_ID,  "我的功能")
driver.find_element(AppiumBy.ACCESSIBILITY_ID,"我的功能"')

2、class_name
class_name定位方法使用元素的type属性,type属性表示控件类型,一般不具有唯一性,因此class_name不常用。

例如:type属性为:XCUIElementTypeButton

driver.find_element_by_class_name("XCUIElementTypeButton")
driver.find_element(MobileBy.CLASS_NAME, "XCUIElementTypeButton")
driver.find_element(by=AppiumBy.CLASS_NAME,  "XCUIElementTypeButton")
driver.find_element(AppiumBy.CLASS_NAME,  "XCUIElementTypeButton")

3、Xpath
Appium对app原生环境的xpath定位方法执行效率很低,从iOS 10开始使用的 XCUITest 框架原生不支持,定位速度很慢,官方不推荐这种方式。但是在实际使用中,当其他定位方式都不能找到元素时,可以尝试xpath定位。

driver.find_element_by_xpath("//XCUIElementTypeStaticText[@name='我的功能']")
driver.find_element(MobileBy.XPATH, "//XCUIElementTypeStaticText[@name='我的功能']")
driver.find_element(by=AppiumBy.XPATH, "//XCUIElementTypeStaticText[@name='我的功能']")
driver.find_element(AppiumBy.XPATH, "//XCUIElementTypeStaticText[@name='我的功能']")

4、ios_class_chain(类型链)
ios_class_chain仅支持iOS 10或以上,且仅限于WebDriverAgent 框架中使用。此方法用于替代xpath,但该方法还有待完善,没有纳入官方文档。

Github说明地址:https://github.com/appium/appium-xcuitest-driver

driver.find_element_by_ios_class_chain('**/XCUIElementTypeStaticText[`label == "我的功能"`]')
driver.find_element(MobileBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeStaticText[`label == "我的功能"`]')
driver.find_element(by=AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeStaticText[`label == "我的功能"`]')
driver.find_element(AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeStaticText[`label == "我的功能"`]')

5、ios_predicate(谓词)
iOS Predicate 即谓词逻辑。ios_predicate定位方法支持iOS所有的版本,也就是能适配XCUITest 和 UIAutomation底层测试框架,使用的就是iOS编程语言,因此可以把此方法作为首选定位方式。

谓词表达式由属性、运算符和值构成。

1.1、常用的属性

在上面的表格中已经介绍,可以使用的元素属性:type、value、name、label、enabled、visible

1.2、运算符

1.2.1、比较运算符

比较运算符

, <, ==, >=, <=, !=

可用于数值和字符串的比较,如:label == ‘我的功能’,label >= 500

示例:

driver.find_element_by_ios_predicate("label == '我的功能'")
driver.find_element(MobileBy.IOS_PREDICATE, "label == '我的功能'")
driver.find_element_by_ios_predicate("type == 'XCUIElementTypeButton' AND value == 'ClearEmail'")

启动参数

有不用单独启动命令启动的方法可以发在评论区或者私信一下 共同进步!
通过wda链接手机的命令,需要命令行运行。“wda包名”就是搭建环境时的起的名字。
启动wda链接手机后会有个遮罩层 “Automation Running”类的字样

tidevice -u 手机的udid xctest -B wda包名

然后就可以启动driver启动对应的app了

// An highlighted block
# This sample code supports Appium Python client >=2.3.0
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver
from appium.options.common.base import AppiumOptions
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

options = AppiumOptions()
options.load_capabilities({
	"platformName": "ios",
	"appium:platformVersion": "15.4.1",
	"appium:deviceName": "iPhone Xs Max",
	"appium:udid": "手机的udid",
	"appium:bundleId": "测试app的包名",
	"appium:webDriverAgentUrl": "wda的启动后所带的地址",
	"appium:automationName": "XCUITest",
	"appium:includeSafariInWebviews": True,
	"appium:connectHardwareKeyboard": True,
	"appium:newCommandTimeout": 3600
})

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=options)


driver.quit()
### 配置 PythonAppium 进行 iOS 移动应用自动化测试 #### 安装 Homebrew 为了简化软件包管理,在 macOS 上推荐先安装 Homebrew。Homebrew 是一个开源的软件包管理系统,可以方便地安装各种工具。 ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` #### 安装 Java Development Kit (JDK) 部分依赖项可能需要 JDK 支持: ```bash brew install adoptopenjdk/openjdk/adoptopenjdk8 ``` #### 设置环境变量 编辑 `~/.zshrc` 或者 `~/.bash_profile` 文件来设置 JAVA_HOME 变量: ```bash export JAVA_HOME=$(/usr/libexec/java_home) ``` #### 安装 Node.js 和 npm Node.js 提供了运行 JavaScript 的环境,npm 则用于管理和分发 JavaScript 库和应用程序。 ```bash brew install node ``` #### 安装 Appium Server 通过 npm 来全局安装最新版本的 Appium server: ```bash npm install -g appium@latest ``` #### 启动 Appium Server 可以通过命令行启动 Appium 服务: ```bash appium & ``` #### 安装 Python 和 pip 确保已安装 Python 并更新 pip 工具到最新版本: ```bash brew install python pip3 install --upgrade pip setuptools wheel ``` #### 安装 Appium-Python-Client 这是官方支持的 Python 绑定库,允许使用 Python 编写基于 Appium 的自动化脚本。 ```bash pip3 install Appium-Python-Client ``` #### 准备 iOS 测试设备连接参数 对于 iOS 设备而言,需准备特定的配置信息以便于与真实设备建立连接[^2]: ```json { "appium:bundleId": "com.mc.ts", "appium:automationName": "XCUITest", "appium:platformVersion": "16.2", "appium:udid": "00008020-001238442EEA002E", "platformName": "iOS", "appium:deviceName": "iPhone Simulator" } ``` 这些参数中的 bundle ID 可以通过终端命令获取: ```bash ideviceinstaller -l ``` #### 创建简单的测试案例 编写一个基本的 Python 脚本来验证环境是否正常工作: ```python from appium import webdriver desired_caps = { 'platformName': 'iOS', 'appium:automationName': 'XCUITest', 'appium:bundleId': 'com.example.app', # 替换成实际的应用程序 Bundle Id 'appium:udid': 'your_device_udid_here' # 使用真实的 UDID 值替换此处 } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) try: element = driver.find_element_by_accessibility_id("Some Element") # 查找元素的例子 finally: driver.quit() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值