Appium作为一个开源的、跨平台的自动化测试工具,适用于测试原生或混合型移动App,它使用WebDriver协议驱动IOS,Android和Windows应用程序,本篇文章介绍实现ios自动化测试

Appium实现iOS自动化测试

01 启动应用

填写 capability信息

app 获取

uuid获取

点击Window—->Devices—>在右侧可查看到identifier identifier,即为我们获取到的iPhone 的uuid

元素获取

通过代码开启,pycharm编写

from time import sleepfrom appium import webdrivercaps ={}#平台版本caps["platformName"]="iOS"# APP信息通过xcodecaps["app"]="Users/hanxingyuan/Library/Developer/Xcode/DerivedData/UICatalog- elvxjsgcreylppcxqfmmfzwuujpo/Build/Products/Debug-iphonesimulator/UICatalog.app"#设备名称caps["deviceName"]="iPhone X"#设备版本caps["platformVersion"]="12.1"#uuid 通过xcode获取caps['uuid']='4A8743D2-501D-42B6-A20D-14901A5BE61B'#创建driver对象driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)# 等待5sdriver.implicitly_wait(5)

需求:第一个脚本,点击Action Sheets – 点击 OK

from time import sleepfrom appium import webdrivercaps ={}# 平台版本caps["platformName"]="iOS"# APP信息通过xcodecaps["app"]="Users/hanxingyuan/Library/Developer/Xcode/DerivedData/UICatalog- elvxjsgcreylppcxqfmmfzwuujpo/Build/Products/Debug-iphonesimulator/UICatalog.app"#设备名称caps["deviceName"]="iPhone X"#设备版本caps["platformVersion"]="12.1"#uuid 通过xcode获取caps['uuid']='4A8743D2-501D-42B6-A20D-14901A5BE61B'#创建driver对象driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)# 等待5sdriver.implicitly_wait(5)#点击Action Sheetsdriver.find_element_by_xpath(**'(//XCUIElementTypeButton[@name="More Info"]) [1]'**).click()#点击okdriver.find_element_by_xpath(**'//XCUIElementTypeStaticText[@name="Okay / Cancel"]'**).click()

1、ios定位方法

ios_predicate

iOS 的 UI 自动化中,使用原生支持的Predicate定位方式是最好,可支持元素的单个属性和多个属性定位,强烈推荐使用driver.find_element_by_ios_predicate(“value == ‘ClearEmail’”)driver.find_element_by_ios_predicate(“type == ‘’ AND value == ‘’)

accessibility_id

替代以前的name定位方式,在 iOS 上,主要使用元素的label或name(两个属性的值都一样)属性进行定位,如该属性为空,也是不能使用该属性。driver.find_element_by_accessibility_id(‘ClearEmail’)

xpath

由于 iOS 10开始使用的 XCUITest 框架原生不支持,定位速度很慢,所以官方现在不推荐大家使用,也有其他替代的定位方式可使用。

使用绝对路径定位driver.find_element_by_xpath(’/XCUIElementTypeApplication/XCUIElementTypeButton’)使用相对路径定位:driver.find_element_by_xpath(’//XCUIElementTypeButton’)通过元素的索引定位driver.find_element_by_xpath(’//XCUIElementTypeButton[index]’)通过元素的属性定位driver.find_element_by_xpath(”//className[@value=‘ClearEmail’]")

iOSNsPredicateString

仅支持 iOS 10或以上,可支持元素的单个属性和多个属性定位,推荐使用。一种属性:MobileBy.iOSNsPredicateString(“type == ‘XCUIElementTypeButton’”)两种属性:MobileBy.iOSNsPredicateString(“type == ‘XCUIElementTypeButton’ AND label== ‘更多信息’”)

以上定位方式基本同Android一致,ios专项定位方式PredicateString,需求:点击 search Bars – 点击Default – 点击输入框 –点击输入内容

2、pycharm设置默认执行器

代码实现:

from appium import webdriver import timefrom selenium.webdriver.support.ui import WebDriverWait # from selenium import webdriverclass TestDemo:def setup(self):caps ={}caps["platformName"]="iOS"caps["app"]="Users/hanxingyuan/Library/Developer/Xcode/DerivedData/UICatalog- elvxjsgcreylppcxqfmmfzwuujpo/Build/Products/Debug-iphonesimulator/UICatalog.app"# # caps["automationName"]="xcuitest"caps["deviceName"]="iPhone X"caps["platformVersion"]="12.1"caps['uuid']='4A8743D2-501D-42B6-A20D-14901A5BE61B'caps['startIWDP']= Trueself.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)self.driver.implicitly_wait(5)# 滑动方法def swipe_view(self):size = self.driver.get_window_size() self.driver.swipe(size['width']*0.5, size['height']*0.75,size['width']*0.75, size['height']*0.25,3000)def test_search(self):self.swipe_view()点击 search Barsself.driver.find_element_by_accessibility_id('SearchBars').click()# 点击Defaultself.driver.find_element_by_accessibility_id('Default').click()# ios 10 以上操作系统支持 type =="XCUIElementTypeSearchField" #点击输入框self.driver.find_element_by_ios_predicate('type == "XCUIElementTypeSearchField"').click()time.sleep(4)点击输入内容self.driver.find_element_by_accessibility_id('L').click()time.sleep(4)self.driver.find_element_by_accessibility_id('g').click()time.sleep(4)print(self.driver.find_element_by_ios_predicate('value == "Lg"').text)

最后: 下方这份完整的软件测试视频学习教程已经整理上传完成,朋友们如果需要可以自行免费领取【保证100%免费】

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!