- CLI + Web 双平台,基于 LLM 的 TDD+DDD 自动化测试生成 - 集成 unified-test-platform 903 个 D-Cloud 用例 + 13 个 Robogo 场景 - Agent 5 步流水线: 扫描 -> DDD -> LLM 生成测试 - 精确回归 + UI 变更检查 - 可视化 HTML 测试报告 - 暗色/亮色主题切换 - 19 个 API 端点 + 6 个 SPA 页面 - 15/15 项 TDD+DDD 合规
362 lines
13 KiB
Python
362 lines
13 KiB
Python
"""设备管理页面对象"""
|
|
|
|
from typing import List, Dict, Optional
|
|
from playwright.sync_api import Page, Locator
|
|
from tests.base.base_page import BasePage
|
|
|
|
|
|
class DeviceManagePage(BasePage):
|
|
"""设备管理页面 - 设备列表、状态监控、SSH登录"""
|
|
|
|
URL_PATH = "/d-cloud/board/list"
|
|
|
|
SELECTORS = {
|
|
# 页面标题和按钮
|
|
"page_title": "h4:has-text('设备管理')",
|
|
"add_device_button": "button:has-text('新增设备')",
|
|
"search_input": "input[placeholder*='搜索']",
|
|
|
|
# 设备列表
|
|
"device_card_container": ".device-list",
|
|
"device_card": ".device-card",
|
|
"device_id": ".device-id",
|
|
"device_model": ".device-model",
|
|
"device_status": ".device-status",
|
|
|
|
# 监控指标
|
|
"cpu_usage": ".cpu-usage",
|
|
"gpu_usage": ".gpu-usage",
|
|
"temperature": ".temperature",
|
|
|
|
# 操作按钮
|
|
"ssh_login_button": ".ssh-login",
|
|
"device_menu_button": ".device-menu",
|
|
"menu_option": ".ant-dropdown-menu-item",
|
|
|
|
# 状态标识
|
|
"online_indicator": ".online-indicator",
|
|
"offline_indicator": ".offline-indicator",
|
|
"warning_indicator": ".warning-indicator",
|
|
}
|
|
|
|
def __init__(self, page: Page):
|
|
super().__init__(page)
|
|
self.url = f"{self.base_url}{self.URL_PATH}"
|
|
|
|
def navigate(self, url: Optional[str] = None) -> None:
|
|
"""导航到设备管理页面"""
|
|
target_url = url if url else self.url
|
|
self.page.goto(target_url)
|
|
self.page.wait_for_load_state("networkidle")
|
|
|
|
# ==================== 基础操作 ====================
|
|
|
|
def click_add_device(self) -> bool:
|
|
"""点击新增设备按钮"""
|
|
add_button = self.page.locator(self.SELECTORS["add_device_button"])
|
|
if add_button.is_visible():
|
|
add_button.click()
|
|
self.wait_for_page_ready()
|
|
return True
|
|
return False
|
|
|
|
def search_device(self, keyword: str) -> bool:
|
|
"""搜索设备"""
|
|
search_input = self.page.locator(self.SELECTORS["search_input"])
|
|
if search_input.is_visible():
|
|
search_input.fill(keyword)
|
|
self.page.keyboard.press("Enter")
|
|
self.wait_for_page_ready()
|
|
return True
|
|
return False
|
|
|
|
def clear_search(self) -> bool:
|
|
"""清除搜索条件"""
|
|
search_input = self.page.locator(self.SELECTORS["search_input"])
|
|
if search_input.is_visible():
|
|
search_input.clear()
|
|
self.page.keyboard.press("Enter")
|
|
self.wait_for_page_ready()
|
|
return True
|
|
return False
|
|
|
|
# ==================== 设备列表操作 ====================
|
|
|
|
def get_device_list(self) -> List[Locator]:
|
|
"""获取设备列表中的所有设备卡片"""
|
|
device_cards = self.page.locator(self.SELECTORS["device_card"]).all()
|
|
return device_cards
|
|
|
|
def get_device_count(self) -> int:
|
|
"""获取设备数量"""
|
|
return self.page.locator(self.SELECTORS["device_card"]).count()
|
|
|
|
def get_device_by_id(self, device_id: str) -> Optional[Locator]:
|
|
"""根据设备ID获取设备卡片"""
|
|
device_cards = self.get_device_list()
|
|
for card in device_cards:
|
|
# 假设每个设备卡片中包含设备ID
|
|
id_element = card.locator(self.SELECTORS["device_id"])
|
|
if id_element.is_visible():
|
|
actual_id = id_element.inner_text().strip()
|
|
if device_id in actual_id:
|
|
return card
|
|
return None
|
|
|
|
def get_device_info(self, device_card: Locator) -> Dict[str, str]:
|
|
"""获取设备卡片的详细信息"""
|
|
info = {}
|
|
|
|
# 设备ID
|
|
id_element = device_card.locator(self.SELECTORS["device_id"])
|
|
if id_element.is_visible():
|
|
info["id"] = id_element.inner_text().strip()
|
|
|
|
# 设备型号
|
|
model_element = device_card.locator(self.SELECTORS["device_model"])
|
|
if model_element.is_visible():
|
|
info["model"] = model_element.inner_text().strip()
|
|
|
|
# 设备状态
|
|
status_element = device_card.locator(self.SELECTORS["device_status"])
|
|
if status_element.is_visible():
|
|
info["status"] = status_element.inner_text().strip()
|
|
|
|
return info
|
|
|
|
def get_all_devices_info(self) -> List[Dict[str, str]]:
|
|
"""获取所有设备的详细信息"""
|
|
devices = []
|
|
device_cards = self.get_device_list()
|
|
|
|
for card in device_cards:
|
|
device_info = self.get_device_info(card)
|
|
if device_info: # 只添加有信息的设备
|
|
devices.append(device_info)
|
|
|
|
return devices
|
|
|
|
# ==================== 设备状态检查 ====================
|
|
|
|
def get_device_status(self, device_id: str) -> Optional[str]:
|
|
"""获取设备状态"""
|
|
device_card = self.get_device_by_id(device_id)
|
|
if device_card:
|
|
status_element = device_card.locator(self.SELECTORS["device_status"])
|
|
if status_element.is_visible():
|
|
return status_element.inner_text().strip()
|
|
return None
|
|
|
|
def is_device_online(self, device_id: str) -> bool:
|
|
"""检查设备是否在线"""
|
|
status = self.get_device_status(device_id)
|
|
if status:
|
|
return "在线" in status or "运行中" in status or "正常" in status
|
|
return False
|
|
|
|
def is_device_offline(self, device_id: str) -> bool:
|
|
"""检查设备是否离线"""
|
|
status = self.get_device_status(device_id)
|
|
if status:
|
|
return "离线" in status or "停止" in status
|
|
return False
|
|
|
|
def has_warning_status(self, device_id: str) -> bool:
|
|
"""检查设备是否有警告状态"""
|
|
status = self.get_device_status(device_id)
|
|
if status:
|
|
return "警告" in status or "异常" in status or "故障" in status
|
|
return False
|
|
|
|
# ==================== 监控指标获取 ====================
|
|
|
|
def get_device_metrics(self, device_id: str) -> Dict[str, Optional[str]]:
|
|
"""获取设备监控指标 (CPU/GPU/温度)"""
|
|
metrics = {
|
|
"cpu_usage": None,
|
|
"gpu_usage": None,
|
|
"temperature": None
|
|
}
|
|
|
|
device_card = self.get_device_by_id(device_id)
|
|
if device_card:
|
|
# CPU使用率
|
|
cpu_element = device_card.locator(self.SELECTORS["cpu_usage"])
|
|
if cpu_element.is_visible():
|
|
metrics["cpu_usage"] = cpu_element.inner_text().strip()
|
|
|
|
# GPU使用率
|
|
gpu_element = device_card.locator(self.SELECTORS["gpu_usage"])
|
|
if gpu_element.is_visible():
|
|
metrics["gpu_usage"] = gpu_element.inner_text().strip()
|
|
|
|
# 温度
|
|
temp_element = device_card.locator(self.SELECTORS["temperature"])
|
|
if temp_element.is_visible():
|
|
metrics["temperature"] = temp_element.inner_text().strip()
|
|
|
|
return metrics
|
|
|
|
def get_cpu_usage(self, device_id: str) -> Optional[str]:
|
|
"""获取设备CPU使用率"""
|
|
metrics = self.get_device_metrics(device_id)
|
|
return metrics.get("cpu_usage")
|
|
|
|
def get_gpu_usage(self, device_id: str) -> Optional[str]:
|
|
"""获取设备GPU使用率"""
|
|
metrics = self.get_device_metrics(device_id)
|
|
return metrics.get("gpu_usage")
|
|
|
|
def get_temperature(self, device_id: str) -> Optional[str]:
|
|
"""获取设备温度"""
|
|
metrics = self.get_device_metrics(device_id)
|
|
return metrics.get("temperature")
|
|
|
|
# ==================== 操作按钮 ====================
|
|
|
|
def click_ssh_login(self, device_id: str) -> bool:
|
|
"""点击SSH登录按钮"""
|
|
device_card = self.get_device_by_id(device_id)
|
|
if device_card:
|
|
ssh_button = device_card.locator(self.SELECTORS["ssh_login_button"])
|
|
if ssh_button.is_visible():
|
|
ssh_button.click()
|
|
self.wait_for_page_ready()
|
|
return True
|
|
return False
|
|
|
|
def click_device_menu(self, device_id: str) -> bool:
|
|
"""点击设备更多操作菜单"""
|
|
device_card = self.get_device_by_id(device_id)
|
|
if device_card:
|
|
menu_button = device_card.locator(self.SELECTORS["device_menu_button"])
|
|
if menu_button.is_visible():
|
|
menu_button.click()
|
|
# 等待菜单显示
|
|
self.page.wait_for_timeout(500)
|
|
return True
|
|
return False
|
|
|
|
def select_menu_option(self, option_text: str) -> bool:
|
|
"""选择菜单选项"""
|
|
menu_option = self.page.locator(
|
|
f"{self.SELECTORS['menu_option']}:has-text('{option_text}')"
|
|
)
|
|
if menu_option.is_visible():
|
|
menu_option.click()
|
|
self.wait_for_page_ready()
|
|
return True
|
|
return False
|
|
|
|
def click_device_menu_option(self, device_id: str, option_text: str) -> bool:
|
|
"""点击设备更多操作菜单中的特定选项"""
|
|
if self.click_device_menu(device_id):
|
|
return self.select_menu_option(option_text)
|
|
return False
|
|
|
|
# ==================== 断言和验证 ====================
|
|
|
|
def is_page_loaded(self) -> bool:
|
|
"""检查页面是否加载成功"""
|
|
title_element = self.page.locator(self.SELECTORS["page_title"])
|
|
return title_element.is_visible()
|
|
|
|
def has_devices(self) -> bool:
|
|
"""检查是否有设备显示"""
|
|
return self.get_device_count() > 0
|
|
|
|
def has_no_devices(self) -> bool:
|
|
"""检查是否没有设备(空状态)"""
|
|
return self.get_device_count() == 0
|
|
|
|
def wait_for_device_appear(self, device_id: str, timeout: int = 10000) -> bool:
|
|
"""等待设备出现在列表中"""
|
|
def condition():
|
|
return self.get_device_by_id(device_id) is not None
|
|
return self.wait_until(condition, timeout)
|
|
|
|
def wait_for_device_disappear(self, device_id: str, timeout: int = 10000) -> bool:
|
|
"""等待设备从列表中消失"""
|
|
def condition():
|
|
return self.get_device_by_id(device_id) is None
|
|
return self.wait_until(condition, timeout)
|
|
|
|
# ==================== 页面操作 ====================
|
|
|
|
def refresh_device_list(self) -> None:
|
|
"""刷新设备列表"""
|
|
self.reload()
|
|
|
|
def sort_by(self, field: str) -> bool:
|
|
"""按字段排序设备列表
|
|
|
|
Args:
|
|
field: 排序字段,如 '状态', 'CPU使用率', '温度'等
|
|
"""
|
|
sort_button = self.page.locator(f"th:has-text('{field}')")
|
|
if sort_button.is_visible():
|
|
sort_button.click()
|
|
self.wait_for_page_ready()
|
|
return True
|
|
return False
|
|
|
|
def filter_by_status(self, status: str) -> bool:
|
|
"""按状态筛选设备
|
|
|
|
Args:
|
|
status: 设备状态,如 '在线', '离线', '警告'
|
|
"""
|
|
filter_dropdown = self.page.locator(".ant-dropdown-trigger:has-text('状态')")
|
|
if filter_dropdown.is_visible():
|
|
filter_dropdown.click()
|
|
# 等待下拉菜单显示
|
|
self.page.wait_for_timeout(500)
|
|
|
|
# 选择状态选项
|
|
status_option = self.page.locator(f"label:has-text('{status}')")
|
|
if status_option.is_visible():
|
|
status_option.click()
|
|
# 点击确定或应用筛选
|
|
apply_button = self.page.locator("button:has-text('确定')")
|
|
if apply_button.is_visible():
|
|
apply_button.click()
|
|
else:
|
|
# 如果没有确定按钮,直接点击外部关闭下拉菜单
|
|
self.page.locator("body").click()
|
|
|
|
self.wait_for_page_ready()
|
|
return True
|
|
|
|
return False
|
|
|
|
# ==================== 批量操作 ====================
|
|
|
|
def select_device(self, device_id: str) -> bool:
|
|
"""选择设备(勾选复选框)"""
|
|
device_card = self.get_device_by_id(device_id)
|
|
if device_card:
|
|
checkbox = device_card.locator("input[type='checkbox']")
|
|
if checkbox.is_visible() and not checkbox.is_checked():
|
|
checkbox.check()
|
|
return True
|
|
return False
|
|
|
|
def select_all_devices(self) -> bool:
|
|
"""全选所有设备"""
|
|
select_all_checkbox = self.page.locator("th input[type='checkbox']")
|
|
if select_all_checkbox.is_visible() and not select_all_checkbox.is_checked():
|
|
select_all_checkbox.check()
|
|
return True
|
|
return False
|
|
|
|
def batch_operation(self, operation: str) -> bool:
|
|
"""执行批量操作
|
|
|
|
Args:
|
|
operation: 批量操作类型,如 '重启', '关机', '删除'
|
|
"""
|
|
batch_button = self.page.locator(f"button:has-text('批量{operation}')")
|
|
if batch_button.is_visible():
|
|
batch_button.click()
|
|
return True
|
|
return False |