tesgsdfsgfd/conftest.py

117 lines
4.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
# coding=utf-8
@Time:2024/7/3 11:16
@Auth:jiafeng
@File:conftest.py
"""
import os
import shutil
import time
import pytest
import allure
from playwright.sync_api import sync_playwright
from CBB.pages.login_page import LoginPage
from CBB.setting import login_base_dir, RunConfig
from CBB.utils.read_file import load_yaml
# 导入pytest的fixture装饰器以及request对象用于定义fixture函数和访问测试上下文
@pytest.fixture(scope="function")
def init_browser(request, browser_type=RunConfig.browser[0]):
"""
初始化浏览器驱动并打开基础URL根据指定的浏览器类型默认从配置中读取
支持的浏览器类型有Chromium, Firefox, WebKit。
参数:
- request: pytest请求对象用于注册清理函数
- browser_type: 字符串默认为从RunConfig获取的第一个浏览器类型
函数功能细节:
1. 初始化Playwright实例以与浏览器交互。
2. 根据browser_type参数选择并启动相应的浏览器实例禁用无头模式并最大化窗口。
3. 配置新浏览器上下文以录制视频,设定视频保存目录及尺寸。
4. 在新上下文中创建页面,供测试使用。
5. 定义清理函数fin用于测试结束时执行资源清理及视频录制文件处理。
6. 使用yield关键字在执行测试前返回页面和上下文对象测试后自动调用fin进行清理工作。
"""
# 初始化Playwright
playwright = sync_playwright().start()
# 根据浏览器类型启动对应浏览器
if browser_type == 'chromium':
browser = playwright.chromium.launch(headless=False, args=['--start-maximized'])
elif browser_type == 'firefox':
browser = playwright.firefox.launch(headless=False, args=['--start-maximized'])
elif browser_type == 'webkit':
browser = playwright.webkit.launch(headless=False, args=['--start-maximized'])
else:
# 如果提供的浏览器类型不受支持,则抛出错误
raise ValueError("不支持的浏览器类型。支持的浏览器: chromium, firefox, webkit.")
# 配置浏览器上下文以录制视频
context = browser.new_context(no_viewport=True,
record_video_dir="videos/",
record_video_size={"width": 1280, "height": 720})
# 在新上下文中创建页面
page = context.new_page()
def fin():
# 清理操作尝试获取视频路径关闭浏览器上下文和浏览器停止Playwright
try:
video_path = page.video.path()
except Exception as e:
video_path = None
context.close()
browser.close()
playwright.stop()
# 将录制的视频附加到Allure测试报告中
if video_path:
allure.attach.file(video_path, name="test_video", attachment_type=allure.attachment_type.MP4)
# 注册清理函数确保测试结束后执行fin
request.addfinalizer(fin)
# 返回页面和上下文给测试函数使用
yield page, context
@allure.title("登录成功测试用例")
@allure.step("操作步骤: 登录")
@pytest.fixture(scope="function")
def test_login_success(init_browser):
"""
进行登录操作,并验证登录成功
"""
page, context = init_browser
login_page = LoginPage(page)
# 从配置文件中读取登录URL
login_url = load_yaml(path=login_base_dir, key='baseUrl')
with allure.step(f"打开登录页面:{login_url}"):
page.goto(login_url)
# 从配置文件中读取用户名和密码
# username = load_yaml(path='config.yaml', key='username')
# password = load_yaml(path='config.yaml', key='password')
# with allure.step("输入用户名"):
login_page.enter_username('wjf')
# with allure.step("输入密码"):
login_page.enter_password('6')
# with allure.step("点击登录按钮"):
login_page.click_login_button()
page.wait_for_timeout(1000) # 等待1秒钟以确保页面加载完成
# 确认登录成功
with allure.step("验证登录成功"):
assert login_page.is_logged_in()
yield page, context