对测试用例的编写我们采用Python + Behave的方案。
behave是一款行为驱动开发(BDD)的测试框架,配合python用以进行测试用例的编写。接下来我们开始安装behave。
安装Behave
- 利用pip3安装Appium-Python-Client,作用是让Appium支持Python的环境。
pip3 install Appium-Python-Client
- 安装Behave
pip3 install behave
- 查看Behave支持的语言
behave --lang-list
- 查看对应语言的关键字
behave --lang-help zh-CN
Translations for Chinese simplified / 简体中文
And: 而且<
Background: 背景
But: 但是<
Examples: 例子
Feature: 功能
Given: 假如<
Scenario: 场景
Scenario Outline: 场景大纲
Then: 那么<
When: 当<
Behave安装完毕之后我们开始编写测试脚本。
自动化测试运行方式一:通过behave编写测试用例并运行脚本
1、创建如下目录结构
├── app # 待测app
│ └── TestApp.app
└── features
├── calculate.feature # behave待测功能定义
├── environment.py # 环境配置
└── steps
└── step.py # 测试steps
2、测试求和功能
创建calculate.feature,输入如下内容
#language: zh-CN
功能: 求和
场景: 计算两个数相加
假如 第一个值输入 10
而且 第二个值输入 20
当 点击 求和按钮
那么 结果应该为30
3、配置环境
创建environment.py,输入如下内容,下列以真机配置为例
# -*- coding: utf-8 -*
import os
from appium import webdriver
def before_feature(context, feature):
app = '/Users/jackie-pc/Desktop/TestDemo/app/TestApp.app'
context.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'iOS',
'deviceName': '机型',
'platformVersion': '手机版本号',
'bundleId': 'App的bundleID',
'udid': '你的手机udid'
})
def after_feature(context, feature):
context.driver.quit()
Tip:
解释下里面的内容:app 是安装包的绝对路径,你可以打开命令行然后把你的包拖进去即可获取。udid可以通过itunes(音乐)链接你的真机进行获取。command_executor,即是你的Appium的服务器地址,你是通过
Appium-Python-Client
这句指令让Appium响应Python。
4、创建steps
创建step.py,根据测试用例编写脚本。
5、运行测试
behave
自动化测试运行方式二:直接编写测试Python脚本文件
上面是通过behave编写测试用例进行自动化测试的,如果你对测试用例要求度不高,你可以直接用IDE去编写Python测试脚本然后执行也可以。
附上一个简单的登录流程py测试脚本文件代码:
# -*- coding: utf-8 -*
import unittest
import os
from appium import webdriver
from time import sleep
class appiumSimpleTezt(unittest.TestCase):
def setUp(self):
app = os.path.abspath('/Users/legion/Desktop/AppiumTest/apps/CCB_Test.ipa')
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'iOS',
'platformVersion': '13.2',
'deviceName': 'iphone XR',
'bundleId': 'org.zzmetro.cn',
'udid': '00008020-001E35E12692002E'
}
)
def test_push_view(self):
sleep(2)
#next_view_button = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@name='tabbar_我的']")
next_view_button = self.driver.find_element_by_accessibility_id("tabbar_我的")
next_view_button.click()
# next_view_button = self.driver.find_element_by_xpath("(//XCUIElementTypeButton[@name='TBUIAutoTest_Property_noLoginB'])[1]")
next_view_button = self.driver.find_element_by_name("loginButton")
next_view_button.click()
next_textField = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_phoneTextFiled")
next_textField.set_value("15228856302")
sleep(2)
next_view_button = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_verfiCodeB")
next_view_button.click()
sleep(2)
next_view_button1 = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_agreeB")
next_view_button1.click()
sleep(2)
next_textView = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_textView")
# //XCUIElementTypeTextView[@name="TBUIAutoTest_Property_textView"]
next_textView.set_value("1234")
def tearDown(self):
sleep(2)
# self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(appiumSimpleTezt)
unittest.TextTestRunner(verbosity=2).run(suite)
特别说明:
上述代码中的
app = os.path.abspath('/Users/legion/Desktop/AppiumTest/apps/CCB_Test.ipa')
填写绝对路径,其中包是支持.app、.ipa文件类型。并且你的ipa文件上传至服务器后还能支持其下载地址。
小结
最佳的方案是用behave编写测试用例之后,然后针对测试用例中的场景用Python的IDE编写测试脚本。这里使用的IDE是PyCharm。
python元素定位
安卓:采用基本元素定位即可实现
driver.find_element_by_id("ddeeffv")
iOS:综合使用以下三种获取方式
self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_phoneTextFiled")
self.driver.find_element_by_name("("loginButton")"),
self.driver.find_element_by_xpath("//XCUIElementTypeButton[@name='tabbar_我的']")
python基本事件获取(iOS):
点击事件:
next_view_button = self.driver.find_element_by_name("loginButton")
next_view_button.click()
输入框事件:
next_textField = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_phoneTextFiled")
next_textField.set_value("15228856302")
文本清空
.clear()
获取富文本属性
.get_attribute('value')
参考链接:
使用python+Behave编写测试用例
在这里非常感谢每一位愿意在简书进行分享的朋友!!!
网友评论