- 16 个业务模块,856 个测试用例,40 个 E2E 测试 - 单一 React 19 前端 + PlatformSwitcher 二合一切换 - Flask 后端支持 scenario/dcloud_module/dcloud_e2e/dcloud_all 四种任务类型 - E2E 测试面板:API 全链路(8条) + UI 点击(3条) + 分步测试(3个) - SSE 实时日志流 + 飞书告警 + 定时调度 - 完整还原原 ToB 平台 TestPanel E2E 模块
519 lines
17 KiB
Python
519 lines
17 KiB
Python
"""工作台页面对象模型"""
|
||
|
||
from playwright.sync_api import Page, Locator
|
||
from tests.base.base_page import BasePage
|
||
|
||
|
||
class WorkbenchPage(BasePage):
|
||
"""工作台页面 - /d-cloud/workbench/list
|
||
|
||
工作台是网站的主入口页面,包含:
|
||
- 工作台卡片列表
|
||
- 导航菜单
|
||
- 快捷操作入口
|
||
- 统计信息展示
|
||
"""
|
||
|
||
def __init__(self, page: Page):
|
||
"""初始化工作台页面
|
||
|
||
Args:
|
||
page: Playwright Page 对象
|
||
"""
|
||
super().__init__(page)
|
||
self.path = "/d-cloud/workbench/list"
|
||
|
||
# ==================== 页面元素定位器 ====================
|
||
|
||
# 页面标题
|
||
self.page_title = page.locator("h1:has-text('工作台')")
|
||
|
||
# 导航菜单
|
||
self.nav_menu = page.locator(".ant-menu")
|
||
self.nav_menu_items = page.locator(".ant-menu-item")
|
||
|
||
# 快捷操作区域
|
||
self.quick_actions_section = page.locator("section:has-text('快捷操作')")
|
||
self.quick_action_buttons = page.locator(".quick-action-button")
|
||
|
||
# 工作台卡片区域
|
||
self.workbench_cards_section = page.locator("section:has-text('我的工作台')")
|
||
self.workbench_cards = page.locator(".workbench-card")
|
||
self.card_titles = page.locator(".workbench-card .card-title")
|
||
self.card_descriptions = page.locator(".workbench-card .card-description")
|
||
self.card_actions = page.locator(".workbench-card .card-action-button")
|
||
|
||
# 统计信息区域
|
||
self.stats_section = page.locator("section:has-text('统计信息')")
|
||
self.stat_cards = page.locator(".stat-card")
|
||
|
||
# 最近活动区域
|
||
self.recent_activities_section = page.locator("section:has-text('最近活动')")
|
||
self.activity_items = page.locator(".activity-item")
|
||
|
||
# 搜索框
|
||
self.search_input = page.locator("input[placeholder*='搜索']")
|
||
self.search_button = page.locator("button[aria-label='搜索']")
|
||
|
||
# 用户头像/菜单
|
||
self.user_avatar = page.locator(".user-avatar")
|
||
self.user_menu = page.locator(".user-menu")
|
||
|
||
# 通知图标
|
||
self.notification_bell = page.locator(".notification-bell")
|
||
|
||
# 帮助按钮
|
||
self.help_button = page.locator("button[aria-label='帮助']")
|
||
|
||
# 页面加载指示器
|
||
self.loading_spinner = page.locator(".ant-spin-spinning")
|
||
|
||
# ==================== 导航方法 ====================
|
||
|
||
def navigate(self) -> None:
|
||
"""导航到工作台页面"""
|
||
self.page.goto(f"{self.base_url}{self.path}")
|
||
self.page.wait_for_load_state("networkidle")
|
||
self.wait_for_page_load()
|
||
|
||
def wait_for_page_load(self) -> None:
|
||
"""等待页面加载完成
|
||
|
||
使用更通用的等待策略:
|
||
1. 等待 URL 匹配预期路径(放宽限制)
|
||
2. 等待页面稳定(无网络活动)
|
||
3. 等待加载动画消失(如果存在)
|
||
"""
|
||
# 等待页面基本稳定
|
||
self.page.wait_for_load_state("domcontentloaded")
|
||
|
||
# 使用更宽松的URL检查,避免因为URL微小变化而失败
|
||
try:
|
||
self.page.wait_for_url("**/d-cloud/**", timeout=self.timeout)
|
||
except:
|
||
# 如果URL检查失败,仍然继续
|
||
pass
|
||
|
||
# 等待可能的加载动画消失(如果存在)
|
||
try:
|
||
self.page.wait_for_selector(".ant-spin-spinning", state="hidden", timeout=3000)
|
||
except:
|
||
# 加载动画可能不存在,忽略
|
||
pass
|
||
|
||
# ==================== 导航菜单方法 ====================
|
||
|
||
def get_nav_menu_items(self) -> list[str]:
|
||
"""获取导航菜单项列表
|
||
|
||
Returns:
|
||
菜单项文本列表
|
||
"""
|
||
items = []
|
||
for item in self.nav_menu_items.all():
|
||
try:
|
||
text = item.text_content().strip()
|
||
if text:
|
||
items.append(text)
|
||
except:
|
||
continue
|
||
return items
|
||
|
||
def click_nav_menu_item(self, item_text: str) -> None:
|
||
"""点击导航菜单项
|
||
|
||
Args:
|
||
item_text: 菜单项文本
|
||
"""
|
||
item = self.page.locator(f".ant-menu-item:has-text('{item_text}')")
|
||
if item.is_visible():
|
||
item.click()
|
||
self.page.wait_for_load_state("networkidle")
|
||
|
||
def is_nav_menu_item_active(self, item_text: str) -> bool:
|
||
"""检查导航菜单项是否激活
|
||
|
||
Args:
|
||
item_text: 菜单项文本
|
||
|
||
Returns:
|
||
bool: 是否激活状态
|
||
"""
|
||
item = self.page.locator(f".ant-menu-item:has-text('{item_text}')")
|
||
return item.is_visible() and "ant-menu-item-selected" in item.get_attribute("class", timeout=1000) or False
|
||
|
||
# ==================== 工作台卡片方法 ====================
|
||
|
||
def get_card_count(self) -> int:
|
||
"""获取工作台卡片数量
|
||
|
||
Returns:
|
||
int: 卡片数量,如果元素不存在则返回0
|
||
"""
|
||
try:
|
||
# 使用更短的超时时间,如果元素不存在则返回0
|
||
self.page.wait_for_selector(".workbench-card", timeout=3000, state="attached")
|
||
return self.workbench_cards.count()
|
||
except:
|
||
return 0
|
||
|
||
def get_card_titles(self) -> list[str]:
|
||
"""获取所有卡片标题
|
||
|
||
Returns:
|
||
卡片标题列表
|
||
"""
|
||
titles = []
|
||
for card in self.card_titles.all():
|
||
try:
|
||
title = card.text_content().strip()
|
||
if title:
|
||
titles.append(title)
|
||
except:
|
||
continue
|
||
return titles
|
||
|
||
def get_card_descriptions(self) -> list[str]:
|
||
"""获取所有卡片描述
|
||
|
||
Returns:
|
||
卡片描述列表
|
||
"""
|
||
descriptions = []
|
||
for desc in self.card_descriptions.all():
|
||
try:
|
||
text = desc.text_content().strip()
|
||
if text:
|
||
descriptions.append(text)
|
||
except:
|
||
continue
|
||
return descriptions
|
||
|
||
def get_card_info(self, card_title: str) -> dict:
|
||
"""获取指定卡片的详细信息
|
||
|
||
Args:
|
||
card_title: 卡片标题
|
||
|
||
Returns:
|
||
卡片信息字典
|
||
"""
|
||
card = self.page.locator(f".workbench-card:has-text('{card_title}')")
|
||
info = {}
|
||
try:
|
||
info["title"] = card_title
|
||
info["description"] = card.locator(".card-description").text_content().strip() if card.locator(".card-description").is_visible() else ""
|
||
info["has_action_button"] = card.locator(".card-action-button").is_visible()
|
||
|
||
# 获取统计数字(如果有)
|
||
stats = card.locator(".stat-number").all_text_contents()
|
||
info["stats"] = [s.strip() for s in stats if s.strip()]
|
||
|
||
# 获取标签(如果有)
|
||
tags = card.locator(".ant-tag").all_text_contents()
|
||
info["tags"] = [t.strip() for t in tags if t.strip()]
|
||
|
||
except:
|
||
pass
|
||
return info
|
||
|
||
def click_card_action(self, card_title: str) -> None:
|
||
"""点击卡片操作按钮
|
||
|
||
Args:
|
||
card_title: 卡片标题
|
||
"""
|
||
card = self.page.locator(f".workbench-card:has-text('{card_title}')")
|
||
action_btn = card.locator(".card-action-button")
|
||
if action_btn.is_visible():
|
||
action_btn.click()
|
||
self.page.wait_for_load_state("networkidle")
|
||
|
||
# ==================== 快捷操作方法 ====================
|
||
|
||
def get_quick_actions(self) -> list[str]:
|
||
"""获取快捷操作按钮列表
|
||
|
||
Returns:
|
||
快捷操作按钮文本列表
|
||
"""
|
||
actions = []
|
||
for btn in self.quick_action_buttons.all():
|
||
try:
|
||
text = btn.text_content().strip()
|
||
if text:
|
||
actions.append(text)
|
||
except:
|
||
continue
|
||
return actions
|
||
|
||
def click_quick_action(self, action_text: str) -> None:
|
||
"""点击快捷操作按钮
|
||
|
||
Args:
|
||
action_text: 操作按钮文本
|
||
"""
|
||
action_btn = self.page.locator(f".quick-action-button:has-text('{action_text}')")
|
||
if action_btn.is_visible():
|
||
action_btn.click()
|
||
self.page.wait_for_load_state("networkidle")
|
||
|
||
# ==================== 统计信息方法 ====================
|
||
|
||
def get_stat_cards_count(self) -> int:
|
||
"""获取统计卡片数量
|
||
|
||
Returns:
|
||
int: 统计卡片数量
|
||
"""
|
||
return self.stat_cards.count()
|
||
|
||
def get_stat_card_info(self, index: int = 0) -> dict:
|
||
"""获取统计卡片信息
|
||
|
||
Args:
|
||
index: 卡片索引,默认为0
|
||
|
||
Returns:
|
||
统计卡片信息字典
|
||
"""
|
||
cards = self.stat_cards.all()
|
||
if index >= len(cards):
|
||
return {}
|
||
|
||
card = cards[index]
|
||
info = {}
|
||
try:
|
||
info["title"] = card.locator(".stat-title").text_content().strip() if card.locator(".stat-title").is_visible() else ""
|
||
info["value"] = card.locator(".stat-value").text_content().strip() if card.locator(".stat-value").is_visible() else ""
|
||
info["change"] = card.locator(".stat-change").text_content().strip() if card.locator(".stat-change").is_visible() else ""
|
||
info["icon"] = card.locator(".stat-icon").is_visible()
|
||
except:
|
||
pass
|
||
return info
|
||
|
||
# ==================== 最近活动方法 ====================
|
||
|
||
def get_recent_activities_count(self) -> int:
|
||
"""获取最近活动数量
|
||
|
||
Returns:
|
||
int: 活动数量
|
||
"""
|
||
return self.activity_items.count()
|
||
|
||
def get_activity_info(self, index: int = 0) -> dict:
|
||
"""获取活动信息
|
||
|
||
Args:
|
||
index: 活动索引,默认为0
|
||
|
||
Returns:
|
||
活动信息字典
|
||
"""
|
||
activities = self.activity_items.all()
|
||
if index >= len(activities):
|
||
return {}
|
||
|
||
activity = activities[index]
|
||
info = {}
|
||
try:
|
||
info["title"] = activity.locator(".activity-title").text_content().strip() if activity.locator(".activity-title").is_visible() else ""
|
||
info["description"] = activity.locator(".activity-description").text_content().strip() if activity.locator(".activity-description").is_visible() else ""
|
||
info["time"] = activity.locator(".activity-time").text_content().strip() if activity.locator(".activity-time").is_visible() else ""
|
||
info["user"] = activity.locator(".activity-user").text_content().strip() if activity.locator(".activity-user").is_visible() else ""
|
||
except:
|
||
pass
|
||
return info
|
||
|
||
# ==================== 搜索方法 ====================
|
||
|
||
def search(self, keyword: str) -> None:
|
||
"""在工作台搜索
|
||
|
||
Args:
|
||
keyword: 搜索关键词
|
||
"""
|
||
if self.search_input.is_visible():
|
||
self.search_input.fill(keyword)
|
||
self.search_input.press("Enter")
|
||
self.page.wait_for_load_state("networkidle")
|
||
|
||
def clear_search(self) -> None:
|
||
"""清除搜索内容"""
|
||
if self.search_input.is_visible():
|
||
self.search_input.clear()
|
||
self.page.wait_for_load_state("domcontentloaded")
|
||
|
||
# ==================== 用户菜单方法 ====================
|
||
|
||
def open_user_menu(self) -> None:
|
||
"""打开用户菜单"""
|
||
if self.user_avatar.is_visible():
|
||
self.user_avatar.click()
|
||
self.page.wait_for_selector(".user-menu", state="visible", timeout=5000)
|
||
|
||
def get_user_menu_items(self) -> list[str]:
|
||
"""获取用户菜单项列表
|
||
|
||
Returns:
|
||
菜单项文本列表
|
||
"""
|
||
items = []
|
||
if self.user_menu.is_visible():
|
||
menu_items = self.user_menu.locator(".ant-dropdown-menu-item")
|
||
for item in menu_items.all():
|
||
try:
|
||
text = item.text_content().strip()
|
||
if text:
|
||
items.append(text)
|
||
except:
|
||
continue
|
||
return items
|
||
|
||
def click_user_menu_item(self, item_text: str) -> None:
|
||
"""点击用户菜单项
|
||
|
||
Args:
|
||
item_text: 菜单项文本
|
||
"""
|
||
self.open_user_menu()
|
||
item = self.page.locator(f".ant-dropdown-menu-item:has-text('{item_text}')")
|
||
if item.is_visible():
|
||
item.click()
|
||
self.page.wait_for_load_state("networkidle")
|
||
|
||
# ==================== 通知方法 ====================
|
||
|
||
def open_notifications(self) -> None:
|
||
"""打开通知面板"""
|
||
if self.notification_bell.is_visible():
|
||
self.notification_bell.click()
|
||
self.page.wait_for_selector(".notification-panel", state="visible", timeout=5000)
|
||
|
||
def get_notification_count(self) -> int:
|
||
"""获取通知数量
|
||
|
||
Returns:
|
||
int: 通知数量,如果无法获取则返回0
|
||
"""
|
||
if self.notification_bell.is_visible():
|
||
badge = self.notification_bell.locator(".ant-badge-count")
|
||
if badge.is_visible():
|
||
try:
|
||
count_text = badge.text_content().strip()
|
||
return int(count_text) if count_text.isdigit() else 0
|
||
except:
|
||
pass
|
||
return 0
|
||
|
||
# ==================== 页面状态检查 ====================
|
||
|
||
def is_page_loaded(self) -> bool:
|
||
"""检查页面是否加载完成
|
||
|
||
Returns:
|
||
bool: 页面是否加载完成
|
||
"""
|
||
try:
|
||
# 首先等待页面基本加载完成
|
||
self.page.wait_for_load_state("domcontentloaded")
|
||
|
||
# 检查是否有加载动画
|
||
if self.loading_spinner.is_visible(timeout=3000):
|
||
return False
|
||
|
||
# 使用更宽松的页面加载检查:URL包含/d-cloud/路径
|
||
current_url = self.get_current_url()
|
||
if "/d-cloud/" not in current_url:
|
||
return False
|
||
|
||
# 可选:检查页面标题,但不要严格要求
|
||
try:
|
||
return self.page_title.is_visible(timeout=3000)
|
||
except:
|
||
# 如果页面标题不可见,但页面已基本加载,也可以接受
|
||
return True
|
||
except:
|
||
return False
|
||
|
||
def has_workbench_cards(self) -> bool:
|
||
"""检查是否有工作台卡片
|
||
|
||
Returns:
|
||
bool: 是否有工作台卡片
|
||
"""
|
||
try:
|
||
return self.workbench_cards.count() > 0
|
||
except:
|
||
return False
|
||
|
||
def has_quick_actions(self) -> bool:
|
||
"""检查是否有快捷操作
|
||
|
||
Returns:
|
||
bool: 是否有快捷操作
|
||
"""
|
||
try:
|
||
return self.quick_action_buttons.count() > 0
|
||
except:
|
||
return False
|
||
|
||
def has_stat_cards(self) -> bool:
|
||
"""检查是否有统计卡片
|
||
|
||
Returns:
|
||
bool: 是否有统计卡片
|
||
"""
|
||
try:
|
||
return self.stat_cards.count() > 0
|
||
except:
|
||
return False
|
||
|
||
def has_recent_activities(self) -> bool:
|
||
"""检查是否有最近活动
|
||
|
||
Returns:
|
||
bool: 是否有最近活动
|
||
"""
|
||
try:
|
||
return self.activity_items.count() > 0
|
||
except:
|
||
return False
|
||
|
||
# ==================== 页面操作 ====================
|
||
|
||
def refresh_page(self) -> None:
|
||
"""刷新页面"""
|
||
self.page.reload()
|
||
self.wait_for_page_load()
|
||
|
||
def get_page_title(self) -> str:
|
||
"""获取页面标题
|
||
|
||
Returns:
|
||
str: 页面标题文本
|
||
"""
|
||
try:
|
||
return self.page_title.text_content().strip()
|
||
except:
|
||
return ""
|
||
|
||
def get_current_url(self) -> str:
|
||
"""获取当前页面URL
|
||
|
||
Returns:
|
||
str: 当前页面URL
|
||
"""
|
||
return self.page.url
|
||
|
||
# ==================== 辅助方法 ====================
|
||
|
||
def take_screenshot(self, filename: str = "workbench") -> None:
|
||
"""截取页面截图
|
||
|
||
Args:
|
||
filename: 截图文件名(不含扩展名)
|
||
"""
|
||
screenshot_path = f"{self.config.SCREENSHOT_DIR}/{filename}.png"
|
||
self.page.screenshot(path=screenshot_path, full_page=True) |