1、数据驱动
- 参数化数据读取外部文件:使用yaml、json数据
- 测试步骤读取自外部文件:定制执行引擎
- 断言步骤读取自外部文件:定制执行引擎
- 整个用例读取自外部文件:动态构建用例
2、参数化的数据驱动
@pytest.mark.parametrize("keyword, expected_price", yaml.safe_load(open("search.yaml", "r")))
def test_001(self, keyword, expected_price):
def test_001(self, keyword, expected_price):
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_banner").click()
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys(keyword)
self.driver.find_element(AppiumBy.ID, "name").click()
price = self.driver.find_element(AppiumBy.ID, "current_price")
assert float(price.text) > expected_price
assert "price" in price.get_attribute("resource-id")
assert_that(price.get_attribute("package"), equal_to("com.xueqiu.android"))
search.yaml的数据格式
- [ pdd, 20]
- [ alibaba, 100]
- [ jd, 10]
3、测试步骤的数据驱动
import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
from hamcrest import *
import yaml
class TestCase:
def __init__(self, path):
file = open(path, "r")
self.steps = yaml.safe_load(file)
def run(self, driver: WebDriver):
for step in self.steps:
element = None
print(step)
if isinstance(step, dict):
if "id" in step.keys():
element = driver.find_element(AppiumBy.ID, step["id"])
elif "xpath" in step.keys():
element = driver.find_element(AppiumBy.XPATH, step["xpath"])
else:
print(step.keys())
if "input" in step.keys():
element.send_keys(step["input"])
else:
element.click()
if "get" in step.keys():
text = element.get_attribute(step["get"])
print(text)
class TestDemo:
def setup(self):
caps = {
"platformName": "android",
"deviceName": "008640dd0804",
"automationName": "uiautomator2",
"appPackage": "com.xueqiu.android",
"appActivity": ".view.WelcomeActivityAlias",
"autoGrantPermissions": "true"
}
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_agree").click()
self.driver.implicitly_wait(20)
def test_001(self):
TestCase("testcase001.yaml").run(self.driver)
def teardown(self):
self.driver.quit()
testcase001.yaml
- id: com.xueqiu.android:id/tv_banner
- id: com.xueqiu.android:id/search_input_text
input: alibaba
- id: name
- id: current_price
get: text
4、完整代码
import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
from hamcrest import *
import yaml
class TestCase:
def __init__(self, path):
file = open(path, "r")
self.steps = yaml.safe_load(file)
def run(self, driver: WebDriver):
for step in self.steps:
element = None
print(step)
if isinstance(step, dict):
if "id" in step.keys():
element = driver.find_element(AppiumBy.ID, step["id"])
elif "xpath" in step.keys():
element = driver.find_element(AppiumBy.XPATH, step["xpath"])
else:
print(step.keys())
if "input" in step.keys():
element.send_keys(step["input"])
else:
element.click()
if "get" in step.keys():
text = element.get_attribute(step["get"])
print(text)
class TestDemo:
def setup(self):
caps = {
"platformName": "android",
"deviceName": "008640dd0804",
"automationName": "uiautomator2",
"appPackage": "com.xueqiu.android",
"appActivity": ".view.WelcomeActivityAlias",
"autoGrantPermissions": "true"
}
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_agree").click()
self.driver.implicitly_wait(20)
def test_001(self):
TestCase("testcase001.yaml").run(self.driver)
search_data = yaml.safe_load(open("search.yaml", "r"))
print(search_data)
@pytest.mark.parametrize("keyword, expected_price", search_data)
def test_002(self, keyword, expected_price):
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_banner").click()
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys(keyword)
self.driver.find_element(AppiumBy.ID, "name").click()
price = self.driver.find_element(AppiumBy.ID, "current_price")
assert float(price.text) > expected_price
assert "price" in price.get_attribute("resource-id")
assert_that(price.get_attribute("package"), equal_to("com.xueqiu.android"))
def teardown(self):
self.driver.quit()
|