151 lines
4.4 KiB
Python
151 lines
4.4 KiB
Python
import os
|
|
import pytest
|
|
from py.xml import html
|
|
from tools.config import RunConfig
|
|
from playwright.async_api import Playwright
|
|
from tools.get_log import GetLog
|
|
|
|
logger = GetLog.get_log()
|
|
# 项目目录配置
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
REPORT_DIR = BASE_DIR + "/test_report/"
|
|
|
|
|
|
# 定义基本测试环境
|
|
@pytest.fixture(scope='session')
|
|
def base_url():
|
|
return RunConfig.url
|
|
|
|
|
|
# 定义全局登录
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def page(base_url, playwright: Playwright):
|
|
browser = playwright.chromium.launch(headless=False)
|
|
context = browser.new_context()
|
|
|
|
page = context.new_page()
|
|
page.goto(base_url)
|
|
logger.info("打开浏览器进入首页:{}".format(base_url))
|
|
|
|
page.get_by_role("link", name="登录").click()
|
|
page.get_by_placeholder("手机号码").click()
|
|
page.get_by_placeholder("手机号码").fill("18211111111")
|
|
page.get_by_placeholder("密码").click()
|
|
page.get_by_placeholder("密码").fill("111111")
|
|
page.get_by_role("button", name="登录").click()
|
|
logger.info("完成前置登录操作:{}".format(18211111111))
|
|
|
|
yield page
|
|
|
|
page.close()
|
|
logger.info("测试完成,关闭页面!!!")
|
|
# ---------------------
|
|
context.close()
|
|
browser.close()
|
|
logger.info("关闭浏览器!!!")
|
|
|
|
|
|
# 视频录制
|
|
@pytest.fixture(scope="session")
|
|
def browser_context_args(browser_context_args):
|
|
"""
|
|
pytest-playwrigt 内置钩子
|
|
:param browser_context_args:
|
|
:param tmpdir_factory:
|
|
:return:
|
|
"""
|
|
return {
|
|
**browser_context_args,
|
|
"record_video_dir": os.path.join(REPORT_DIR, "videos") # 开始录制时并不知道测试结果正确与否,所以成功失败都会录制
|
|
}
|
|
|
|
|
|
# 设置用例描述表头
|
|
def pytest_html_results_table_header(cells):
|
|
cells.insert(2, html.th('Description'))
|
|
cells.pop()
|
|
|
|
|
|
# 设置用例描述表格
|
|
def pytest_html_results_table_row(report, cells):
|
|
cells.insert(2, html.td(report.description))
|
|
cells.pop()
|
|
|
|
|
|
@pytest.mark.hookwrapper
|
|
def pytest_runtest_makereport(item):
|
|
"""
|
|
用于向测试用例中添加用例的开始时间、内部注释,和失败截图等.
|
|
:param item:
|
|
"""
|
|
pytest_html = item.config.pluginmanager.getplugin('html')
|
|
outcome = yield
|
|
report = outcome.get_result()
|
|
report.description = description_html(item.function.__doc__)
|
|
extra = getattr(report, 'extra', [])
|
|
if "page" not in item.funcargs:
|
|
return "page not in item.funcargs"
|
|
page = item.funcargs["page"]
|
|
if report.when == 'call':
|
|
xfail = hasattr(report, 'wasxfail')
|
|
if (report.skipped and xfail) or (report.failed and not xfail):
|
|
case_path = report.nodeid.replace("::", "_") + ".png"
|
|
if "[" in case_path:
|
|
case_name = case_path.split("-")[0] + "].png"
|
|
else:
|
|
case_name = case_path
|
|
|
|
capture_screenshots(case_name, page)
|
|
img_path = "image/" + case_name.split("/")[-1]
|
|
if img_path:
|
|
html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
|
|
'onclick="window.open(this.src)" align="right"/></div>' % img_path
|
|
extra.append(pytest_html.extras.html(html))
|
|
report.extra = extra
|
|
|
|
|
|
def description_html(desc):
|
|
"""
|
|
将用例中的描述转成HTML对象
|
|
:param desc: 描述
|
|
:return:
|
|
"""
|
|
if desc is None:
|
|
return "No case description"
|
|
desc_ = ""
|
|
for i in range(len(desc)):
|
|
if i == 0:
|
|
pass
|
|
elif desc[i] == '\n':
|
|
desc_ = desc_ + ";"
|
|
else:
|
|
desc_ = desc_ + desc[i]
|
|
|
|
desc_lines = desc_.split(";")
|
|
desc_html = html.html(
|
|
html.head(
|
|
html.meta(name="Content-Type", value="text/html; charset=latin1")),
|
|
html.body(
|
|
[html.p(line) for line in desc_lines]))
|
|
return desc_html
|
|
|
|
|
|
def capture_screenshots(case_name, page):
|
|
"""
|
|
配置用例失败截图路径
|
|
:param case_name: 用例名
|
|
:return:
|
|
"""
|
|
global driver
|
|
file_name = case_name.split("/")[-1]
|
|
print("qhh", RunConfig.NEW_REPORT)
|
|
if RunConfig.NEW_REPORT is None:
|
|
raise NameError('没有初始化测试报告目录')
|
|
else:
|
|
image_dir = os.path.join(RunConfig.NEW_REPORT, "image", file_name)
|
|
page.screenshot(path=image_dir)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
capture_screenshots("test_cases/test_baidu_search.test_search_python.png")
|