继续分离,实现打开一次chrome浏览器,执行N个测试用例,具体查询的数据通过data.yaml定义
project > data > data.yaml
project > utils > read.py
project > test_po > conftest.py
project > test_po > test_baidu.py
project > page > page_baidu.py
project > base > base_page.py
# project > data > data.yaml
skill:
- 接口自动化
- UI自动化
- 性能测试
# project > utils > read.py
import configparser
import yaml
from utils.get_file_path import get_yaml_path, get_ini_path
path = get_yaml_path()
ini_path = get_ini_path()
def read_yaml():
with open(path, encoding="utf8") as f:
data = yaml.safe_load(f)
return data
def read_ini():
config = configparser.ConfigParser()
config.read(ini_path, encoding='utf8')
return config
if __name__ == '__main__':
print(read_yaml())
# print(read_ini()['mysql']['HOST'])
# project > test_po > conftest.py
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope="session")
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器")
yield driver
print("关闭浏览器")
driver.close()
driver.quit()
@pytest.fixture()
def fixture():
print("我是前置步骤")
yield "老白"
print("我是后置步骤")
# project > test_po > test_baidu.py
from time import sleep
import pytest
from page.page_baidu import PageBaidu
from utils.read import read_yaml
class TestBaidu:
@pytest.mark.parametrize("key",read_yaml()['skill'])
def test_baidu1(self,driver,key):
page = PageBaidu(driver)
page.search_keyword(key)
sleep(2)
def test_baidu2(self,driver):
page = PageBaidu(driver)
page.search_keyword("UI自动化")
sleep(2)
# project > page > page_baidu.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from base.base_page import BasePage
from utils.log_util import logger
class PageBaidu(BasePage):
# 新闻
news = (By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]')
# 百度一下按钮
button = (By.ID, 'su')
# 百度输入框
input = (By.ID, 'kw')
def search_keyword(self, keyword): # 把操作封装到方法里面去。
logger.info("查找元素并输入内容")
self.driver.find_element(*self.input).send_keys(keyword)
self.driver.find_element(*self.button).click()
# project > base > base_page.py
from selenium import webdriver
class BasePage:
def __init__(self,driver): # 此处的driver是形参,最终会调用conftest里面的driver
self.driver = driver
# self.driver.maximize_window()
self.driver.implicitly_wait(10)
self.driver.get("https://www.baidu.com/")
继续优化 只更新test_baidu.py,其他代码复用以上
# project > test_po > test_baidu.py
from time import sleep
import pytest
from page.page_baidu import PageBaidu
from utils.read import read_yaml
class TestBaidu:
@pytest.mark.parametrize("key",read_yaml()['skill'])
def test_baidu1(self,driver,key):
page = PageBaidu(driver)
page.search_keyword(key)
sleep(2)
def test_baidu2(self,driver):
page = PageBaidu(driver)
page.search_keyword("UI自动化")
sleep(2)
def test_baidu3(self,driver):
page = PageBaidu(driver)
driver.find_element(*page.input).send_keys("RDMA")
driver.find_element(*page.button).click()
sleep(2)
网友评论