"""数据生成页面对象模型 (工作台子页面)""" from playwright.sync_api import Page, Locator from tests.base.base_page import BasePage class DataCreatePage(BasePage): """数据生成页面 - /d-cloud/data-create/list""" def __init__(self, page: Page): super().__init__(page) self.path = "/d-cloud/data-create/list" # 侧边栏 self.sidebar_data_gen = page.locator("span:has-text('数据生成'):visible").first self.sidebar_dataset = page.locator("span:has-text('数据集'):visible").first self.sidebar_model_workshop = page.locator( "span:has-text('模型工坊'):visible" ).first self.sidebar_model_plaza = page.locator( "span:has-text('模型广场'):visible" ).first self.sidebar_device_mgmt = page.locator( "span:has-text('设备管理'):visible" ).first # 左侧卡片 self.workflow_cards = page.locator(".ant-card:has-text('工作流')") self.material_library_card = page.locator(".ant-card:has-text('素材库')") self.lora_finetune_card = page.locator(".ant-card:has-text('LORA微调')") self.prompt_library_card = page.locator(".ant-card:has-text('系统提示词库')") # 任务表格 self.task_table = page.locator(".ant-table") self.task_table_headers = page.locator(".ant-table-thead th") self.task_table_rows = page.locator(".ant-table-tbody tr") # 创建任务按钮 self.create_task_button = page.get_by_role("button", name="创建任务") def navigate(self) -> None: """导航到数据生成页面""" self.page.goto(f"{self.base_url}{self.path}", wait_until="domcontentloaded") try: self.page.wait_for_load_state("networkidle", timeout=10000) except Exception: pass def get_table_headers(self) -> list[str]: """获取任务表格表头""" self.page.wait_for_selector(".ant-table-thead th", timeout=10000) headers = self.task_table_headers.all_text_contents() return [h.strip() for h in headers if h.strip()] def get_task_count(self) -> int: """获取任务数量""" self.page.wait_for_selector(".ant-table-tbody", timeout=10000) return self.task_table_rows.count() def get_task_names(self, limit: int = 5) -> list[str]: """获取任务名称列表(限制数量以提高性能)""" self.page.wait_for_selector(".ant-table-tbody", timeout=10000) rows = self.task_table_rows.all()[:limit] names = [] for row in rows: try: name_cell = row.locator("td").first text = name_cell.text_content().strip() if text and not text.isspace(): names.append(text) except: continue return names def click_create_task(self) -> None: """点击创建任务按钮""" self.create_task_button.click() self.page.wait_for_load_state("networkidle") def delete_task_by_name(self, task_name: str) -> None: row = self.page.locator(f".ant-table-tbody tr:has-text('{task_name}')") delete_btn = row.locator("text=删除").first if delete_btn.is_visible(): delete_btn.click() self.page.wait_for_load_state("domcontentloaded") confirm_btn = self.page.get_by_role("button", name="确 定") if confirm_btn.is_visible(): confirm_btn.click() def clone_task_by_name(self, task_name: str) -> None: row = self.page.locator(f".ant-table-tbody tr:has-text('{task_name}')") clone_btn = row.locator("text=克隆").first if clone_btn.is_visible(): clone_btn.click() self.page.wait_for_load_state("networkidle") def click_delete_in_row(self, row_index: int = 0) -> None: row = self.task_table_rows.nth(row_index) delete_btn = row.locator("text=删除").first if delete_btn.is_visible(): delete_btn.click() self.page.wait_for_load_state("domcontentloaded") def click_clone_in_row(self, row_index: int = 0) -> None: row = self.task_table_rows.nth(row_index) clone_btn = row.locator("text=克隆").first if clone_btn.is_visible(): clone_btn.click() self.page.wait_for_load_state("networkidle") def confirm_delete(self) -> None: confirm_btn = self.page.get_by_role("button", name="确 定") if confirm_btn.is_visible(): confirm_btn.click() self.page.wait_for_load_state("domcontentloaded") def cancel_delete(self) -> None: cancel_btn = self.page.get_by_role("button", name="取 消") if cancel_btn.is_visible(): cancel_btn.click() def get_task_row_info(self, row_index: int = 0) -> dict: row = self.task_table_rows.nth(row_index) cells = row.locator("td").all() if len(cells) >= 7: return { "name": cells[0].text_content().strip(), "status": cells[1].text_content().strip(), "generated": cells[3].text_content().strip(), "files": cells[4].text_content().strip(), "workflow": cells[5].text_content().strip(), "created_at": cells[6].text_content().strip(), } return {} def get_sidebar_items(self) -> list[str]: """获取侧边栏项目列表""" items = [] for item in [ self.sidebar_data_gen, self.sidebar_dataset, self.sidebar_model_workshop, self.sidebar_model_plaza, self.sidebar_device_mgmt, ]: if item.is_visible(): items.append(item.text_content().strip()) return items def wait_for_list_load(self) -> None: """等待任务列表加载完成""" self.page.wait_for_selector(".ant-table-tbody", timeout=10000) self.page.wait_for_load_state("domcontentloaded") def is_task_visible(self, task_name: str) -> bool: """检查任务是否在列表中可见""" row = self.page.locator(f".ant-table-tbody tr:has-text('{task_name}')") return row.is_visible() def clone_task(self, row_index: int = 0) -> None: """克隆任务 (默认第一行)""" self.click_clone_in_row(row_index) def delete_task(self, row_index: int = 0) -> None: """删除任务 (默认第一行)""" self.click_delete_in_row(row_index) def go_to_next_page(self) -> None: """跳转到下一页""" next_btn = self.page.locator(".ant-pagination-next") if next_btn.is_enabled(): next_btn.click() self.page.wait_for_load_state("domcontentloaded") def go_to_page(self, page_num: int) -> None: """跳转到指定页""" page_item = self.page.locator(f".ant-pagination-item:has-text('{page_num}')") if page_item.is_visible(): page_item.click() self.page.wait_for_load_state("domcontentloaded") def is_page_active(self, page_num: int) -> bool: """检查指定页是否为当前活动页""" active_item = self.page.locator(".ant-pagination-item-active") return active_item.text_content().strip() == str(page_num) def is_list_loaded(self) -> bool: """检查列表是否加载完成""" try: self.page.wait_for_selector(".ant-table-tbody tr", timeout=5000) return True except: return False def is_confirm_dialog_visible(self) -> bool: """检查确认对话框是否可见""" dialog = self.page.locator(".ant-modal:visible") return dialog.is_visible() def view_workflow_detail(self, workflow_name: str = None) -> None: """查看工作流详情""" if workflow_name: card = self.page.locator(f".ant-card:has-text('{workflow_name}')") if card.count() > 0: card.click() else: cards = self.page.locator(".ant-card") if cards.count() > 0: cards.first.click() def refresh_list(self) -> None: """刷新任务列表""" refresh_btn = self.page.get_by_role("button", name="刷新") if refresh_btn.is_visible(): refresh_btn.click() self.page.wait_for_load_state("domcontentloaded") def open_filter(self) -> None: """打开筛选""" filter_btn = self.page.get_by_role("button", name="筛选") if filter_btn.is_visible(): filter_btn.click() def debug_workflow(self, workflow_name: str = None) -> None: """调试工作流""" if workflow_name: card = self.page.locator(f".ant-card:has-text('{workflow_name}')") if card.count() > 0: debug_btn = card.locator("button:has-text('调试')") if debug_btn.is_visible(): debug_btn.click() else: debug_btn = self.page.get_by_role("button", name="调试") if debug_btn.count() > 0 and debug_btn.first.is_visible(): debug_btn.first.click() """检查详情页是否可见""" return self.page.locator(".ant-descriptions").is_visible() def is_filter_menu_visible(self) -> bool: """检查筛选菜单是否可见""" return self.page.locator(".ant-dropdown:visible").is_visible() def is_debug_mode_active(self) -> bool: """检查调试模式是否激活""" return ( "debug" in self.page.url or self.page.locator(".debug-panel").is_visible() ) class CreateTaskPage(BasePage): """创建任务页面 - /d-cloud/data-create/create 表单字段: - 任务名称 (必填) - 资源类型 (包年包月/弹性资源, 默认弹性资源) - 工作流 (必选, 动态列表) - 实例数量 (必填, 数字输入) - 生成数量 (必填, 数字输入) - 数据空间输出位置 (必选, 文件选择对话框) - 启用评分配置 (可选checkbox) - 评分提示词 (启用评分后可用) - 正向提示词 (选择工作流后显示) 按钮: - 返回 (返回任务列表) - 重置 (清空表单) - 执行 (提交任务) """ def __init__(self, page: Page): super().__init__(page) self.path = "/d-cloud/data-create/create" self.task_name_input = page.get_by_role("textbox", name="任务名称") self.resource_type_yearly_radio = page.locator( "label:has-text('包年包月') input[type='radio']" ).first self.resource_type_elastic_radio = page.locator( "label:has-text('弹性资源') input[type='radio']" ).first self.workflow_select = page.locator( ".ant-select:has-text('请选择工作流')" ).first self.instance_count_input = page.locator("input[type='number']").first self.generate_count_input = page.locator("input[type='number']").nth(1) self.output_location_input = page.get_by_role("textbox", name="请选择路径") self.enable_scoring_checkbox = page.locator("input[type='checkbox']").first self.scoring_prompt_input = page.get_by_role("textbox", name="评分提示词") self.prompt_tab = page.get_by_role("tab", name="正向提示词") self.prompt_input = page.locator(".ant-tabs-tabpane-active textarea").first self.back_button = page.get_by_role("button", name="创建任务").locator("..") self.reset_button = page.get_by_role("button", name="重 置") self.execute_button = page.get_by_role("button", name="执 行") self.file_dialog = page.locator(".ant-modal:has-text('文件选择')") self.file_dialog_personal_space = page.locator( ".ant-modal:has-text('个人空间') li" ) self.file_dialog_team_space = page.locator(".ant-modal:has-text('团队空间') li") self.file_dialog_cancel = page.get_by_role("button", name="取 消") self.file_dialog_confirm = page.get_by_role("button", name="选 择") def navigate(self) -> None: self.page.goto(f"{self.base_url}{self.path}", wait_until="domcontentloaded") try: self.page.wait_for_load_state("networkidle", timeout=10000) except Exception: pass try: self.select_resource_type("elastic") except Exception: pass def fill_task_name(self, name: str) -> None: inp = self.page.locator("#taskName") if inp.count() > 0: inp.fill(name) else: self.task_name_input.fill(name) def fill_instance_count(self, count: int) -> None: self.instance_count_input.fill(str(count)) def fill_generate_count(self, count: int) -> None: self.generate_count_input.fill(str(count)) def fill_basic_info( self, task_name: str, instance_count: int = 1, generate_count: int = 1 ) -> None: self.fill_task_name(task_name) self.fill_instance_count(instance_count) self.fill_generate_count(generate_count) def select_resource_type(self, resource_type: str) -> None: modal = self.page.locator(".ant-modal-wrap:visible") if modal.count() > 0: try: self.page.keyboard.press("Escape") except Exception: pass try: if resource_type == "yearly": self.resource_type_yearly_radio.click(timeout=5000) else: self.resource_type_elastic_radio.click(timeout=5000) except Exception: pass def select_workflow(self, workflow_name: str) -> None: modal = self.page.locator(".ant-modal-wrap:visible") if modal.count() > 0: close_btn = modal.locator(".ant-modal-close").first if close_btn.count() > 0: close_btn.click() self.page.locator("#workflow").click() self.page.get_by_text(workflow_name, exact=True).click() def get_workflow_options(self) -> list[str]: self.workflow_select.click() self.page.wait_for_load_state("domcontentloaded") options = self.page.locator( ".ant-select-dropdown:visible .ant-select-item" ).all_text_contents() return [o.strip() for o in options if o.strip()] def open_file_dialog(self) -> None: self.output_location_input.click() self.page.wait_for_load_state("domcontentloaded") def select_personal_space(self, space_name: str) -> None: self.file_dialog_personal_space.filter(has_text=space_name).click() def select_team_space(self, space_name: str) -> None: self.file_dialog_team_space.filter(has_text=space_name).click() def cancel_file_dialog(self) -> None: self.file_dialog_cancel.click() def confirm_file_dialog(self) -> None: self.file_dialog_confirm.click() def enable_scoring(self, enable: bool = True) -> None: if enable: if not self.enable_scoring_checkbox.is_checked(): self.enable_scoring_checkbox.click() else: if self.enable_scoring_checkbox.is_checked(): self.enable_scoring_checkbox.click() def fill_scoring_prompt(self, prompt: str) -> None: self.enable_scoring(True) self.scoring_prompt_input.fill(prompt) def fill_prompt(self, prompt: str) -> None: self.prompt_input.fill(prompt) def click_execute(self) -> None: self.execute_button.click() def click_reset(self) -> None: self.reset_button.click() def click_back(self) -> None: self.page.get_by_role("button", name="创建任务").click() def is_execute_button_enabled(self) -> bool: return self.execute_button.is_enabled() def get_validation_error(self) -> str: error = self.page.locator(".ant-form-item-explain-error").first if error.is_visible(): return error.text_content() return "" def fill_complete_form( self, task_name: str, workflow: str = None, instance_count: int = 1, generate_count: int = 5, ) -> None: """填写完整的任务表单""" self.fill_task_name(task_name) if workflow: self.select_workflow(workflow) self.fill_instance_count(instance_count) self.fill_generate_count(generate_count) def select_output_path(self, space_type: str = "personal") -> bool: """选择输出路径,返回是否成功""" self.open_file_dialog() self.page.wait_for_load_state("domcontentloaded") try: if space_type == "personal": space = self.page.locator("text=个人空间").first.first else: space = self.page.locator("text=团队空间").first.first if space.is_visible(): space.click() self.page.wait_for_load_state("domcontentloaded") folder = self.page.locator( ".ant-modal:visible .ant-tree-treenode" ).first if folder.is_visible(): folder.click() self.page.wait_for_load_state("domcontentloaded") if self.file_dialog_confirm.is_enabled(): self.file_dialog_confirm.click() return True except Exception: pass return False def execute_task(self) -> bool: """点击执行按钮,处理GPU配置对话框""" self.click_execute() self.page.wait_for_load_state("networkidle") gpu_dialog = self.page.locator(".ant-modal:visible:has-text('GPU资源配置')") if gpu_dialog.is_visible(): return True return False def confirm_gpu_config(self) -> None: """确认GPU配置""" confirm_btn = self.page.locator(".ant-modal:visible button:has-text('确 认')") if confirm_btn.is_visible() and confirm_btn.is_enabled(): confirm_btn.click() self.page.wait_for_load_state("networkidle") def cancel_gpu_config(self) -> None: """取消GPU配置""" cancel_btn = self.page.locator(".ant-modal:visible button:has-text('取 消')") if cancel_btn.is_visible(): cancel_btn.click() self.page.wait_for_load_state("domcontentloaded")