class WorklistPage { getTable() { return cy.get('table'); } findTableAndDoubleClickFirstRow() { cy.get('table').within(() => { cy.get('tbody tr[data-testid="row-0"]') .scrollIntoView() .should('be.visible') .dblclick({ force: true }); }); } /** * 获取表头(排除第一列选择框) */ getTableHeaders() { return cy.get('table thead th').not(':first'); } /** * 获取所有表头 */ getAllTableHeaders() { return cy.get('table thead th'); } /** * 获取特定列 * @param columnName 列名文本 */ getColumn(columnName: string) { return cy.get('table thead th').contains(columnName); } /** * 验证列存在 * @param columnName 列名文本 */ columnExists(columnName: string) { return this.getTableHeaders().contains(columnName).should('exist'); } /** * 验证列不存在 * @param columnName 列名文本 */ columnNotExists(columnName: string) { return this.getTableHeaders().contains(columnName).should('not.exist'); } /** * 获取列宽 * @param columnName 列名文本 */ getColumnWidth(columnName: string) { return cy .get('table thead th') .contains(columnName) .invoke('width'); } /** * 验证列的顺序 * @param expectedOrder 期望的列名顺序数组 */ verifyColumnOrder(expectedOrder: string[]) { this.getTableHeaders().then(($headers) => { expectedOrder.forEach((columnName, index) => { cy.wrap($headers.eq(index)).should('contain', columnName); }); }); } /** * 获取表头数量 */ getHeaderCount() { return this.getTableHeaders().its('length'); } /** * 点击指定索引的行 * @param index 行索引(从0开始) */ clickRowByIndex(index: number) { cy.get('table').within(() => { cy.get(`tbody tr[data-testid="row-${index}"]`) .scrollIntoView() .should('be.visible') .click({ force: true }); }); } /** * 验证指定行被选中(黄色高亮) * @param index 行索引(从0开始) */ verifyRowSelected(index: number) { cy.get('table').within(() => { cy.get(`tbody tr[data-testid="row-${index}"]`) .should('be.visible') .should('have.class', 'bg-yellow-500'); }); } /** * 点击删除按钮 */ clickDeleteButton() { cy.get('[data-testid="delete-button"]').click(); } /** * 获取删除确认对话框 */ getDeleteConfirmModal() { return cy.get('.ant-modal-confirm').should('be.visible'); } /** * 在对话框中确认删除 */ confirmDeleteInModal() { cy.get('[data-testid="modal-confirm-delete"]').click(); } /** * 在对话框中取消删除 */ cancelDeleteInModal() { cy.get('[data-testid="modal-cancel-delete"]').click(); } /** * 验证删除成功提示消息 */ verifyDeleteSuccessMessage() { cy.contains('删除成功').should('be.visible'); } /** * 验证删除警告消息 * @param expectedMessage 期望的警告消息文本 */ verifyDeleteWarningMessage(expectedMessage: string) { cy.contains(expectedMessage).should('be.visible'); } /** * 验证删除错误消息 */ verifyDeleteErrorMessage() { cy.contains('删除失败').should('be.visible'); } /** * 验证表格行数 * @param expectedCount 期望的行数 */ verifyRowCount(expectedCount: number) { cy.get('table tbody tr').should('have.length.at.least', expectedCount); } /** * 验证对话框不存在 */ verifyModalNotExist() { cy.get('.ant-modal-confirm').should('not.exist'); } // ============ 日期范围验证方法 ============ /** * 验证表格为空(无数据) */ verifyTableEmpty() { cy.get('table tbody').should('not.exist'); // 或者验证空状态提示 cy.contains('暂无数据').should('be.visible'); } /** * 验证表格中显示的 study 日期在指定范围内 * @param startDate 开始日期 * @param endDate 结束日期 */ verifyStudyDatesWithinRange(startDate: Date, endDate: Date) { cy.get('table tbody tr').each(($row) => { // 获取表格中的日期列(假设日期在特定列) cy.wrap($row) .find('td') .then(($cells) => { // 查找包含日期的单元格(通常是 StudyStartDatetime 列) const dateText = $cells .toArray() .map((cell) => cell.textContent) .find((text) => text && /\d{4}-\d{2}-\d{2}/.test(text || '')); if (dateText) { const studyDate = new Date(dateText); expect(studyDate.getTime()).to.be.gte(startDate.getTime()); expect(studyDate.getTime()).to.be.lte(endDate.getTime()); } }); }); } /** * 获取表格中第一行的检查日期 */ getFirstRowStudyDate() { return cy .get('table tbody tr') .first() .find('td') .then(($cells) => { // 查找包含日期的单元格 const dateText = $cells .toArray() .map((cell) => cell.textContent) .find((text) => text && /\d{4}-\d{2}-\d{2}/.test(text || '')); return dateText ? new Date(dateText) : null; }); } /** * 验证表格中所有行的日期都是今天 */ verifyAllStudyDatesAreToday() { const today = new Date(); today.setHours(0, 0, 0, 0); const tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); this.verifyStudyDatesWithinRange(today, tomorrow); } /** * 验证表格中所有行的日期在最近7天内 */ verifyAllStudyDatesWithinLast7Days() { const today = new Date(); today.setHours(23, 59, 59, 999); const sevenDaysAgo = new Date(today); sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); sevenDaysAgo.setHours(0, 0, 0, 0); this.verifyStudyDatesWithinRange(sevenDaysAgo, today); } /** * 验证表格至少有指定数量的行 * @param minCount 最小行数 */ verifyTableHasMinRows(minCount: number) { cy.get('table tbody tr').should('have.length.at.least', minCount); } /** * 验证表格显示指定数量的行 * @param count 期望的行数 */ verifyTableHasExactRows(count: number) { if (count === 0) { this.verifyTableEmpty(); } else { cy.get('table tbody tr').should('have.length', count); } } /** * 获取表格中所有 study 的 StudyID */ getAllStudyIds() { return cy.get('table tbody tr').then(($rows) => { const studyIds: string[] = []; $rows.each((index, row) => { const studyId = Cypress.$(row).attr('data-testid')?.replace('row-', ''); if (studyId) { studyIds.push(studyId); } }); return studyIds; }); } /** * 验证指定的 StudyID 在表格中 * @param studyId StudyID */ verifyStudyIdExists(studyId: string) { cy.get('table tbody').contains(studyId).should('be.visible'); } /** * 验证指定的 StudyID 不在表格中 * @param studyId StudyID */ verifyStudyIdNotExists(studyId: string) { cy.get('table tbody').contains(studyId).should('not.exist'); } } export default WorklistPage;