您的位置:首页 > 移动开发

Appium+XCUITest基于Python的操作实例以及环境搭建

2017-03-14 00:00 806 查看
该教程是通过dmg方式安装

关于在mac上安装appium、python的相关安装包链接:http://pan.baidu.com/s/1skUpF6t 密码:gyia

在连接下载相关安装包,dmg安装报操作相对比较见到。

依次安装appium、python、Sublime text。

python安装成功,安装pip工具,用于安装相关的python库文件。执行的命令如下:

sudo easy_install pip

然后安装appium-python-client,在终端窗口输入命令:

sudo pip install Appium-Python-Client

当然你也可以安装selenium,区别只是appium-python-client自带的方法比selenium的方法要多:

sudo pip install selenium
如何检验appium和selenium模块是否安装成功,执行如下命令:

python
进入到python窗口执行命令:

import appium
import selenium
如果安装成功,没有相关错误信息提示:

其次,解压WebDriverAgent,进入到WebDriverAgent-master目录下,执行命令:

sh ./Scripts/bootstrap.sh
安装完成,如果出现问题,可以参考链接:
https://testerhome.com/topics/4904,关于侦测器,我的安装包里面有一个web版的侦测器,安装没有那么复杂。相关链接:
https://github.com/mykola-mokhnach/Appium-iOS-Inspector

启动WebDriverAgent命令如下:

xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination 'id=uuid真机' test
启动
WebDriverAgent可以参考:
https://github.com/facebook/WebDriverAgent/wiki/Starting-WebDriverAgent

如果以上环境都配置ok了,那么就可以开始一个简单的操作实例,在我提供的下载路径下的XCUITest.rar文件是我从网上下载的XUITest的测试例子:

测试机:iphone 6s

ios版本:10.1.1

以XCUITest\examples\python中的ios_simple.py为例:

"""
Simple iOS tests, showing accessing elements and getting/setting text from them.
"""
import unittest
import os
from random import randint
from appium import webdriver
from time import sleep

class SimpleIOSTests(unittest.TestCase):

def setUp(self):
# set up appium
app = os.path.abspath('../../apps/TestApp/build/release-iphonesimulator/TestApp.app')
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'iOS',
'platformVersion': '10.1.1',#修改为当前的版本号
'deviceName': 'iPhone 6s',#修改为当前的机型
'uuid': '42b9d3af431ac1d5af95018f84cdf525e97089f7'  #uuid
})

def tearDown(self):
self.driver.quit()

def _populate(self):
# populate text fields with two random numbers
els = [self.driver.find_element_by_accessibility_id('TextField1'),
self.driver.find_element_by_accessibility_id('TextField2')]

self._sum = 0
for i in range(2):
rnd = randint(0, 10)
els[i].send_keys(rnd)
self._sum += rnd

def test_ui_computation(self):
# populate text fields with values
self._populate()

# trigger computation by using the button
self.driver.find_element_by_accessibility_id('ComputeSumButton').click()

# is sum equal ?
# sauce does not handle class name, so get fourth element
sum = self.driver.find_element_by_accessibility_id('Answer').text
self.assertEqual(int(sum), self._sum)

def test_scroll(self):
els = self.driver.find_elements_by_class_name('XCUIElementTypeButton')
els[5].click()

sleep(1)
try:
el = self.driver.find_element_by_accessibility_id('Allow')
el.click()
sleep(1)
except:
pass

el = self.driver.find_element_by_xpath('//XCUIElementTypeMap[1]')

location = el.location
self.driver.swipe(start_x=location['x'], start_y=location['y'], end_x=0.5, end_y=location['y'], duration=800)

if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(SimpleIOSTests)
unittest.TextTestRunner(verbosity=2).run(suite)


测中用的unittes框架,可以通过pip命令安装:
pip install unittest


测试用例准备完毕!

第一步,启动WebDriverAgent

第二步,在appium配置好端口号,上面的例子是localhost:4723,默认的,只需要启动就行,或者通过命令行

appium -a 127.0.0.1 -p 4723 -U '45f082689dbaebb0ffa3620b3ae22ad9faff9a30'
如果命令行带有-U,脚本中需要注释掉uuid,不然会报错

第三步,计入到Appium-iOS-Inspector-master目录下,点击.HTML的文件,启动服务

按照上述的测试用例更改属性值,来提样下XCUITest+appium+python。

通过Sublime text运行测试用例:command+b

启动手机上的要测试的服务,就可以查看页面的元素,通过元素定位来操作。由于appium版本过高,ios测试中不在用uiautomator,使用xcuitest。通过操作其实是简化了操作,可扩展性更强,移植性更好,使用XCUITest的优点如下:

1.可以直接调用selenium中元素定位方法,不在使用find_element_by_ios_uiautomation方法,使用更便捷

2.可以移植性更强

3.测试用例的重用性更高了

4.类似于web的自动化测试流程,性能更好
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: