feat: 新增JoyHub UI自动化测试目录
1. 新增 Joyhub_ui_auto_test/ 目录: - tests/ - 测试用例目录 - pages/ - 页面元素定位 - config/ - 配置文件 - utils/ - 工具类 - test_data/ - 测试数据 - reports/ - 测试报告 - webapp-testing/ - WebApp测试相关 2. 配置文件: - pytest.ini - pytest配置 - requirements.txt - 依赖列表 - README.md - 项目说明
This commit is contained in:
0
Joyhub_ui_auto_test/tests/__init__.py
Normal file
0
Joyhub_ui_auto_test/tests/__init__.py
Normal file
236
Joyhub_ui_auto_test/tests/conftest.py
Normal file
236
Joyhub_ui_auto_test/tests/conftest.py
Normal file
@@ -0,0 +1,236 @@
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import allure
|
||||
import pytest
|
||||
from config.settings import settings
|
||||
from playwright.sync_api import Error as PlaywrightError
|
||||
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
|
||||
from playwright.sync_api import expect
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger()
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||||
ALLURE_RAW_DIR = PROJECT_ROOT / "reports" / "allure-raw"
|
||||
ALLURE_HTML_DIR = PROJECT_ROOT / "reports" / "allure-results"
|
||||
|
||||
|
||||
def accept_cookie_if_present(page):
|
||||
privacy_link = page.locator('a[href="/privacy-policy-web"]').last
|
||||
try:
|
||||
if privacy_link.is_visible(timeout=1_000):
|
||||
cookie_container = privacy_link.locator("xpath=ancestor::div[.//button][1]")
|
||||
cookie_container.locator("button").first.click()
|
||||
return
|
||||
except PlaywrightTimeoutError:
|
||||
pass
|
||||
|
||||
accept_button = page.locator("button", has_text=re.compile(r"Acce?pet|Accept", re.I)).first
|
||||
try:
|
||||
if accept_button.is_visible(timeout=1_000):
|
||||
accept_button.click()
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("未检测到 Cookie 操作区域,继续执行")
|
||||
except PlaywrightError as exc:
|
||||
logger.info("Cookie 操作区域不可点击,继续执行: %s", exc)
|
||||
|
||||
|
||||
def is_logged_in(page):
|
||||
try:
|
||||
aside_button = page.locator("aside button").first
|
||||
if not aside_button.is_visible(timeout=3_000):
|
||||
return False
|
||||
button_text = aside_button.inner_text(timeout=3_000).strip()
|
||||
return bool(re.search(r"Logout|Log out", button_text, re.I))
|
||||
except PlaywrightError:
|
||||
return False
|
||||
|
||||
|
||||
def logout_if_logged_in(page):
|
||||
if not is_logged_in(page):
|
||||
logger.info("当前为游客状态,无需 Logout")
|
||||
return
|
||||
|
||||
logger.info("当前已登录,点击 Logout 切换为游客状态")
|
||||
page.locator("aside button").first.click()
|
||||
expect(page.locator("aside button").first).not_to_contain_text(re.compile(r"Logout|Log out", re.I), timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def login_as_configured_user_if_needed(page):
|
||||
if is_logged_in(page):
|
||||
logger.info("当前已登录,跳过重复登录")
|
||||
return
|
||||
|
||||
logger.info("当前未登录,执行配置用户登录")
|
||||
login_inputs = page.locator('input[type="text"]')
|
||||
last_error = None
|
||||
for _ in range(3):
|
||||
try:
|
||||
login_button = page.locator("aside button").first
|
||||
expect(login_button).to_be_visible(timeout=settings.default_timeout)
|
||||
login_button.click()
|
||||
expect(login_inputs.first).to_be_visible(timeout=5_000)
|
||||
break
|
||||
except (AssertionError, PlaywrightError) as exc:
|
||||
last_error = exc
|
||||
logger.info("登录弹窗未打开,重试点击登录入口: %s", exc)
|
||||
page.wait_for_timeout(1_000)
|
||||
else:
|
||||
raise AssertionError(f"登录弹窗未打开: {last_error}")
|
||||
login_inputs.nth(0).fill(settings.login_email)
|
||||
login_inputs.nth(1).fill(settings.verification_code)
|
||||
|
||||
confirmation_items = page.locator("div.inline-flex.cursor-pointer")
|
||||
expect(confirmation_items.nth(0)).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(confirmation_items.nth(1)).to_be_visible(timeout=settings.default_timeout)
|
||||
confirmation_items.nth(0).click()
|
||||
confirmation_items.nth(1).click()
|
||||
|
||||
submit_button = page.locator("button").last
|
||||
expect(submit_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
submit_button.click()
|
||||
|
||||
expect(page.locator("aside button").first).to_contain_text(
|
||||
re.compile(r"Logout|Log out", re.I), timeout=settings.default_timeout
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ensure_guest_user(page):
|
||||
def _ensure_guest_user():
|
||||
accept_cookie_if_present(page)
|
||||
logout_if_logged_in(page)
|
||||
|
||||
return _ensure_guest_user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ensure_logged_in_user(page):
|
||||
def _ensure_logged_in_user():
|
||||
accept_cookie_if_present(page)
|
||||
login_as_configured_user_if_needed(page)
|
||||
|
||||
return _ensure_logged_in_user
|
||||
|
||||
|
||||
def pytest_sessionstart(session):
|
||||
for report_dir in (ALLURE_RAW_DIR, ALLURE_HTML_DIR):
|
||||
if report_dir.exists():
|
||||
shutil.rmtree(report_dir)
|
||||
report_dir.mkdir(parents=True, exist_ok=True)
|
||||
logger.info("已清理 Allure 目录: %s", report_dir)
|
||||
|
||||
|
||||
def pytest_sessionfinish(session, exitstatus):
|
||||
if not ALLURE_RAW_DIR.exists() or not any(ALLURE_RAW_DIR.iterdir()):
|
||||
logger.warning("未找到 Allure 原始结果,跳过 HTML 报告生成: %s", ALLURE_RAW_DIR)
|
||||
return
|
||||
|
||||
allure_command = shutil.which("allure") or shutil.which("allure.cmd")
|
||||
if not allure_command:
|
||||
logger.warning("未找到 Allure CLI,跳过 HTML 报告生成")
|
||||
return
|
||||
|
||||
command = [
|
||||
allure_command,
|
||||
"generate",
|
||||
str(ALLURE_RAW_DIR),
|
||||
"-o",
|
||||
str(ALLURE_HTML_DIR),
|
||||
"--clean",
|
||||
]
|
||||
logger.info("开始生成 Allure HTML 报告: %s", " ".join(command))
|
||||
result = subprocess.run(
|
||||
command,
|
||||
cwd=PROJECT_ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
)
|
||||
if result.returncode != 0:
|
||||
logger.error("Allure HTML 报告生成失败: %s", result.stderr.strip())
|
||||
return
|
||||
logger.info("Allure HTML 报告已生成: %s", ALLURE_HTML_DIR / "index.html")
|
||||
|
||||
|
||||
def _safe_attachment_name(name):
|
||||
return re.sub(r"[^0-9A-Za-z_.\-\u4e00-\u9fff]+", "_", name).strip("_")[:120] or "screenshot"
|
||||
|
||||
|
||||
def _attach_page_screenshot(page, name):
|
||||
try:
|
||||
screenshot = page.screenshot(full_page=True)
|
||||
allure.attach(
|
||||
screenshot,
|
||||
name=_safe_attachment_name(name),
|
||||
attachment_type=allure.attachment_type.PNG,
|
||||
)
|
||||
except PlaywrightError as exc:
|
||||
logger.info("Allure 截图附件生成失败: %s", exc)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def attach_page_screenshot(page):
|
||||
def _attach(name="页面截图"):
|
||||
_attach_page_screenshot(page, name)
|
||||
|
||||
return _attach
|
||||
|
||||
|
||||
@pytest.hookimpl(hookwrapper=True)
|
||||
def pytest_runtest_makereport(item, call):
|
||||
outcome = yield
|
||||
report = outcome.get_result()
|
||||
setattr(item, "rep_" + report.when, report)
|
||||
|
||||
if report.when != "call" or "page" not in item.fixturenames:
|
||||
return
|
||||
|
||||
page = item.funcargs.get("page")
|
||||
if not page:
|
||||
return
|
||||
|
||||
if report.failed:
|
||||
_attach_page_screenshot(page, f"失败截图_{item.name}")
|
||||
elif report.passed:
|
||||
_attach_page_screenshot(page, f"结束截图_{item.name}")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def test_run_logger(request):
|
||||
case_name = request.node.nodeid
|
||||
start_time = time.perf_counter()
|
||||
logger.info("用例开始: %s", case_name)
|
||||
|
||||
try:
|
||||
yield
|
||||
except Exception:
|
||||
elapsed = time.perf_counter() - start_time
|
||||
logger.exception("用例异常: %s, 耗时: %.2fs", case_name, elapsed)
|
||||
raise
|
||||
else:
|
||||
elapsed = time.perf_counter() - start_time
|
||||
logger.info("用例通过: %s, 耗时: %.2fs", case_name, elapsed)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def browser_context_args(browser_context_args):
|
||||
return {
|
||||
**browser_context_args,
|
||||
"base_url": settings.base_url,
|
||||
"viewport": {"width": settings.viewport_width, "height": settings.viewport_height},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def browser_type_launch_args(browser_type_launch_args):
|
||||
return {
|
||||
**browser_type_launch_args,
|
||||
"headless": True,
|
||||
# 可视化执行配置:"headless": False, "slow_mo": 300,
|
||||
"timeout": settings.default_timeout,
|
||||
}
|
||||
16
Joyhub_ui_auto_test/tests/run.tests.py
Normal file
16
Joyhub_ui_auto_test/tests/run.tests.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> int:
|
||||
tests_dir = Path(__file__).resolve().parent
|
||||
project_root = tests_dir.parent
|
||||
|
||||
command = [sys.executable, "-m", "pytest", str(tests_dir), *sys.argv[1:]]
|
||||
completed = subprocess.run(command, cwd=project_root)
|
||||
return completed.returncode
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
55
Joyhub_ui_auto_test/tests/test_app_page.py
Normal file
55
Joyhub_ui_auto_test/tests/test_app_page.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
def test_app_page_key_content_and_download_links_visible(page):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开 Download the App 页面")
|
||||
page.goto("/app", wait_until="domcontentloaded")
|
||||
|
||||
logger.info("校验 App 下载页地址和页面已加载")
|
||||
expect(page).to_have_url(re.compile(r".*/app/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
logger.info("动态校验下载入口展示,不绑定具体版本文案")
|
||||
download_links = page.locator('section main a[href]')
|
||||
expect(download_links.first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
download_link_count = download_links.count()
|
||||
logger.info("当前页面下载入口数量: %s", download_link_count)
|
||||
assert download_link_count >= 4
|
||||
|
||||
for index in range(download_link_count):
|
||||
href = download_links.nth(index).get_attribute("href")
|
||||
logger.info("当前下载入口 href: %s", href)
|
||||
assert href is not None
|
||||
|
||||
|
||||
def test_app_page_how_to_download_apk_button_visible(page):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开 Download the App 页面")
|
||||
page.goto("/app", wait_until="domcontentloaded")
|
||||
|
||||
logger.info("动态校验下载说明按钮可见,不绑定按钮文案")
|
||||
how_to_download_button = page.locator("section main button").first
|
||||
expect(how_to_download_button).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(how_to_download_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
71
Joyhub_ui_auto_test/tests/test_download_app.py
Normal file
71
Joyhub_ui_auto_test/tests/test_download_app.py
Normal file
@@ -0,0 +1,71 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
def test_click_download_the_app_then_google_play_return_app_wait_and_close_browser(page, ensure_guest_user):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
logger.info("点击 /app 导航链接")
|
||||
page.locator('header a[href="/app"]').first.click()
|
||||
|
||||
logger.info("等待跳转到 /app 页面")
|
||||
expect(page).to_have_url(re.compile(r".*/app/?$"), timeout=settings.default_timeout)
|
||||
|
||||
app_page_url = page.url
|
||||
logger.info("Download the App 页面跳转成功,当前地址: %s", app_page_url)
|
||||
logger.info("停留 2 秒")
|
||||
page.wait_for_timeout(2_000)
|
||||
|
||||
logger.info("定位 Google Play 按钮")
|
||||
google_play_button = page.locator('a[href="https://www.cecece"]')
|
||||
expect(google_play_button).to_be_visible(timeout=settings.default_timeout)
|
||||
google_play_url = google_play_button.get_attribute("href")
|
||||
|
||||
logger.info("拦截 Google Play 外链请求,仅校验跳转地址,不依赖外部服务可用性")
|
||||
page.route(
|
||||
re.compile(r"https://www\.cecece/?$"),
|
||||
lambda route: route.fulfill(status=200, content_type="text/html", body="Google Play mock page"),
|
||||
)
|
||||
|
||||
logger.info("点击 Google Play 按钮,目标地址: %s", google_play_url)
|
||||
google_play_button.click(no_wait_after=True)
|
||||
page.wait_for_timeout(1_000)
|
||||
|
||||
if page.url == app_page_url and google_play_url:
|
||||
logger.info("点击后页面未跳转,直接导航到链接地址,仅判断地址栏跳转: %s", google_play_url)
|
||||
page.goto(google_play_url, wait_until="commit", timeout=5_000)
|
||||
|
||||
assert page.url.rstrip("/") == google_play_url.rstrip("/")
|
||||
|
||||
logger.info("Google Play 链接跳转成功,当前地址: %s", page.url)
|
||||
logger.info("停留 2 秒")
|
||||
page.wait_for_timeout(2_000)
|
||||
|
||||
logger.info("返回 Download the App 页面")
|
||||
page.goto(app_page_url, wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/app/?$"), timeout=settings.default_timeout)
|
||||
|
||||
logger.info("已返回 Download the App 页面,当前地址: %s", page.url)
|
||||
logger.info("停留 2 秒")
|
||||
page.wait_for_timeout(2_000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
101
Joyhub_ui_auto_test/tests/test_faq_page.py
Normal file
101
Joyhub_ui_auto_test/tests/test_faq_page.py
Normal file
@@ -0,0 +1,101 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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__))]))
|
||||
9
Joyhub_ui_auto_test/tests/test_home_page.py
Normal file
9
Joyhub_ui_auto_test/tests/test_home_page.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import pytest
|
||||
from pages.home_page import HomePage
|
||||
|
||||
|
||||
@pytest.mark.smoke
|
||||
def test_home_page_loaded(page):
|
||||
home_page = HomePage(page)
|
||||
home_page.open()
|
||||
home_page.should_be_loaded()
|
||||
127
Joyhub_ui_auto_test/tests/test_home_posts_load_more.py
Normal file
127
Joyhub_ui_auto_test/tests/test_home_posts_load_more.py
Normal file
@@ -0,0 +1,127 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(PROJECT_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from playwright.sync_api import expect
|
||||
|
||||
from tests.conftest import accept_cookie_if_present, login_as_configured_user_if_needed
|
||||
from config.settings import settings
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
POST_DETAIL_MODAL = "div.fixed.inset-0"
|
||||
POST_CARD_SELECTOR = "div.flex.flex-col.overflow-hidden.rounded-xl.bg-white.cursor-pointer"
|
||||
|
||||
|
||||
def _accept_cookies_if_present(page):
|
||||
accept_cookie_if_present(page)
|
||||
|
||||
|
||||
def _open_home(page):
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
_accept_cookies_if_present(page)
|
||||
|
||||
|
||||
def _post_cards(page):
|
||||
return page.locator(POST_CARD_SELECTOR)
|
||||
|
||||
|
||||
def _open_first_post_and_close(page):
|
||||
cards = _post_cards(page)
|
||||
expect(cards.first).to_be_visible(timeout=settings.default_timeout)
|
||||
logger.info("点击首个帖子查看详情")
|
||||
cards.first.click()
|
||||
|
||||
post_modal = page.locator(POST_DETAIL_MODAL).last
|
||||
expect(post_modal).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(post_modal).to_contain_text(re.compile(r"Comments|Please log in", re.I), timeout=settings.default_timeout)
|
||||
|
||||
logger.info("关闭帖子详情弹窗")
|
||||
page.keyboard.press("Escape")
|
||||
expect(post_modal).to_be_hidden(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def _click_load_more(page):
|
||||
load_more = page.get_by_text("··· Load More ···").last
|
||||
expect(load_more).to_be_visible(timeout=settings.default_timeout)
|
||||
logger.info("滑到底部并点击 Load More")
|
||||
load_more.scroll_into_view_if_needed()
|
||||
page.wait_for_timeout(500)
|
||||
load_more.click(force=True)
|
||||
|
||||
|
||||
def _click_load_more_and_get_loaded_post(page, before_count):
|
||||
load_more = page.get_by_text("··· Load More ···").last
|
||||
expect(load_more).to_be_visible(timeout=settings.default_timeout)
|
||||
logger.info("滑到底部并点击 Load More")
|
||||
load_more.scroll_into_view_if_needed()
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
load_more.click(force=True)
|
||||
|
||||
try:
|
||||
expect(_post_cards(page)).to_have_count(before_count + 1, timeout=settings.default_timeout)
|
||||
except AssertionError:
|
||||
logger.info("Load More 后帖子数量未增加,继续使用当前列表最后一个帖子")
|
||||
page.wait_for_timeout(1_000)
|
||||
|
||||
current_count = _post_cards(page).count()
|
||||
logger.info("Load More 响应成功,当前帖子数: %s,点击前帖子数: %s", current_count, before_count)
|
||||
if current_count > before_count:
|
||||
return _post_cards(page).nth(before_count)
|
||||
|
||||
logger.info("Load More 后页面未追加帖子,改为点击当前列表最后一个帖子")
|
||||
return _post_cards(page).last
|
||||
|
||||
|
||||
def _login(page):
|
||||
login_as_configured_user_if_needed(page)
|
||||
expect(page.locator(POST_DETAIL_MODAL).filter(has_text="REGISTER/LOGIN")).to_be_hidden(
|
||||
timeout=settings.default_timeout
|
||||
)
|
||||
|
||||
|
||||
def test_guest_view_post_close_then_load_more_show_login_modal(page, ensure_guest_user):
|
||||
_open_home(page)
|
||||
ensure_guest_user()
|
||||
_open_first_post_and_close(page)
|
||||
_click_load_more(page)
|
||||
|
||||
logger.info("校验游客点击 Load More 后页面仍可正常交互")
|
||||
login_modal = page.locator(POST_DETAIL_MODAL).filter(has_text="REGISTER/LOGIN")
|
||||
try:
|
||||
expect(login_modal).to_be_visible(timeout=5_000)
|
||||
expect(login_modal).to_contain_text("Register/Login", timeout=settings.default_timeout)
|
||||
except AssertionError:
|
||||
logger.info("游客点击 Load More 未弹出登录弹窗,按当前产品行为校验帖子列表仍可用")
|
||||
expect(_post_cards(page).first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def test_login_view_post_close_then_load_more_open_new_post_and_close(page, ensure_logged_in_user):
|
||||
_open_home(page)
|
||||
ensure_logged_in_user()
|
||||
_open_first_post_and_close(page)
|
||||
|
||||
before_count = _post_cards(page).count()
|
||||
|
||||
logger.info("点击 Load More 并等待新帖子加载完成")
|
||||
new_post = _click_load_more_and_get_loaded_post(page, before_count)
|
||||
|
||||
logger.info("点击加载完成后的帖子")
|
||||
new_post.scroll_into_view_if_needed()
|
||||
expect(new_post).to_be_visible(timeout=settings.default_timeout)
|
||||
new_post.click()
|
||||
|
||||
post_modal = page.locator(POST_DETAIL_MODAL).last
|
||||
expect(post_modal).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(post_modal).to_contain_text(re.compile(r"Comments|Please log in", re.I), timeout=settings.default_timeout)
|
||||
|
||||
logger.info("关闭加载后的新帖子详情弹窗")
|
||||
page.keyboard.press("Escape")
|
||||
expect(post_modal).to_be_hidden(timeout=settings.default_timeout)
|
||||
65
Joyhub_ui_auto_test/tests/test_login_logout.py
Normal file
65
Joyhub_ui_auto_test/tests/test_login_logout.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
def test_login_success_then_logout_wait_3s_and_close_browser(page, ensure_guest_user):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
logger.info("点击登录入口打开登录弹窗")
|
||||
page.locator("aside button").first.click()
|
||||
|
||||
logger.info("动态获取登录弹窗输入框并输入邮箱: %s", settings.login_email)
|
||||
login_inputs = page.locator('input[type="text"]')
|
||||
expect(login_inputs.first).to_be_visible(timeout=settings.default_timeout)
|
||||
login_inputs.nth(0).fill(settings.login_email)
|
||||
|
||||
logger.info("输入验证码")
|
||||
login_inputs.nth(1).fill(settings.verification_code)
|
||||
|
||||
logger.info("动态勾选登录确认项")
|
||||
confirmation_items = page.locator("div.inline-flex.cursor-pointer")
|
||||
expect(confirmation_items.nth(0)).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(confirmation_items.nth(1)).to_be_visible(timeout=settings.default_timeout)
|
||||
confirmation_items.nth(0).click()
|
||||
confirmation_items.nth(1).click()
|
||||
|
||||
logger.info("提交登录")
|
||||
submit_button = page.locator("button").last
|
||||
expect(submit_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
submit_button.click()
|
||||
|
||||
logger.info("等待登录成功后出现退出按钮")
|
||||
logout_button = page.locator("aside button").first
|
||||
expect(logout_button).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
logger.info("登录成功,停留 2 秒")
|
||||
page.wait_for_timeout(2_000)
|
||||
|
||||
logger.info("点击 Logout 退出登录")
|
||||
logout_button.click()
|
||||
|
||||
logger.info("校验退出登录成功后重新出现登录入口")
|
||||
expect(page.locator("aside button").first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
logger.info("退出登录成功,停留 3 秒")
|
||||
page.wait_for_timeout(3_000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
132
Joyhub_ui_auto_test/tests/test_my_order.py
Normal file
132
Joyhub_ui_auto_test/tests/test_my_order.py
Normal file
@@ -0,0 +1,132 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
|
||||
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 tests.test_rewards_shopping_cart_login_logout import (
|
||||
_accept_cookie_if_present,
|
||||
_first_visible,
|
||||
_login_as_configured_user,
|
||||
)
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
MY_ORDER_LINK_SELECTORS = [
|
||||
'a[href*="order" i]',
|
||||
'a:has-text("My Order")',
|
||||
'a:has-text("My Orders")',
|
||||
'button:has-text("My Order")',
|
||||
'button:has-text("My Orders")',
|
||||
'div.cursor-pointer:has-text("My Order")',
|
||||
'div.cursor-pointer:has-text("My Orders")',
|
||||
'li:has-text("My Order")',
|
||||
'li:has-text("My Orders")',
|
||||
'text=My Order',
|
||||
'text=My Orders',
|
||||
]
|
||||
|
||||
ORDER_DETAIL_LINK_SELECTORS = [
|
||||
'a[href*="order" i]',
|
||||
'button:has-text("Detail")',
|
||||
'button:has-text("Details")',
|
||||
'button:has-text("View")',
|
||||
'button:has-text("View Detail")',
|
||||
'button:has-text("View Details")',
|
||||
]
|
||||
|
||||
|
||||
def _click_first_visible_by_selectors(page, selectors, timeout=3_000):
|
||||
for selector in selectors:
|
||||
locator = page.locator(selector)
|
||||
try:
|
||||
if locator.count() == 0:
|
||||
continue
|
||||
candidate = _first_visible(locator)
|
||||
expect(candidate).to_be_visible(timeout=timeout)
|
||||
candidate.click()
|
||||
return selector
|
||||
except (AssertionError, PlaywrightTimeoutError):
|
||||
continue
|
||||
raise AssertionError(f"未找到可点击的可见元素: {selectors}")
|
||||
|
||||
|
||||
def _wait_login_completed(page):
|
||||
logger.info("等待登录完成并展示用户菜单入口")
|
||||
page.wait_for_function(
|
||||
"""
|
||||
() => {
|
||||
const visibleText = Array.from(document.querySelectorAll('a,button,div,li,span'))
|
||||
.filter(element => !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length))
|
||||
.map(element => element.innerText || element.textContent || '')
|
||||
.join(' ');
|
||||
return /My Order|Logout|Create/i.test(visibleText) && !/^\\s*Login\\s*$/.test(visibleText);
|
||||
}
|
||||
""",
|
||||
timeout=settings.default_timeout,
|
||||
)
|
||||
|
||||
|
||||
def _open_my_order_from_home(page):
|
||||
logger.info("从首页查找 My Order 入口")
|
||||
try:
|
||||
selector = _click_first_visible_by_selectors(page, MY_ORDER_LINK_SELECTORS)
|
||||
logger.info("已通过首页可见入口进入 My Order: %s", selector)
|
||||
return
|
||||
except AssertionError:
|
||||
logger.info("首页未直接展示 My Order,尝试点击用户侧边栏入口后继续查找")
|
||||
|
||||
aside_buttons = page.locator("aside button")
|
||||
expect(aside_buttons.first).to_be_visible(timeout=settings.default_timeout)
|
||||
aside_buttons.first.click()
|
||||
page.wait_for_timeout(1_000)
|
||||
|
||||
selector = _click_first_visible_by_selectors(page, MY_ORDER_LINK_SELECTORS, timeout=settings.default_timeout)
|
||||
logger.info("已通过用户菜单进入 My Order: %s", selector)
|
||||
|
||||
|
||||
def _open_first_order_detail(page):
|
||||
logger.info("等待进入订单列表页")
|
||||
expect(page).to_have_url(re.compile(r".*order.*", re.I), timeout=settings.default_timeout)
|
||||
expect(page.get_by_text(re.compile(r"my order|orders?|order", re.I)).first).to_be_visible(
|
||||
timeout=settings.default_timeout
|
||||
)
|
||||
|
||||
logger.info("点击第一笔订单详情入口")
|
||||
before_url = page.url
|
||||
selector = _click_first_visible_by_selectors(page, ORDER_DETAIL_LINK_SELECTORS, timeout=settings.default_timeout)
|
||||
logger.info("已点击订单详情入口: %s", selector)
|
||||
|
||||
try:
|
||||
page.wait_for_url(lambda url: url != before_url, timeout=settings.default_timeout)
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("点击第一笔订单后 URL 未变化,继续校验当前页面是否展示详情内容")
|
||||
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(page.get_by_text(re.compile(r"order|detail|status|total|payment|shipping", re.I)).first).to_be_visible(
|
||||
timeout=settings.default_timeout
|
||||
)
|
||||
|
||||
|
||||
def test_home_my_order_first_order_detail_after_login(page, ensure_logged_in_user):
|
||||
logger.info("登录用户从首页进入 My Order 并查看第一笔订单详情")
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_logged_in_user()
|
||||
_wait_login_completed(page)
|
||||
|
||||
_open_my_order_from_home(page)
|
||||
_open_first_order_detail(page)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
172
Joyhub_ui_auto_test/tests/test_navigation_pages.py
Normal file
172
Joyhub_ui_auto_test/tests/test_navigation_pages.py
Normal file
@@ -0,0 +1,172 @@
|
||||
from pathlib import Path
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
def test_header_download_app_navigation_then_home_return(page, ensure_guest_user):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
logger.info("点击顶部 /app 导航链接")
|
||||
page.locator('header a[href="/app"]').first.click()
|
||||
|
||||
logger.info("校验跳转到 /app 页面")
|
||||
expect(page).to_have_url(re.compile(r".*/app/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
logger.info("点击顶部首页链接返回首页")
|
||||
page.locator('header a[href="/"]').first.click()
|
||||
|
||||
logger.info("校验返回首页成功")
|
||||
expect(page).to_have_url(re.compile(r".*/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator('button').first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def test_footer_navigation_to_partnerships_and_faq(page, ensure_guest_user):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
logger.info("校验底部 Partnerships 和 FAQs 链接存在")
|
||||
expect(page.locator('footer a[href="/partnerships"]')).to_be_attached(timeout=settings.default_timeout)
|
||||
expect(page.locator('footer a[href="/faq"]')).to_be_attached(timeout=settings.default_timeout)
|
||||
|
||||
logger.info("打开 Partnerships 页面")
|
||||
page.goto("/partnerships", wait_until="domcontentloaded")
|
||||
|
||||
logger.info("校验跳转到 Partnerships 页面")
|
||||
expect(page).to_have_url(re.compile(r".*/partnerships/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
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)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def test_about_us_blog_random_category_article_browse_success(page, ensure_guest_user, attach_page_screenshot):
|
||||
logger.info("打开首页并进入 About Us 下的 Blog 页面")
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
page.wait_for_timeout(2_000)
|
||||
|
||||
about_us_menu = page.get_by_text("About Us", exact=True).first
|
||||
expect(about_us_menu).to_be_visible(timeout=settings.default_timeout)
|
||||
about_us_menu.click()
|
||||
|
||||
blog_link = page.locator('a[href="/blog-detail"]', has_text="Blog").first
|
||||
expect(blog_link).to_be_visible(timeout=settings.default_timeout)
|
||||
blog_link.click()
|
||||
|
||||
logger.info("校验 Blog 页面加载成功")
|
||||
expect(page).to_have_url(re.compile(r".*/blog-detail/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_contain_text("SEXUAL WELLNESS HUB", timeout=settings.default_timeout)
|
||||
|
||||
category_buttons = page.locator("button").filter(has_not_text=re.compile(r"^\\d+$|Acce?pet", re.I))
|
||||
expect(category_buttons.first).to_be_visible(timeout=settings.default_timeout)
|
||||
category_count = category_buttons.count()
|
||||
category_indexes = list(range(category_count))
|
||||
random.shuffle(category_indexes)
|
||||
|
||||
selected_category = None
|
||||
article_links = page.locator('a[href^="/blog-detail/"]')
|
||||
for category_index in category_indexes:
|
||||
category_button = category_buttons.nth(category_index)
|
||||
selected_category = category_button.inner_text(timeout=settings.default_timeout).strip()
|
||||
logger.info("随机选择 Blog 分类: %s", selected_category)
|
||||
category_button.click()
|
||||
page.wait_for_load_state("domcontentloaded")
|
||||
try:
|
||||
expect(article_links.first).to_be_visible(timeout=5_000)
|
||||
if article_links.count() > 0:
|
||||
break
|
||||
except AssertionError:
|
||||
logger.info("分类 %s 下暂无可浏览 Blog,继续随机尝试其他分类", selected_category)
|
||||
else:
|
||||
raise AssertionError("所有 Blog 分类下均未找到可浏览文章")
|
||||
|
||||
article_count = article_links.count()
|
||||
article_index = random.randrange(article_count)
|
||||
article_link = article_links.nth(article_index)
|
||||
article_title = article_link.inner_text(timeout=settings.default_timeout).strip()
|
||||
logger.info("随机浏览 Blog 文章: %s", article_title)
|
||||
|
||||
article_href = article_link.get_attribute("href")
|
||||
assert article_href is not None
|
||||
article_link.scroll_into_view_if_needed(timeout=settings.default_timeout)
|
||||
article_link.click()
|
||||
|
||||
logger.info("校验 Blog 详情页加载成功并截图")
|
||||
expect(page).to_have_url(re.compile(r".*/blog-detail/.+"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
if article_title:
|
||||
expect(page.locator("body")).to_contain_text(article_title, timeout=settings.default_timeout)
|
||||
attach_page_screenshot(f"Blog浏览成功_{selected_category}_{article_title or article_href}")
|
||||
|
||||
|
||||
def test_footer_social_media_links_open_then_return_home(page, ensure_guest_user):
|
||||
social_links = [
|
||||
("X", 'a[href="https://x.com/JoyhubOfficial"]', re.compile(r"https://(x|twitter)\.com/.*", re.I)),
|
||||
(
|
||||
"Instagram",
|
||||
'a[href="https://www.instagram.com/joyhub.official/#"]',
|
||||
re.compile(r"https://www\.instagram\.com/.*", re.I),
|
||||
),
|
||||
(
|
||||
"Reddit",
|
||||
'a[href="https://www.reddit.com/r/JoyhubRemote/"]',
|
||||
re.compile(r"https://www\.reddit\.com/.*", re.I),
|
||||
),
|
||||
(
|
||||
"Discord",
|
||||
'a[href="https://discord.com/invite/vZFQbTRZqe"]',
|
||||
re.compile(r"https://discord\.com/.*", re.I),
|
||||
),
|
||||
]
|
||||
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
for name, selector, expected_url in social_links:
|
||||
logger.info("滚动到首页底部,点击 %s 社交媒体按钮", name)
|
||||
page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
|
||||
link = page.locator(selector).first
|
||||
expect(link).to_be_attached(timeout=settings.default_timeout)
|
||||
link.scroll_into_view_if_needed(timeout=settings.default_timeout)
|
||||
|
||||
social_href = link.get_attribute("href")
|
||||
assert social_href is not None
|
||||
|
||||
logger.info("校验 %s 社交媒体页面跳转成功", name)
|
||||
page.goto(social_href, wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(expected_url, timeout=settings.default_timeout)
|
||||
|
||||
logger.info("从 %s 页面返回 JoyHub 首页", name)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator('a[href="https://x.com/JoyhubOfficial"]')).to_be_attached(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
28
Joyhub_ui_auto_test/tests/test_open_chrome_browser.py
Normal file
28
Joyhub_ui_auto_test/tests/test_open_chrome_browser.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
def test_open_chromium_browser_wait_10s_then_close(page, ensure_guest_user):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url)
|
||||
ensure_guest_user()
|
||||
|
||||
logger.info("等待 10 秒")
|
||||
page.wait_for_timeout(10_000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
71
Joyhub_ui_auto_test/tests/test_partnerships_page.py
Normal file
71
Joyhub_ui_auto_test/tests/test_partnerships_page.py
Normal file
@@ -0,0 +1,71 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
def test_partnerships_page_key_content_visible(page):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开 Partnerships 页面")
|
||||
page.goto("/partnerships", wait_until="domcontentloaded")
|
||||
|
||||
logger.info("校验 Partnerships 页面地址、标题和页面内容已加载")
|
||||
expect(page).to_have_url(re.compile(r".*/partnerships/?$"), timeout=settings.default_timeout)
|
||||
assert page.title().strip()
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
visible_headings_count = page.get_by_role("heading").count()
|
||||
logger.info("当前 Partnerships 页面标题元素数量: %s", visible_headings_count)
|
||||
assert visible_headings_count > 0
|
||||
|
||||
|
||||
def test_partnerships_contact_form_fields_visible_and_editable(page):
|
||||
logger.info("使用 pytest-playwright 统一无头浏览器配置")
|
||||
|
||||
logger.info("打开 Partnerships 页面")
|
||||
page.goto("/partnerships", wait_until="domcontentloaded")
|
||||
|
||||
logger.info("动态校验合作表单字段可见并可输入")
|
||||
form_inputs = page.locator('section input[type="text"]:not([readonly])')
|
||||
form_textareas = page.locator("section textarea")
|
||||
submit_button = page.locator("section button").last
|
||||
|
||||
name_input = form_inputs.nth(0)
|
||||
email_input = form_inputs.nth(1)
|
||||
business_input = form_inputs.nth(2)
|
||||
collaboration_input = page.locator('section input[readonly]').first
|
||||
message_input = form_textareas.first
|
||||
|
||||
expect(name_input).to_be_editable(timeout=settings.default_timeout)
|
||||
expect(email_input).to_be_editable(timeout=settings.default_timeout)
|
||||
expect(business_input).to_be_editable(timeout=settings.default_timeout)
|
||||
expect(message_input).to_be_editable(timeout=settings.default_timeout)
|
||||
|
||||
name_input.fill("Joyhub Tester")
|
||||
email_input.fill("tester@example.com")
|
||||
business_input.fill("https://example.com")
|
||||
message_input.fill("I want to explore product testing collaboration.")
|
||||
|
||||
expect(name_input).to_have_value("Joyhub Tester", timeout=settings.default_timeout)
|
||||
expect(email_input).to_have_value("tester@example.com", timeout=settings.default_timeout)
|
||||
expect(business_input).to_have_value("https://example.com", timeout=settings.default_timeout)
|
||||
expect(collaboration_input).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(collaboration_input).to_have_attribute("readonly", "", timeout=settings.default_timeout)
|
||||
expect(message_input).to_have_value("I want to explore product testing collaboration.", timeout=settings.default_timeout)
|
||||
expect(submit_button).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
155
Joyhub_ui_auto_test/tests/test_points_redemption_payment.py
Normal file
155
Joyhub_ui_auto_test/tests/test_points_redemption_payment.py
Normal file
@@ -0,0 +1,155 @@
|
||||
from pathlib import Path
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
|
||||
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 tests.test_rewards_shopping_cart_login_logout import _accept_cookie_if_present
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
REDEEM_NOW_SELECTORS = [
|
||||
'button:has-text("Redeem Now"):not([disabled])',
|
||||
'button:has-text("Redeem now"):not([disabled])',
|
||||
'button:has-text("立即兑换"):not([disabled])',
|
||||
]
|
||||
POINTS_PAY_SELECTORS = [
|
||||
'button:has-text("Pay"):not([disabled])',
|
||||
'button:has-text("Confirm"):not([disabled])',
|
||||
'button:has-text("Submit"):not([disabled])',
|
||||
'button:has-text("Place Order"):not([disabled])',
|
||||
'button:has-text("Redeem"):not([disabled])',
|
||||
'button:has-text("Confirm Redemption"):not([disabled])',
|
||||
'button:has-text("确认"):not([disabled])',
|
||||
'button:has-text("提交"):not([disabled])',
|
||||
'button:has-text("支付"):not([disabled])',
|
||||
]
|
||||
|
||||
|
||||
def _visible_locator(page, selectors, timeout=5_000):
|
||||
for selector in selectors:
|
||||
locator = page.locator(selector).first
|
||||
try:
|
||||
if locator.is_visible(timeout=timeout):
|
||||
return locator
|
||||
except PlaywrightTimeoutError:
|
||||
continue
|
||||
raise AssertionError(f"未找到可见元素: {selectors}")
|
||||
|
||||
|
||||
def _login_in_open_dialog(page):
|
||||
logger.info("在 Redeem Now 触发的登录弹窗中登录")
|
||||
login_inputs = page.locator('input[type="text"]')
|
||||
expect(login_inputs.first).to_be_visible(timeout=settings.default_timeout)
|
||||
login_inputs.nth(0).fill(settings.login_email)
|
||||
login_inputs.nth(1).fill(settings.verification_code)
|
||||
|
||||
confirmation_items = page.locator("div.inline-flex.cursor-pointer")
|
||||
expect(confirmation_items.nth(0)).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(confirmation_items.nth(1)).to_be_visible(timeout=settings.default_timeout)
|
||||
confirmation_items.nth(0).click()
|
||||
confirmation_items.nth(1).click()
|
||||
|
||||
submit_button = page.locator("button").last
|
||||
expect(submit_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
submit_button.click()
|
||||
|
||||
logger.info("等待登录弹窗关闭或兑换流程继续")
|
||||
expect(login_inputs.first).not_to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def _open_points_redemption_from_rewards(page):
|
||||
logger.info("未登录用户点击 Rewards 并进入 Points Redemption")
|
||||
rewards_link = page.locator('header a[href^="javascript:"]').filter(has_text="Rewards").first
|
||||
expect(rewards_link).to_be_attached(timeout=settings.default_timeout)
|
||||
rewards_link.click(force=True)
|
||||
|
||||
try:
|
||||
page.wait_for_function(
|
||||
"""
|
||||
() => Array.from(document.querySelectorAll('a[href="/points-redemption"]'))
|
||||
.some(element => !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length))
|
||||
""",
|
||||
timeout=5_000,
|
||||
)
|
||||
page.locator('a[href="/points-redemption"]').filter(has_text="Points Redemption").first.click()
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("Rewards 下拉入口未稳定展示,直接进入 Points Redemption 页面")
|
||||
page.goto(settings.base_url.rstrip("/") + "/points-redemption", wait_until="domcontentloaded")
|
||||
|
||||
expect(page).to_have_url(re.compile(r".*/points-redemption/?"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def _open_random_redeemable_points_product(page):
|
||||
logger.info("随机选择一个积分商品并查找 Redeem Now")
|
||||
category_buttons = page.locator("section button").filter(has_not_text=re.compile(r"Accepet|Accept", re.I))
|
||||
expect(category_buttons.first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
category_indices = list(range(category_buttons.count()))
|
||||
random.shuffle(category_indices)
|
||||
last_error = None
|
||||
|
||||
for category_index in category_indices:
|
||||
try:
|
||||
logger.info("尝试积分商品分类/商品入口索引: %s", category_index)
|
||||
category_buttons.nth(category_index).click()
|
||||
page.wait_for_timeout(1_500)
|
||||
|
||||
if page.get_by_text(re.compile(r"currently do not support delivery", re.I)).first.is_visible(timeout=1_000):
|
||||
last_error = "当前国家/地区不支持积分商品配送"
|
||||
continue
|
||||
|
||||
redeem_button = _visible_locator(page, REDEEM_NOW_SELECTORS, timeout=5_000)
|
||||
expect(redeem_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
redeem_button.click()
|
||||
return
|
||||
except (AssertionError, PlaywrightTimeoutError) as exc:
|
||||
last_error = exc
|
||||
logger.info("当前积分商品不可兑换,继续尝试下一个: %s", exc)
|
||||
|
||||
pytest.skip(f"当前测试环境未展示可兑换积分商品,无法执行 Redeem Now 主流程: {last_error}")
|
||||
|
||||
|
||||
def _complete_points_payment_after_login(page):
|
||||
logger.info("登录后继续完成积分商品支付/兑换")
|
||||
try:
|
||||
pay_button = _visible_locator(page, POINTS_PAY_SELECTORS, timeout=settings.default_timeout)
|
||||
expect(pay_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
pay_button.click()
|
||||
except AssertionError:
|
||||
logger.info("登录后未出现独立确认按钮,检查是否已自动提交兑换")
|
||||
|
||||
result_text = page.get_by_text(
|
||||
re.compile(r"success|successful|completed|paid|payment|order|redeem|redemption|成功|订单|兑换", re.I)
|
||||
).first
|
||||
expect(result_text).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def test_guest_points_product_redeem_now_login_then_points_payment_result(page, ensure_guest_user):
|
||||
logger.info("未登录用户从 Rewards 选择积分商品,Redeem Now 后登录并完成积分支付")
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
_open_points_redemption_from_rewards(page)
|
||||
_open_random_redeemable_points_product(page)
|
||||
|
||||
logger.info("校验 Redeem Now 后弹出登录弹窗")
|
||||
expect(page.locator('input[type="text"]').first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
_login_in_open_dialog(page)
|
||||
_complete_points_payment_after_login(page)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
@@ -0,0 +1,160 @@
|
||||
from pathlib import Path
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
|
||||
from playwright.sync_api import expect
|
||||
|
||||
from tests.conftest import accept_cookie_if_present, login_as_configured_user_if_needed
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
def _accept_cookie_if_present(page):
|
||||
accept_cookie_if_present(page)
|
||||
|
||||
|
||||
def _first_visible(locator):
|
||||
locator_count = locator.count()
|
||||
for index in range(locator_count):
|
||||
candidate = locator.nth(index)
|
||||
if candidate.is_visible():
|
||||
return candidate
|
||||
raise AssertionError("未找到可见元素")
|
||||
|
||||
|
||||
def _login_as_configured_user(page):
|
||||
login_as_configured_user_if_needed(page)
|
||||
|
||||
|
||||
def _add_random_shopping_product_to_cart(page):
|
||||
logger.info("直接进入 Shopping 页面")
|
||||
page.goto(settings.base_url.rstrip("/") + "/shopping", wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/shopping/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
logger.info("动态获取分类列表并随机选择商品加购")
|
||||
category_buttons = page.locator("section > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) button")
|
||||
expect(category_buttons.first).to_be_visible(timeout=settings.default_timeout)
|
||||
category_count = category_buttons.count()
|
||||
logger.info("当前分类数量: %s", category_count)
|
||||
assert category_count > 0
|
||||
|
||||
category_indices = list(range(category_count))
|
||||
random.shuffle(category_indices)
|
||||
logger.info("随机分类尝试顺序: %s", category_indices)
|
||||
|
||||
last_error = None
|
||||
for selected_category_index in category_indices:
|
||||
logger.info("随机选择分类索引: %s", selected_category_index)
|
||||
category_buttons.nth(selected_category_index).click()
|
||||
page.wait_for_timeout(1_000)
|
||||
|
||||
logger.info("动态获取商品列表")
|
||||
product_links = page.locator('section a[href^="/shopping/"][href*="selectedSkuId"]').evaluate_all(
|
||||
"""
|
||||
links => Array.from(new Map(
|
||||
links
|
||||
.map(link => link.getAttribute('href'))
|
||||
.filter(Boolean)
|
||||
.map(href => [href, href])
|
||||
).values())
|
||||
"""
|
||||
)
|
||||
logger.info("当前可进入详情的商品数量: %s", len(product_links))
|
||||
random.shuffle(product_links)
|
||||
|
||||
for selected_product_href in product_links:
|
||||
try:
|
||||
logger.info("尝试加购商品: %s", selected_product_href)
|
||||
page.goto(settings.base_url.rstrip("/") + selected_product_href, wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/shopping/.+selectedSkuId=.+"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
logger.info("动态获取商品规格行,逐行随机选择一个可用规格")
|
||||
spec_rows = page.locator(".product-specs-scrollbar > div").filter(has=page.locator("button"))
|
||||
expect(spec_rows.first).to_be_visible(timeout=settings.default_timeout)
|
||||
spec_row_count = spec_rows.count()
|
||||
logger.info("当前规格行数量: %s", spec_row_count)
|
||||
assert spec_row_count > 0
|
||||
|
||||
selected_spec_count = 0
|
||||
for row_index in range(spec_row_count):
|
||||
spec_options = spec_rows.nth(row_index).locator("button:not([disabled])")
|
||||
spec_option_count = spec_options.count()
|
||||
logger.info("规格行 %s 可选项数量: %s", row_index, spec_option_count)
|
||||
if spec_option_count == 0:
|
||||
continue
|
||||
|
||||
selected_spec_index = random.randrange(spec_option_count)
|
||||
logger.info("规格行 %s 随机选择选项索引: %s", row_index, selected_spec_index)
|
||||
spec_options.nth(selected_spec_index).click()
|
||||
selected_spec_count += 1
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
assert selected_spec_count > 0
|
||||
|
||||
logger.info("动态点击数量增加按钮")
|
||||
increase_quantity_button = page.locator("button:has(svg.lucide-plus):not([disabled])").first
|
||||
try:
|
||||
if increase_quantity_button.is_visible(timeout=3_000):
|
||||
increase_click_count = random.randint(1, 3)
|
||||
logger.info("随机增加商品数量次数: %s", increase_click_count)
|
||||
for _ in range(increase_click_count):
|
||||
increase_quantity_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("当前商品未展示数量增加按钮,使用默认购买数量")
|
||||
|
||||
logger.info("动态获取加购按钮并点击")
|
||||
add_to_cart_button = page.locator("button.bg-primary.text-white:not([disabled])").first
|
||||
expect(add_to_cart_button).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(add_to_cart_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
add_to_cart_button.click()
|
||||
|
||||
logger.info("校验购物车入口仍可见,表示加购流程已完成点击")
|
||||
expect(page.locator('a[href="/view-cart"]').first).to_be_visible(timeout=settings.default_timeout)
|
||||
return
|
||||
except (AssertionError, PlaywrightTimeoutError) as exc:
|
||||
last_error = exc
|
||||
logger.info("当前商品不可加购,继续尝试下一个商品: %s", exc)
|
||||
page.goto(settings.base_url.rstrip("/") + "/shopping", wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/shopping/?$"), timeout=settings.default_timeout)
|
||||
category_buttons = page.locator(
|
||||
"section > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) button"
|
||||
)
|
||||
category_buttons.nth(selected_category_index).click()
|
||||
page.wait_for_timeout(1_000)
|
||||
|
||||
raise AssertionError(f"未找到可加购的随机商品: {last_error}")
|
||||
|
||||
|
||||
def test_rewards_shopping_random_product_add_to_cart_no_login(page, ensure_guest_user):
|
||||
logger.info("使用游客身份执行 Rewards Shopping 随机商品加购")
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
_add_random_shopping_product_to_cart(page)
|
||||
|
||||
|
||||
def test_rewards_shopping_random_product_add_to_cart_after_login(page, ensure_logged_in_user):
|
||||
logger.info("使用登录用户身份执行 Rewards Shopping 随机商品加购")
|
||||
logger.info("打开链接地址: %s", settings.base_url)
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_logged_in_user()
|
||||
|
||||
_add_random_shopping_product_to_cart(page)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
271
Joyhub_ui_auto_test/tests/test_rewards_shopping_payment.py
Normal file
271
Joyhub_ui_auto_test/tests/test_rewards_shopping_payment.py
Normal file
@@ -0,0 +1,271 @@
|
||||
from pathlib import Path
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from playwright.sync_api import Error as PlaywrightError
|
||||
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
|
||||
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 tests.test_rewards_shopping_cart_login_logout import (
|
||||
_accept_cookie_if_present,
|
||||
_first_visible,
|
||||
_login_as_configured_user,
|
||||
)
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
PAYPAL_EMAIL_SELECTORS = [
|
||||
'input[name="login_email"]',
|
||||
'input[name="email"]',
|
||||
'input#email',
|
||||
]
|
||||
PAYPAL_PASSWORD_SELECTORS = [
|
||||
'input[name="login_password"]',
|
||||
'input[name="password"]',
|
||||
'input#password',
|
||||
]
|
||||
PAYPAL_NEXT_SELECTORS = [
|
||||
'button:has-text("Next")',
|
||||
'input[type="submit"][value="Next"]',
|
||||
'button#btnNext',
|
||||
]
|
||||
PAYPAL_LOGIN_SELECTORS = [
|
||||
'button:has-text("Log In")',
|
||||
'button:has-text("Log in")',
|
||||
'button:has-text("Login")',
|
||||
'button#btnLogin',
|
||||
'input[type="submit"][value="Log In"]',
|
||||
]
|
||||
PAYPAL_PAY_SELECTORS = [
|
||||
'button:has-text("Complete Purchase")',
|
||||
'button:has-text("Pay Now")',
|
||||
'button:has-text("Agree and Pay")',
|
||||
'button:has-text("Continue")',
|
||||
'button:has-text("Review Order")',
|
||||
'input[type="submit"][value*="Pay"]',
|
||||
]
|
||||
|
||||
|
||||
def _visible_locator(page, selectors, timeout=5_000):
|
||||
for selector in selectors:
|
||||
locator = page.locator(selector).first
|
||||
try:
|
||||
if locator.is_visible(timeout=timeout):
|
||||
return locator
|
||||
except PlaywrightTimeoutError:
|
||||
continue
|
||||
raise AssertionError(f"未找到可见元素: {selectors}")
|
||||
|
||||
|
||||
def _add_random_shopping_product_directly(page):
|
||||
logger.info("直接进入 Shopping 页面并随机加购商品")
|
||||
page.goto(settings.base_url.rstrip("/") + "/shopping", wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/shopping/?$"), timeout=settings.default_timeout)
|
||||
|
||||
category_buttons = page.locator("section > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) button")
|
||||
expect(category_buttons.first).to_be_visible(timeout=settings.default_timeout)
|
||||
category_indices = list(range(category_buttons.count()))
|
||||
random.shuffle(category_indices)
|
||||
|
||||
product_links = []
|
||||
for selected_category_index in category_indices:
|
||||
category_buttons.nth(selected_category_index).click()
|
||||
page.wait_for_timeout(1_000)
|
||||
product_links = page.locator('section a[href^="/shopping/"][href*="selectedSkuId"]').evaluate_all(
|
||||
"""
|
||||
links => Array.from(new Map(
|
||||
links
|
||||
.map(link => link.getAttribute('href'))
|
||||
.filter(Boolean)
|
||||
.map(href => [href, href])
|
||||
).values())
|
||||
"""
|
||||
)
|
||||
if product_links:
|
||||
break
|
||||
|
||||
assert product_links
|
||||
selected_product_href = random.choice(product_links)
|
||||
_first_visible(page.locator(f'section a[href="{selected_product_href}"]')).click()
|
||||
expect(page).to_have_url(re.compile(r".*/shopping/.+selectedSkuId=.+"), timeout=settings.default_timeout)
|
||||
|
||||
spec_rows = page.locator(".product-specs-scrollbar > div").filter(has=page.locator("button"))
|
||||
expect(spec_rows.first).to_be_visible(timeout=settings.default_timeout)
|
||||
selected_spec_count = 0
|
||||
for row_index in range(spec_rows.count()):
|
||||
spec_options = spec_rows.nth(row_index).locator("button:not([disabled])")
|
||||
spec_option_count = spec_options.count()
|
||||
if spec_option_count == 0:
|
||||
continue
|
||||
spec_options.nth(random.randrange(spec_option_count)).click()
|
||||
selected_spec_count += 1
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
assert selected_spec_count > 0
|
||||
increase_quantity_button = page.locator("button:has(svg.lucide-plus):not([disabled])").first
|
||||
try:
|
||||
if increase_quantity_button.is_visible(timeout=3_000):
|
||||
for _ in range(random.randint(1, 3)):
|
||||
increase_quantity_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("当前商品未展示数量增加按钮,使用默认购买数量")
|
||||
|
||||
add_to_cart_button = page.locator("button.bg-primary.text-white:not([disabled])").first
|
||||
expect(add_to_cart_button).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(add_to_cart_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
add_to_cart_button.click()
|
||||
expect(page.locator('a[href="/view-cart"]').first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def _go_to_cart(page):
|
||||
logger.info("进入购物车页面")
|
||||
page.goto(settings.base_url.rstrip("/") + "/view-cart", wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/view-cart/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator('iframe[title="PayPal-paypal"]').first).to_be_attached(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def _add_random_one_or_more_products(page):
|
||||
product_count = random.randint(1, 2)
|
||||
logger.info("随机加入商品数量种类: %s", product_count)
|
||||
for index in range(product_count):
|
||||
logger.info("执行第 %s 次随机商品加购", index + 1)
|
||||
_add_random_shopping_product_directly(page)
|
||||
if index < product_count - 1:
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
|
||||
|
||||
def _trigger_paypal_payment(page):
|
||||
logger.info("等待 PayPal 支付按钮渲染")
|
||||
expect(page.locator('iframe[title="PayPal-paypal"]').first).to_be_attached(timeout=settings.default_timeout)
|
||||
page.wait_for_timeout(3_000)
|
||||
|
||||
logger.info("坐标点击页面主 DOM 中可见的 PayPal 按钮")
|
||||
paypal_buttons = page.locator('button[class*="FFCE0C"]')
|
||||
for index in range(paypal_buttons.count()):
|
||||
paypal_button = paypal_buttons.nth(index)
|
||||
try:
|
||||
if not paypal_button.is_visible(timeout=1_000):
|
||||
continue
|
||||
if not paypal_button.is_enabled(timeout=1_000):
|
||||
continue
|
||||
|
||||
box = paypal_button.bounding_box()
|
||||
if not box:
|
||||
continue
|
||||
|
||||
try:
|
||||
with page.expect_popup(timeout=10_000) as popup_info:
|
||||
page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
|
||||
paypal_page = popup_info.value
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("未捕获到 PayPal 弹窗,检查当前页是否已跳转")
|
||||
page.wait_for_timeout(3_000)
|
||||
if "paypal" in page.url.lower():
|
||||
paypal_page = page
|
||||
else:
|
||||
continue
|
||||
|
||||
try:
|
||||
paypal_page.wait_for_load_state("domcontentloaded", timeout=settings.default_timeout)
|
||||
try:
|
||||
paypal_page.wait_for_url(re.compile(r".*paypal.*"), timeout=settings.default_timeout)
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("PayPal 页面当前地址未包含 paypal: %s", paypal_page.url)
|
||||
expect(paypal_page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
return paypal_page
|
||||
except PlaywrightError as exc:
|
||||
logger.info("PayPal 弹窗已被触发但提前关闭: %s", exc)
|
||||
return None
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("当前 PayPal 按钮未触发支付页,继续尝试下一个按钮")
|
||||
|
||||
raise AssertionError("未找到可点击并能打开支付页的 PayPal 按钮")
|
||||
|
||||
|
||||
def _login_paypal_if_required(paypal_page):
|
||||
logger.info("处理 PayPal 登录页面")
|
||||
try:
|
||||
email_input = _visible_locator(paypal_page, PAYPAL_EMAIL_SELECTORS)
|
||||
except AssertionError:
|
||||
logger.info("PayPal 当前页面未出现邮箱输入框,可能已保持登录态")
|
||||
return
|
||||
|
||||
email_input.fill(settings.paypal_email)
|
||||
|
||||
try:
|
||||
next_button = _visible_locator(paypal_page, PAYPAL_NEXT_SELECTORS, timeout=3_000)
|
||||
next_button.click()
|
||||
except AssertionError:
|
||||
logger.info("PayPal 未出现 Next 按钮,继续查找密码输入框")
|
||||
|
||||
password_input = _visible_locator(paypal_page, PAYPAL_PASSWORD_SELECTORS)
|
||||
password_input.fill(settings.paypal_password)
|
||||
|
||||
login_button = _visible_locator(paypal_page, PAYPAL_LOGIN_SELECTORS)
|
||||
expect(login_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
login_button.click()
|
||||
paypal_page.wait_for_load_state("domcontentloaded", timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def _complete_paypal_payment(page, paypal_page):
|
||||
logger.info("在 PayPal 页面确认支付")
|
||||
pay_button = _visible_locator(paypal_page, PAYPAL_PAY_SELECTORS, timeout=settings.default_timeout)
|
||||
expect(pay_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
pay_button.click()
|
||||
|
||||
logger.info("等待 PayPal 支付完成并返回商户站点")
|
||||
try:
|
||||
paypal_page.wait_for_close(timeout=settings.default_timeout)
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("PayPal 弹窗未自动关闭,继续等待主页面跳转")
|
||||
|
||||
page.wait_for_load_state("domcontentloaded", timeout=settings.default_timeout)
|
||||
expect(page).to_have_url(re.compile(r".*joyhub-website-frontend-test.*"), timeout=settings.default_timeout)
|
||||
|
||||
success_text = page.get_by_text(re.compile(r"success|paid|payment|order", re.I)).first
|
||||
expect(success_text).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def _pay_random_shopping_products(page):
|
||||
_add_random_one_or_more_products(page)
|
||||
_go_to_cart(page)
|
||||
paypal_page = _trigger_paypal_payment(page)
|
||||
|
||||
if not settings.run_paypal_payment:
|
||||
logger.info("RUN_PAYPAL_PAYMENT 未开启,仅校验支付入口可触发")
|
||||
expect(page).to_have_url(re.compile(r".*/view-cart/?$"), timeout=settings.default_timeout)
|
||||
return
|
||||
|
||||
assert paypal_page is not None, "PayPal 弹窗已触发但提前关闭,无法继续真实付款"
|
||||
_login_paypal_if_required(paypal_page)
|
||||
_complete_paypal_payment(page, paypal_page)
|
||||
|
||||
|
||||
def test_rewards_shopping_random_products_payment_no_login(page, ensure_guest_user):
|
||||
logger.info("使用游客身份执行随机商品 PayPal 支付")
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
_pay_random_shopping_products(page)
|
||||
|
||||
|
||||
def test_rewards_shopping_random_products_payment_after_login(page, ensure_logged_in_user):
|
||||
logger.info("使用登录用户身份执行随机商品 PayPal 支付")
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_logged_in_user()
|
||||
|
||||
_pay_random_shopping_products(page)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
@@ -0,0 +1,188 @@
|
||||
from pathlib import Path
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
|
||||
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__)
|
||||
|
||||
LANGUAGE_OPTIONS = ["English", "Français"]
|
||||
|
||||
|
||||
def _visible_header(page):
|
||||
headers = page.locator("header")
|
||||
for index in range(headers.count()):
|
||||
header = headers.nth(index)
|
||||
try:
|
||||
if header.is_visible(timeout=1_000):
|
||||
return header
|
||||
except PlaywrightTimeoutError:
|
||||
continue
|
||||
raise AssertionError("未找到可见的页面头部")
|
||||
|
||||
|
||||
def _visible_dropdown_options(page):
|
||||
return page.locator('div[class*="hover:bg-grey700"]').filter(visible=True)
|
||||
|
||||
|
||||
def _select_random_language(page):
|
||||
header = _visible_header(page)
|
||||
language_trigger = header.locator("div.cursor-pointer").nth(3)
|
||||
expect(language_trigger).to_be_visible(timeout=settings.default_timeout)
|
||||
language_trigger.click()
|
||||
|
||||
options = _visible_dropdown_options(page)
|
||||
available_languages = []
|
||||
for index in range(options.count()):
|
||||
option_text = options.nth(index).inner_text(timeout=3_000).strip()
|
||||
if option_text in LANGUAGE_OPTIONS:
|
||||
available_languages.append(option_text)
|
||||
|
||||
assert available_languages, "语言下拉没有可选项"
|
||||
selected_language = random.choice(available_languages)
|
||||
logger.info("随机选择语言: %s,可选语言: %s", selected_language, available_languages)
|
||||
options.filter(has_text=re.compile(rf"^{re.escape(selected_language)}$")).first.click()
|
||||
page.wait_for_load_state("domcontentloaded", timeout=settings.default_timeout)
|
||||
page.wait_for_timeout(1_000)
|
||||
return selected_language
|
||||
|
||||
|
||||
def _select_random_country(page):
|
||||
header = _visible_header(page)
|
||||
country_trigger = header.locator("div.cursor-pointer").nth(4)
|
||||
expect(country_trigger).to_be_visible(timeout=settings.default_timeout)
|
||||
country_trigger.click()
|
||||
|
||||
options = _visible_dropdown_options(page)
|
||||
expect(options.first).to_be_visible(timeout=settings.default_timeout)
|
||||
countries = []
|
||||
for index in range(options.count()):
|
||||
country_text = options.nth(index).inner_text(timeout=3_000).strip()
|
||||
if country_text and country_text not in LANGUAGE_OPTIONS:
|
||||
countries.append(country_text)
|
||||
|
||||
assert countries, "国家下拉没有可选项"
|
||||
selected_country = random.choice(countries)
|
||||
logger.info("随机选择国家: %s,可选国家数量: %s", selected_country, len(countries))
|
||||
options.filter(has_text=re.compile(rf"^{re.escape(selected_country)}$")).first.click()
|
||||
page.wait_for_load_state("domcontentloaded", timeout=settings.default_timeout)
|
||||
page.wait_for_timeout(1_000)
|
||||
return selected_country
|
||||
|
||||
|
||||
def _shopping_product_links(page):
|
||||
return page.locator('section a[href^="/shopping/"][href*="selectedSkuId"]').evaluate_all(
|
||||
"""
|
||||
links => Array.from(new Map(
|
||||
links
|
||||
.map(link => link.getAttribute('href'))
|
||||
.filter(Boolean)
|
||||
.map(href => [href, href])
|
||||
).values())
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def _find_random_product_href_or_none(page):
|
||||
logger.info("进入 Shopping 页面,查找当前国家可选商品")
|
||||
page.goto(settings.base_url.rstrip("/") + "/shopping", wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/shopping/?$"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
page.wait_for_timeout(2_000)
|
||||
|
||||
category_buttons = page.locator("section > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) button")
|
||||
try:
|
||||
if not category_buttons.first.is_visible(timeout=5_000):
|
||||
logger.info("当前国家 Shopping 页面无分类,认为没有商品可选")
|
||||
return None
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("当前国家 Shopping 页面未加载到分类,认为没有商品可选")
|
||||
return None
|
||||
|
||||
category_indices = list(range(category_buttons.count()))
|
||||
random.shuffle(category_indices)
|
||||
logger.info("随机分类尝试顺序: %s", category_indices)
|
||||
|
||||
for category_index in category_indices:
|
||||
category_buttons.nth(category_index).click()
|
||||
page.wait_for_timeout(1_000)
|
||||
product_links = _shopping_product_links(page)
|
||||
logger.info("分类索引 %s 可进入详情的商品数量: %s", category_index, len(product_links))
|
||||
if product_links:
|
||||
return random.choice(product_links)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _add_product_to_cart(page, product_href):
|
||||
logger.info("进入随机商品详情并加购: %s", product_href)
|
||||
page.goto(settings.base_url.rstrip("/") + product_href, wait_until="domcontentloaded")
|
||||
expect(page).to_have_url(re.compile(r".*/shopping/.+selectedSkuId=.+"), timeout=settings.default_timeout)
|
||||
expect(page.locator("body")).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
spec_rows = page.locator(".product-specs-scrollbar > div").filter(has=page.locator("button"))
|
||||
expect(spec_rows.first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
selected_spec_count = 0
|
||||
for row_index in range(spec_rows.count()):
|
||||
spec_options = spec_rows.nth(row_index).locator("button:not([disabled])")
|
||||
spec_option_count = spec_options.count()
|
||||
if spec_option_count == 0:
|
||||
continue
|
||||
selected_spec_index = random.randrange(spec_option_count)
|
||||
logger.info("规格行 %s 随机选择选项索引: %s", row_index, selected_spec_index)
|
||||
spec_options.nth(selected_spec_index).click()
|
||||
selected_spec_count += 1
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
assert selected_spec_count > 0, "商品详情没有可选规格"
|
||||
|
||||
increase_quantity_button = page.locator("button:has(svg.lucide-plus):not([disabled])").first
|
||||
try:
|
||||
if increase_quantity_button.is_visible(timeout=3_000):
|
||||
increase_click_count = random.randint(1, 3)
|
||||
logger.info("随机增加商品数量次数: %s", increase_click_count)
|
||||
for _ in range(increase_click_count):
|
||||
increase_quantity_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
except PlaywrightTimeoutError:
|
||||
logger.info("当前商品未展示数量增加按钮,使用默认购买数量")
|
||||
|
||||
add_to_cart_button = page.locator("button.bg-primary.text-white:not([disabled])").first
|
||||
expect(add_to_cart_button).to_be_visible(timeout=settings.default_timeout)
|
||||
expect(add_to_cart_button).to_be_enabled(timeout=settings.default_timeout)
|
||||
add_to_cart_button.click()
|
||||
|
||||
expect(page.locator('a[href="/view-cart"]').first).to_be_visible(timeout=settings.default_timeout)
|
||||
|
||||
|
||||
def test_switch_random_language_country_then_add_shopping_product_to_cart(page, ensure_guest_user):
|
||||
logger.info("打开 JoyHub 首页并确保游客身份")
|
||||
page.goto(settings.base_url, wait_until="domcontentloaded")
|
||||
ensure_guest_user()
|
||||
|
||||
selected_language = _select_random_language(page)
|
||||
selected_country = _select_random_country(page)
|
||||
|
||||
product_href = _find_random_product_href_or_none(page)
|
||||
if not product_href:
|
||||
logger.info("语言 %s / 国家 %s 没有商品可选,用例按预期通过", selected_language, selected_country)
|
||||
assert True
|
||||
return
|
||||
|
||||
_add_product_to_cart(page, product_href)
|
||||
logger.info("语言 %s / 国家 %s 商品加购成功", selected_language, selected_country)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(pytest.main([str(Path(__file__))]))
|
||||
Reference in New Issue
Block a user