文件
nahaichuan/tests/test_homepage_button_links.py

150 行
8.1 KiB
Python

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
首页按钮点击与跳转审查自动化测试 (TDD)
验证首页所有按钮(PC 端与移动端)均准确跳转到对应的业务位置或锚点
"""
import os
import re
import unittest
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
class HomepageButtonLinksTest(unittest.TestCase):
def setUp(self):
self.pc_home_path = os.path.join(BASE_DIR, 'addons', 'official', 'view', 'web', 'official_home', 'index.html')
self.mobile_home_path = os.path.join(BASE_DIR, 'addons', 'official', 'view', 'web', 'official_home', 'index_e.html')
self.pc_nav_path = os.path.join(BASE_DIR, 'addons', 'official', 'view', 'web', 'official_common', 'pc', 'nav.html')
self.css_path = os.path.join(BASE_DIR, 'public', 'assets', 'addons', 'official', 'css', 'modern_nhc.css')
with open(self.pc_home_path, 'r', encoding='utf-8') as f:
self.pc_html = f.read()
with open(self.mobile_home_path, 'r', encoding='utf-8') as f:
self.mobile_html = f.read()
with open(self.pc_nav_path, 'r', encoding='utf-8') as f:
self.pc_nav = f.read()
with open(self.css_path, 'r', encoding='utf-8') as f:
self.css = f.read()
def test_01_pc_hero_carousel_buttons(self):
"""PC 首页首屏轮播图所有按钮跳转必须精准"""
# 幻灯片 1:预约架构师评估 与 浏览成熟产品
self.assertIn('#contact', self.pc_html, "首屏应包含直达联系模块的锚点跳转")
self.assertIn('#products', self.pc_html, "首屏应包含直达产品矩阵的锚点跳转")
# 幻灯片 2:软件开发能力 与 标杆案例
self.assertTrue(
"official_product/list" in self.pc_html and ":pid'=>19" in self.pc_html,
"幻灯片2必须准确跳转到服务页 (:pid=>19)"
)
self.assertTrue(
"official_case_show/list" in self.pc_html and ":pid'=>14" in self.pc_html,
"幻灯片2必须准确跳转到案例页 (:pid=>14)"
)
# 幻灯片 4:申请全功能在线演示 - 严禁粗糙跳到没有演示功能的 #products
# 必须跳转到产品页演示沙箱 demo-sandbox 或联系预约
self.assertFalse(
re.search(r'<a\s+href="#products"[^>]*>(?:(?!</a>)[\s\S])*?申请全功能在线演示', self.pc_html),
"申请全功能在线演示严禁跳往 #products,必须精准跳转演示沙箱或预约页面"
)
self.assertTrue(
"demo-sandbox" in self.pc_html or "official_online_message/list" in self.pc_html,
"申请在线演示必须链接到 demo-sandbox 或联系预约"
)
def test_02_pc_service_section_buttons(self):
"""PC 首页服务板块跳转必须准确一致"""
# 核心服务一与二必须跳转服务页
self.assertTrue(
re.search(r'href="\{:addon_url\(\'official/web\.official_product/list\',\[\':pid\'=>19,\':sid\'=>19\]\)[^"]*"[^>]*>[\s\S]*?软件开发', self.pc_html) or
re.search(r'软件开发方案[\s\S]*?\{:addon_url\(\'official/web\.official_product/list\'', self.pc_html) or
("了解软件开发" in self.pc_html and ":pid'=>19" in self.pc_html),
"软件开发服务卡片必须跳转到服务页"
)
# 核心服务三:如果是“探索智能体开发方案”,跳转到服务页;如果是“案例”,必须跳案例页,文案与链接不能精神分裂
if "探索智能体落地案例" in self.pc_html:
# 如果文案是案例,则链接必须是案例页
self.assertTrue(
"official_case_show/list" in self.pc_html,
"探索智能体落地案例必须指向案例页"
)
def test_03_pc_product_section_buttons(self):
"""PC 首页四大成熟商用系统按钮必须跳转到对应位置"""
# 收银「申请演示」必须跳往产品页的演示沙箱 #demo-sandbox 或联系页
self.assertTrue(
re.search(r'href="\{:addon_url\(\'official/web\.official_product/list\',\[\':pid\'=>22,\':sid\'=>22\]\)\}#demo-sandbox"[^>]*>[\s\S]*?申请演示', self.pc_html) or
("demo-sandbox" in self.pc_html and "申请演示" in self.pc_html),
"收银系统【申请演示】必须精准跳转到产品独立页演示沙箱"
)
# SaaS版本「立即开通」必须跳往演示开通沙箱或预约
self.assertTrue(
re.search(r'href="\{:addon_url\(\'official/web\.official_product/list\',\[\':pid\'=>22,\':sid\'=>22\]\)\}#demo-sandbox"[^>]*>[\s\S]*?立即开通', self.pc_html) or
("demo-sandbox" in self.pc_html and "立即开通" in self.pc_html) or
("official_online_message/list" in self.pc_html and "立即开通" in self.pc_html),
"SaaS版本【立即开通】必须跳转到产品演示沙箱或咨询开通"
)
def test_04_pc_case_cards_clickable(self):
"""PC 首页案例卡片必须具备可点击跳转到案例页的入口"""
# 每个案例卡片应包含查看详情链接或整卡可点击
case_links_count = len(re.findall(r'official_case_show/list', self.pc_html))
self.assertGreaterEqual(case_links_count, 3, "首页案例板块及卡片必须包含足够的案例页跳转链接")
self.assertIn("浏览全部标杆案例", self.pc_html, "首页案例板块必须包含【浏览全部标杆案例】主按钮")
def test_05_pc_process_and_cta_buttons(self):
"""PC 首页交付流程与底部 CTA 按钮跳转"""
# 底部 CTA 必须有预约架构师技术初审和致电专线
self.assertTrue(
"official_online_message/list" in self.pc_html and ":pid'=>18" in self.pc_html,
"底部 CTA 必须准确跳转到联系留言独立页 (:pid=>18)"
)
self.assertIn('tel:{:__config(\'site_mobile\',\'value\')}', self.pc_html, "底部 CTA 必须包含拨打电话链接")
def test_06_nav_actions_button(self):
"""导航栏右上角行动按钮必须全局可用,不能是单页孤立锚点"""
self.assertFalse(
re.search(r'<a\s+href="#contact"\s+class="nhc-btn\s+nhc-btn-primary\s+nhc-btn-sm"', self.pc_nav),
"导航栏右上角【预约演示】不能是单纯的 #contact,否则在非首页子页面点击无效"
)
def test_07_mobile_home_buttons(self):
"""移动端首页各板块必须具备完整的操作与跳转按钮"""
# 轮播图幻灯片4
self.assertFalse(
re.search(r'<a\s+href="#products"[^>]*>(?:(?!</a>)[\s\S])*?申请全系统演示', self.mobile_html),
"移动端申请全系统演示严禁跳转 #products,必须跳转演示沙箱或联系预约"
)
# 服务、产品、案例必须有链接或按钮跳转到独立页
self.assertTrue(
"official_product/list" in self.mobile_html and ":pid'=>19" in self.mobile_html,
"移动端服务板块必须有直达服务独立页的跳转链接"
)
self.assertTrue(
"official_product/list" in self.mobile_html and ":pid'=>22" in self.mobile_html,
"移动端产品板块必须有直达产品独立页的跳转链接"
)
self.assertTrue(
"official_case_show/list" in self.mobile_html and ":pid'=>14" in self.mobile_html,
"移动端案例板块必须有直达案例独立页的跳转链接"
)
# 移动端底部 CTA 必须有在线咨询与致电
self.assertTrue(
"official_online_message/list" in self.mobile_html or "tel:" in self.mobile_html,
"移动端底部 CTA 必须具备有效联络跳转"
)
def test_08_smooth_scroll_and_offset(self):
"""CSS 必须具备平滑滚动与避免 fixed 导航遮挡的 scroll-margin-top"""
self.assertIn('scroll-behavior: smooth', self.css, "CSS 必须配置 scroll-behavior: smooth")
self.assertIn('scroll-margin-top', self.css, "CSS 必须配置 scroll-margin-top 防止锚点被 fixed 导航遮挡")
if __name__ == '__main__':
unittest.main()