0
点赞
收藏
分享

微信扫一扫

基于Appium的ios自动化教程


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

01Appium实现iOS自动化测试

01、启动应用

填写 capability信息

基于Appium的ios自动化教程_单元测试

app 获取

基于Appium的ios自动化教程_程序人生_02

uuid获取

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

基于Appium的ios自动化教程_单元测试_03

02、元素获取

通过代码开启,pycharm编写

from time import sleep
from appium import webdriver
caps = {}
#平台版本
caps["platformName"] = "iOS"
# APP信息通过xcode
caps["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)
# 等待5s
driver.implicitly_wait(5)

基于Appium的ios自动化教程_单元测试_04

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

from time import sleep
from appium import webdriver
caps = {}
# 平台版本
caps["platformName"] = "iOS"
# APP信息通过xcode
caps["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)
# 等待5s
driver.implicitly_wait(5)
#点击Action Sheets
driver.find_element_by_xpath(**'(//XCUIElementTypeButton[@name="More Info"]) [1]'**).click()
#点击ok
driver.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 -- 点击输入框 --点击输入内容

基于Appium的ios自动化教程_单元测试_05

2、pycharm设置默认执行器

基于Appium的ios自动化教程_程序人生_06

代码实现:

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)



举报

相关推荐

0 条评论