from pathlib import Path import re import sys import allure import pytest from playwright.sync_api import expect PROJECT_ROOT = Path(__file__).resolve().parents[1] if str(PROJECT_ROOT) not in sys.path: sys.path.insert(0, str(PROJECT_ROOT)) from config.settings import settings from utils.logger import get_logger logger = get_logger(__name__) @allure.feature("FAQ") @allure.story("FAQ页面内容") @allure.title("校验 FAQ 页面核心内容和分类链接可见") def test_faq_page_key_content_visible(page): logger.info("使用 pytest-playwright 统一无头浏览器配置") logger.info("打开 FAQ 页面") page.goto("/faq", wait_until="domcontentloaded") logger.info("校验 FAQ 页面地址、标题和页面内容已加载") expect(page).to_have_url(re.compile(r".*/faq/?$"), timeout=settings.default_timeout) assert page.title().strip() expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout) faq_links = page.locator('a[href^="/faq/"]') expect(faq_links.first).to_be_visible(timeout=settings.default_timeout) assert faq_links.count() > 0 @allure.feature("FAQ") @allure.story("FAQ分类详情") @allure.title("校验 FAQ 分类详情链接可见且地址格式正确") def test_faq_category_detail_link_visible(page): logger.info("使用 pytest-playwright 统一无头浏览器配置") logger.info("打开 FAQ 页面") page.goto("/faq", wait_until="domcontentloaded") logger.info("动态校验 FAQ 分类详情链接可见且地址格式正确") faq_detail_link = page.locator('a[href^="/faq/"]').first expect(faq_detail_link).to_be_visible(timeout=settings.default_timeout) faq_detail_href = faq_detail_link.get_attribute("href") logger.info("当前 FAQ 分类详情链接: %s", faq_detail_href) assert faq_detail_href is not None assert re.match(r"/faq/\d+(\?.*)?$", faq_detail_href) @allure.feature("FAQ") @allure.story("提交问题") @allure.title("游客提交 FAQ 问题表单成功") def test_faq_submit_question_form_success(page, ensure_guest_user): logger.info("打开 FAQ 页面") page.goto("/faq", wait_until="domcontentloaded") ensure_guest_user() expect(page).to_have_url(re.compile(r".*/faq/?$"), timeout=settings.default_timeout) logger.info("点击 Submit a Question 进入问题提交表单") submit_question_link = page.locator('a[href="/faq/submit-question"]') expect(submit_question_link).to_be_visible(timeout=settings.default_timeout) submit_question_link.click() expect(page).to_have_url(re.compile(r".*/faq/submit-question/?$"), timeout=settings.default_timeout) logger.info("填写 FAQ 问题提交表单") name_input = page.locator('input[placeholder="Your Name"]') email_input = page.locator('input[placeholder="Your Email"]') order_input = page.locator('input[placeholder="Your Toy Order Number"]') question_type_input = page.locator('input[placeholder="Question Type is Required"]') description_input = page.locator("textarea") send_button = page.get_by_role("button", name="Send message") expect(name_input).to_be_visible(timeout=settings.default_timeout) name_input.fill("Auto Test User") email_input.fill("autotest@example.com") order_input.fill("AUTO-FAQ-001") description_input.fill("This is an automated FAQ submit question test. Please ignore.") logger.info("选择问题类型 Other") question_type_input.click(force=True) other_option = page.locator('div[title="Other"]') expect(other_option).to_be_visible(timeout=settings.default_timeout) other_option.click(force=True) expect(question_type_input).to_have_value("Other", timeout=settings.default_timeout) logger.info("点击 Send message 并校验提交成功") expect(send_button).to_be_enabled(timeout=settings.default_timeout) with page.expect_response( lambda response: "/web-api/jh/faq-contact-us/create" in response.url, timeout=settings.default_timeout, ) as response_info: send_button.click() response = response_info.value assert response.ok, f"FAQ 提交接口失败: {response.status} {response.url}" expect(name_input).to_have_value("", timeout=settings.default_timeout) expect(email_input).to_have_value("", timeout=settings.default_timeout) expect(order_input).to_have_value("", timeout=settings.default_timeout) expect(question_type_input).to_have_value("", timeout=settings.default_timeout) expect(description_input).to_have_value("", timeout=settings.default_timeout) if __name__ == "__main__": raise SystemExit(pytest.main([str(Path(__file__))]))