python+appium+iOS 搭建Mac环境
可以参考以下几个链接内的搭建过程
第一次做ios的自动化挺折磨人的 wda的安装尽量找ios开发来协助几分钟的事自己捣鼓需要一阵子。环境搭建完成之后就是定位方式的不同了。
感谢文章内所有链接的原博主!!!
最好是先有个mac环境的电脑在进行ios测试
windows搭建ios环境尝试之后无果,有成功案例可以发在评论区或者私信一下 共同进步!
https://blog.csdn.net/AI_Green/article/details/132482614 mac下搭建
https://blog.csdn.net/u011466469/article/details/133135070 win虚拟机内搭建
定位工具
weditor这个工具安卓和ios都可以使用。
https://blog.csdn.net/m0_67695717/article/details/130893108
由网易开发的Airtest的安装、配置、使用教程
https://blog.csdn.net/baobei0921/article/details/131779515
https://www.jianshu.com/p/43e7bab0260d
appium自带的Appium Inspector
https://blog.csdn.net/crayonjingjing/article/details/125750591
iOS自动化测试元素定位
以下链接都可以参考自身来使用
https://blog.csdn.net/wangmcn1984/article/details/78963178
https://blog.csdn.net/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()