app自动化7-数据驱动
1. 参数化:根据传入的数据,对测试用例进行迭代调用
2. 数据驱动:基于数据完成流程调度,通过数据来自外部数据文件
3. 参数化是数据驱动的基础和特例
4. 参数化:通过pytest实现参数化

from appium import webdriver from hamcrest import * import pytest class TestDemo(): def setup(self): #参数设置 caps = {} caps["platformName"] = "android" caps["deviceName"] = "qiucaixia" caps["appPackage"] = "com.xueqiu.android" caps["appActivity"] = ".view.WelcomeActivityAlias" caps["autoGrantPermissions"] = "true" #自动赋予app权限 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(10) #隐式等待 @pytest.mark.parametrize("keyword,expected_price",[ ("pdd",20), ("alibaba",200), ("jd",30) ]) def test_search(self,keyword,expected_price): #点击搜索栏 self.driver.find_element_by_id("com.xueqiu.android:id/home_search").click() #输入搜索内容 self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text").send_keys(keyword) self.driver.find_element_by_id("name").click() price=self.driver.find_element_by_id("current_price") assert float(price.text) > expected_price #断言属性- resource-id assert 'price' in price.get_attribute('resource-id') #hamcrest断言 assert_that(price.get_attribute('package'),equal_to('com.xueqiu.android')) def teardown(self): self.driver.quit()
5. 数据驱动:
1> 参数化数据读取自外部文件:使用YAML,JSON读取
2> 测试步骤读取自外部文件:定制执行引擎
3> 断言步骤读取自部文件:定制执行引擎
4> 整个用例读取自外部文件:动态创建用例
6. 参数化数据读取YAML文件:
1> yaml文件的数据:(search_data.yaml)
- ['pdd',20]
- [jd,10]
- [alibaba,100]
2> 脚本如下:(脚本和yaml文件在一个目录下)

from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait # 显示等待 from selenium.webdriver.support import expected_conditions from selenium.webdriver.common.by import By from hamcrest import * import pytest import yaml class TestDemo(): with open(r'D:\test_dir\pycharm_project\other\search_data.yaml','r') as f: search_data=yaml.safe_load(f) print(search_data) def setup(self): #参数设置 caps = {} caps["platformName"] = "android" caps["deviceName"] = "qiucaixia" caps["appPackage"] = "com.xueqiu.android" caps["appActivity"] = ".view.WelcomeActivityAlias" caps["autoGrantPermissions"] = "true" #自动赋予app权限 # caps["unicodeKeyboard"]="true" #支持输入中文 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(10) #隐式等待 @pytest.mark.parametrize("keyword,expected_price",search_data) def test_search_from_yaml(self,keyword,expected_price): #点击搜索栏 self.driver.find_element_by_id("com.xueqiu.android:id/home_search").click() #等待搜索栏下面的页面展示出来,否则会覆盖掉输入内容后搜索出来的结果,导致下面选择不到结果 WebDriverWait(self.driver,15).until(expected_conditions.visibility_of_element_located((By.ID,'tv_hot_title'))) #输入搜索内容 self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text").send_keys(keyword) #等待搜索结果可见 WebDriverWait(self.driver,15).until(expected_conditions.visibility_of_element_located((By.ID,'name'))) self.driver.find_element_by_id("name").click() #点击搜索到的第一个内容 price=self.driver.find_element_by_id("current_price") assert float(price.text) > expected_price #断言属性- resource-id # assert 'price' in price.get_attribute('resource-id') #hamcrest断言 # assert_that(price.get_attribute('package'),equal_to('com.xueqiu.android')) # assert_that(price.get_attribute('id'),equal_to('current_price')) def teardown(self): self.driver.quit()
3> 执行结果
7. 测试步骤数据驱动:将步骤和数据都维护到yaml文件中
8. 步骤数据驱动举例:
1> 步骤和用例数据维护在step_data.yaml文件
- id: home_search
- wait: tv_hot_title
- id: search_input_text
input: pdd
- wait: name
- id: name
- id: current_price
2> 用例脚本:(脚本和yaml文件在一个目录下)

from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait # 显示等待 from selenium.webdriver.support import expected_conditions from selenium.webdriver.common.by import By import yaml class TestDemo(): def setup(self): #参数设置 caps = {} caps["platformName"] = "android" caps["deviceName"] = "qiucaixia" caps["appPackage"] = "com.xueqiu.android" caps["appActivity"] = ".view.WelcomeActivityAlias" caps["autoGrantPermissions"] = "true" #自动赋予app权限 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(10) #隐式等待 def test_search_from_steps(self): TestCase(r'D:\test_dir\pycharm_project\other\steps_data.yaml').run(self.driver) def teardown(self): self.driver.quit() 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_by_id(step['id']) elif 'xpath' in step.keys(): element=driver.find_element_by_xpath(step['xpath']) else: print(step.keys()) if 'input' in step.keys(): element.send_keys(step['input']) elif 'get' in step.keys(): attr_value=element.get_attribute(step['get']) elif 'math' in step.keys(): activty_value=element.text elif 'wait' in step.keys(): WebDriverWait(driver,15).until(expected_conditions.visibility_of_element_located((By.ID,step['wait']))) else: element.click() if 'assert' in step.keys(): expected_value=step['assert'] assert activty_value>expected_value
3> 执行结果: