taskCenter.e2e.js 8.36 KB
/**
 * 任务中心 E2E 测试
 *
 * 测试场景:
 * 1. 导航到任务中心页面
 * 2. 验证任务列表显示
 * 3. 使用筛选条件搜索任务
 * 4. 查看任务详情
 * 5. 验证排序功能
 * 6. 验证分页功能
 * 7. 验证任务汇总统计
 *
 * 登录策略:共享 context,beforeAll 登录一次,所有 test 复用同一个 page
 * (应用登录态依赖 Vuex 内存,无法通过 storageState 跨 context 保持)
 */

import { expect, test } from "@playwright/test";
import { createLoggedInPage, getBaseUrl } from "./helpers/auth.js";

const BASE_URL = getBaseUrl();

test.describe("任务中心 (Task Center)", () => {
	let sharedPage;

	// 全局前置:只运行一次,登录后共享 page(Vuex 内存态)
	test.beforeAll(async ({ browser }) => {
		sharedPage = await createLoggedInPage(browser);
		console.log("登录完成");
	});

	// 全部测试结束后清理
	test.afterAll(async () => {
		await sharedPage.context().close();
	});

	test("应该能够访问任务中心页面", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");
		await expect(sharedPage.locator(".table-title-wrapper .title")).toContainText("任务列表");
	});

	test("应该显示任务汇总统计", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const summaryTags = sharedPage.locator(".table-sum-wrapper .el-tag");
		await expect(summaryTags).toHaveCount(4);
		await expect(summaryTags.nth(0)).toContainText("总任务数");
		await expect(summaryTags.nth(1)).toContainText("未完成");
		await expect(summaryTags.nth(2)).toContainText("已完成");
		await expect(summaryTags.nth(3)).toContainText("已逾期");
	});

	test("应该能够使用筛选条件搜索 - 任务编号", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const searchForm = sharedPage.locator(".d2-search");
		await expect(searchForm).toBeVisible();

		const taskNoInput = sharedPage.locator("input[placeholder*=\"任务编号\"], input[name*=\"taskNo\"]");
		if (await taskNoInput.count() > 0) {
			await taskNoInput.fill("T2026");
		}

		const searchButton = sharedPage.locator("button:has-text(\"查询\")");
		await expect(searchButton).toBeVisible();
		await searchButton.click();
		await sharedPage.waitForLoadState("networkidle");
	});

	test("应该能够使用筛选条件搜索 - 任务标题", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const titleInput = sharedPage.locator("input[placeholder*=\"任务标题\"], input[name*=\"title\"]");
		if (await titleInput.count() > 0) {
			await titleInput.fill("测试任务");
		}

		const searchButton = sharedPage.locator("button:has-text(\"查询\")");
		await searchButton.click();
		await sharedPage.waitForLoadState("networkidle");
	});

	test("应该能够使用筛选条件搜索 - 优先级", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const prioritySelect = sharedPage.locator("select[name*=\"priorities\"], .el-select:has-text(\"优先级\")");
		if (await prioritySelect.count() > 0) {
			await prioritySelect.click();
			const urgentOption = sharedPage.locator(".el-select-dropdown__item:has-text(\"紧急\")");
			if (await urgentOption.count() > 0) {
				await urgentOption.click();
			}
		}

		const searchButton = sharedPage.locator("button:has-text(\"查询\")");
		await searchButton.click();
		await sharedPage.waitForLoadState("networkidle");
	});

	test("应该能够使用筛选条件搜索 - 完成状态", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const statusSelect = sharedPage.locator("select[name*=\"status\"], .el-select:has-text(\"完成状态\")");
		if (await statusSelect.count() > 0) {
			await statusSelect.click();
			const finishedOption = sharedPage.locator(".el-select-dropdown__item:has-text(\"已完成\")");
			if (await finishedOption.count() > 0) {
				await finishedOption.click();
			}
		}

		const searchButton = sharedPage.locator("button:has-text(\"查询\")");
		await searchButton.click();
		await sharedPage.waitForLoadState("networkidle");
	});

	test("应该能够使用筛选条件搜索 - 是否逾期", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const overdueSelect = sharedPage.locator("select[name*=\"overdueFlag\"], .el-select:has-text(\"是否逾期\")");
		if (await overdueSelect.count() > 0) {
			await overdueSelect.click();
			const overdueOption = sharedPage.locator(".el-select-dropdown__item:has-text(\"逾期\")");
			if (await overdueOption.count() > 0) {
				await overdueOption.click();
			}
		}

		const searchButton = sharedPage.locator("button:has-text(\"查询\")");
		await searchButton.click();
		await sharedPage.waitForLoadState("networkidle");
	});

	test("应该能够重置搜索条件", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const resetButton = sharedPage.locator("button:has-text(\"重置\")");
		await expect(resetButton).toBeVisible();
		await resetButton.click();
		await sharedPage.waitForLoadState("networkidle");
	});

	test("应该能够切换排序方式", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const sortDropdown = sharedPage.locator(".sort-wrapper .el-dropdown-link");
		await expect(sortDropdown).toBeVisible();
		await sortDropdown.click();

		const sortOptions = sharedPage.locator(".el-dropdown-menu__item");
		await expect(sortOptions).toHaveCount(2);
		await expect(sortOptions.nth(0)).toContainText("按创建时间");
		await expect(sortOptions.nth(1)).toContainText("按截止时间");

		await sortOptions.nth(1).click();
		await sharedPage.waitForLoadState("networkidle");
		await expect(sortDropdown).toContainText("按截止时间");
	});

	test("应该能够查看任务详情", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");

		const table = sharedPage.locator(".d2-table");
		await expect(table).toBeVisible();

		const firstRow = sharedPage.locator(".el-table__body tr").first();
		await expect(firstRow).toBeVisible();

		const taskNoCell = firstRow.locator("td").nth(1);
		const taskNo = await taskNoCell.textContent();

		const viewButton = firstRow.locator("button:has-text(\"查看\"), .auth-button");
		if (await viewButton.count() > 0) {
			await viewButton.click();
			await sharedPage.waitForLoadState("networkidle");
			await expect(sharedPage).toHaveURL(/.*task-center\/detail.*taskId=/);

			const detailTitle = sharedPage.locator(".title-wrapper .title");
			await expect(detailTitle).toBeVisible();
		}
	});

	test("详情页应该显示任务信息", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center/detail?taskId=TASK001`);
		await sharedPage.waitForLoadState("networkidle");

		const descriptionWrapper = sharedPage.locator(".description-wrapper");
		await expect(descriptionWrapper).toBeVisible();

		const backButton = sharedPage.locator(".back-btn, button:has-text('Back'), .VenusBackButton");
		if (await backButton.count() > 0) {
			await expect(backButton.first()).toBeVisible();
		}
	});

	test("详情页应该能够返回", async () => {
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center/detail?taskId=TASK001`);
		await sharedPage.waitForLoadState("networkidle");

		const backButton = sharedPage.locator(".back-btn, .VenusBackButton");
		if (await backButton.count() > 0) {
			await backButton.first().click();
			await sharedPage.waitForLoadState("networkidle");
			await expect(sharedPage).toHaveURL(/.*resident-manage\/task-center.*/);
		}
	});

	test("移动端视口适配", async () => {
		await sharedPage.setViewportSize({ width: 375, height: 667 });
		await sharedPage.goto(`${BASE_URL}/#/resident-manage/task-center`);
		await sharedPage.waitForLoadState("networkidle");
		await expect(sharedPage.locator(".table-title-wrapper .title")).toBeVisible();
	});
});