每天进步一点点,关注我们哦,每天分享测试技术文章
本文章出自【码同学软件测试】
码同学公众号:自动化软件测试
码同学抖音号:小码哥聊软件测试
Appium作为一个开源的、跨平台的自动化测试工具,适用于测试原生或混合型移动App,它使用WebDriver协议驱动IOS,Android和Windows应用程序,本篇文章介绍实现ios自动化测试
01
Appium实现iOS自动化测试
01
启动应用
填写 capability信息
app 获取
uuid获取
点击Window---->Devices--->在右侧可查看到identifier identifier,即为我们获取到的iPhone 的uuid
02
元素获取
通过代码开启,pycharm编写
from time import sleepfrom appium import webdriver
caps = {}
#平台版本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)
免费领取 码同学软件测试 课程笔记+超多学习资料+完整视频+最新面试题,可以转发文章 + 私信「码同学666」获取资料哦
需求:第一个脚本,点击Action Sheets -- 点击 OK
from time import sleepfrom appium import webdriver
caps = {}
# 平台版本
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 time
from selenium.webdriver.support.ui import WebDriverWait
# from selenium import webdriver
class 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'] = True
self.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 Bars self.driver.find_element_by_accessibility_id('Search Bars').click()
# 点击Default
self.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)
免费领取码同学软件测试课程笔记+超多学习资料+学习完整视频,可以关注我们公众号哦:自动化软件测试
本文著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。