参数化
数据驱动
po定义的数据驱动
search:
- id: home_search
- click: []
- id: search_input_text
- send_keys: ${keyword}
- id: name
- click: []
测试数据的数据驱动
固定列表风格、正交法、笛卡尔积在这个地方实现
params:
#列表
- keyword: alibba
- keyword: baidu
- keyword: jd
#params2:
# #词典
# username:
# - user1
# - user2
# - user3
# password:
# - 12345678
# - abcdefgh
代码
测试用例
import pytest
from test_framework.demo_page import DemoPage
from test_framework.utils import Utils
class TestLogin:
data = Utils.from_file('test_search.yaml')
def setup_class(self):
self.demo = DemoPage()
self.demo.start()
def setup(self):
pass
def teardown(self):
self.demo.back()
def teardown_class(self):
self.demo.stop()
# todo:测试数据的数据驱动
@pytest.mark.parametrize('username, password', [
("user1", 'pasword1'),
('user2', 'password2')
])
def test_login(self, username, password):
# todo: 测试步骤的数据驱动
self.demo.login(username, password)
assert 1 == 1
# @pytest.mark.parametrize('keyword', [
# 'alibaba',
# # 'baidu',
# # 'jd'
# ])
@pytest.mark.parametrize(data['keys'], data['values'])
def test_search(self, keyword):
self.demo.search(keyword)
demo_page
from selenium.webdriver.common.by import By
from test_framework.base_page import BasePage
class DemoPage(BasePage):
_search_button=(By.ID, 'home_search')
#todo:po的数据驱动
def login(self, username, password):
pass
def forget_password(self):
pass
def search(self, keyword):
self.po_run('search', keyword=keyword)
# self.find(self._search_button).click()
return self
def back(self):
self.po_run('back')
return self
base_page
import logging
import yaml
from appium import webdriver
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
class BasePage:
_driver: WebDriver = None
_current_element: WebElement = None
def start(self):
caps = {
'platformName': 'android',
'deviceName': 'ceshiren.com',
'appPackage': 'com.xueqiu.android',
'appActivity': '.view.WelcomeActivityAlias',
'noReset': True
}
self._driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps)
self._driver.implicitly_wait(20)
return self
def stop(self):
self._driver.quit()
def find(self, by):
self._current_element = self._driver.find_element(*by)
return self
def click(self):
self._current_element.click()
return self
def send_keys(self, text):
self._current_element.send_keys(text)
return self
def back(self):
self._driver.back()
return self
def po_run(self, po_method, **kwargs):
# read yaml
with open('page_demo.yaml') as f:
yaml_data = yaml.safe_load(f)
# find search
for step in yaml_data[po_method]:
# find by click send_keys
if isinstance(step, dict):
# id click send_keys
for key in step.keys():
if key == 'id':
locator = (By.ID, step[key])
self.find(locator)
elif key == 'click':
self.click()
elif key == 'send_keys':
text = str(step[key])
for k, v in kwargs.items():
text=text.replace('${' + k + '}', v)
self.send_keys(text)
# todo: 更多关键词
else:
logging.error(f"dont know {step}")
测试框架
作业
完成自己的基础测试框架,可以完整的重写今天的课程代码, 把自己的git地址贴到回复里