版权声明:本文为博主原创文章,未经博主允许不得转载。如有问题,欢迎指正。 https://blog.csdn.net/bibi1003/article/details/87182731
总体流程
-
环境搭建
-
准备一个简单的APP在IOS上
-
编写Python脚本
-
启动Appium和Python脚本开始自动化测试
1. 环境搭建 详见:https://testerhome.com/topics/8375
2. 准备一个简单的APP在IOS上:https://jingyan.baidu.com/article/a17d5285c9afc48099c8f279.html
其中,过程中会产生bundle identifier
3. 编写Python脚本(PyCharm):
appium启动配置,参见:https://blog.csdn.net/bibi1003/article/details/85066887
获得app元素的属性:启动appium,配置好参数,点击start inspector session-> 会出现左侧是app页面,中间和右边是对应属性栏,可获得如xpath,name等的属性值,可直接通过这些值find elements
然后使用Python的unitest框架提供的TestSuite类及其中方法编写test cases
# coding=utf-8
from appium import webdriver
import time
import unittest
class Tests(unittest.TestCase):
#启动app
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'ios' # 设备系统
desired_caps['platformVersion'] = '12.0.1' # 设备系统版本
desired_caps['deviceName'] = 'iphone XR' # 设备名称
desired_caps['bundleId'] = 'Y.TestProduct' # 测试app包名
desired_caps['udid'] = 'B7416A87-6C4A-476E-BF03-4EAD8A272304'
desired_caps['automationName'] = 'XCUITest' # 测试appActivity
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
#定义多个test cases,比如查看页面元素
def testCase1_findElements(self):
driver = self.driver
textWorld = driver.find_element_by_xpath('//XCUIElementTypeStaticText[@name="world"]').text
textaaa = driver.find_element_by_xpath('//XCUIElementTypeApplication[@name="TestProduct"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeTextField')
time.sleep(2)
assert textWorld=="world", print('no word of world')
#比如点击按钮,页面会有元素改变
def testCase2_ButtonChange(self):
driver = self.driver
# Click the button to change the content of the label and the text
button = driver.find_element_by_xpath('//XCUIElementTypeButton[@name="Button"]')
button.click()
time.sleep(2)
textWorld = driver.find_element_by_xpath('//XCUIElementTypeStaticText[@name="world"]').text
textaaa = driver.find_element_by_xpath('//XCUIElementTypeApplication[@name="TestProduct"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeTextField')
# 添加断言,若label和text没有被改变或正确地被改变,则打印错误信息try:
assert textWorld == 'changed label', print('The label has not been changed by button')
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(Tests('testCase1_findElements'))
suite.addTest(Tests('testCase2_ButtonChange'))
unittest.TextTestRunner(verbosity=2).run(suite)
#一种简单打印测试报告的方式,执行数,成功数,失败数
4. 启动Appoium和Python脚本开始自动化测试
启动安装的app Appium->运行python脚本,simulator模拟器会被启动,会有测试结果在PyCharm中输出。