Skip to content

Commit

Permalink
test: updated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Oct 24, 2024
1 parent 0924b10 commit 2a2a983
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 85 deletions.
6 changes: 3 additions & 3 deletions page-objects/layout/detailLayoutPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Page } from '@playwright/test';
import ListLayoutPage from '@/page-objects/layout/listLayoutPage';
import NormalLayoutPage from '@/page-objects/layout/normalLayoutPage';

export default abstract class DetailLayoutPage<T extends ListLayoutPage> extends NormalLayoutPage {
private listPage: T;
export default abstract class DetailLayoutPage<T, L extends ListLayoutPage<T>> extends NormalLayoutPage {
private listPage: L;

protected abstract getListPage(): T;
protected abstract getListPage(): L;

constructor(page: Page) {
super(page);
Expand Down
22 changes: 21 additions & 1 deletion page-objects/layout/listLayoutPage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import NormalLayoutPage from '@/page-objects/layout/normalLayoutPage';

export default abstract class ListLayoutPage extends NormalLayoutPage {
export default abstract class ListLayoutPage<T> extends NormalLayoutPage {
protected abstract path: string;

protected listContainer = '.list-layout';
protected createButton = '#add-btn';
protected searchInput = '#filter-search .el-input input';
protected tableRows = '.list-layout table tbody tr';
protected viewButton = '.view-btn';
protected deleteButton = '.delete-btn';

async navigate() {
await super.navigate();
Expand All @@ -23,4 +24,23 @@ export default abstract class ListLayoutPage extends NormalLayoutPage {
const row = this.page.locator(this.tableRows).nth(rowIndex);
await row.locator(this.viewButton).click();
}

async searchRows(searchTerm: string) {
await this.page.fill(this.searchInput, searchTerm);
}

async clickCreate() {
await this.page.click(this.createButton);
}

async deleteRow(rowIndex: number) {
const row = this.page.locator(this.tableRows).nth(rowIndex);
await row.locator(this.deleteButton).click();
}

async getTableRowCount(): Promise<number> {
return await this.page.locator(this.tableRows).count();
}

abstract getTableRow(rowIndex: number): Promise<T>;
}
4 changes: 2 additions & 2 deletions page-objects/node/nodeDetailPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NodeListPage } from '@/page-objects/node/nodeListPage';
import DetailLayoutPage from '@/page-objects/layout/detailLayoutPage';

export class NodeDetailPage extends DetailLayoutPage<NodeListPage> {
export class NodeDetailPage extends DetailLayoutPage<Node, NodeListPage> {
private nameField = '.form-item[data-test="name"] input';
private typeSelector = '.form-item[data-test="type"] .el-tag';
private enabledSwitch = '.form-item[data-test="enabled"] .el-switch__input';
Expand All @@ -10,7 +10,7 @@ export class NodeDetailPage extends DetailLayoutPage<NodeListPage> {
private currentMetricsSelector = '.current-metrics';
private monitoringTabSelector = '.nav-tabs [data-test="monitoring"]';

getListPage(): NodeListPage {
getListPage() {
return new NodeListPage(this.page);
}

Expand Down
20 changes: 3 additions & 17 deletions page-objects/node/nodeListPage.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import ListLayoutPage from '@/page-objects/layout/listLayoutPage';

export class NodeListPage extends ListLayoutPage {
export class NodeListPage extends ListLayoutPage<Node> {
protected path = '/#/nodes';

// Updated Locators
private nodeTypeFilter = '#filter-select-type .el-select';
private nodeStatusFilter = '#filter-select-status .el-select';
private nodeEnabledFilter = '#filter-select-enabled .el-select';
private createNodeNotice = '.create-node-notice'; // Adjust this selector as needed

// Updated Actions
async clickCreateNode() {
await this.page.click(this.createButton);
}

async searchNode(searchTerm: string) {
await this.page.fill(this.searchInput, searchTerm);
}

async filterByNodeType(type: string) {
await this.selectOption(this.nodeTypeFilter, type);
Expand All @@ -40,17 +30,13 @@ export class NodeListPage extends ListLayoutPage {
return await this.page.locator(this.tableRows).count();
}

async getNodeData(rowIndex: number): Promise<{ name: string; type: string; status: string; enabled: string }> {
async getTableRow(rowIndex: number) {
const row = this.page.locator(this.tableRows).nth(rowIndex);
return {
name: await row.locator('td:nth-child(2)').innerText(),
type: await row.locator('td:nth-child(3)').innerText(),
status: await row.locator('td:nth-child(4)').innerText(),
enabled: await row.locator('td:nth-child(6)').innerText(),
};
}

async getCreateNodeNoticeContent(): Promise<string | null> {
return await this.page.locator(this.createNodeNotice).textContent();
} as Node;
}
}
Empty file.
46 changes: 6 additions & 40 deletions page-objects/project/projectListPage.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,20 @@
import ListLayoutPage from '@/page-objects/layout/listLayoutPage';

export class ProjectListPage extends ListLayoutPage {
export class ProjectListPage extends ListLayoutPage<Project> {
protected path = '/#/projects';

// Locators
private createProjectButton = '#add-btn';
private projectNameColumn = 'td.name';
private projectSpidersColumn = 'td.spiders';
private projectDescriptionColumn = 'td:nth-child(3)';
private deleteButton = '.delete-btn';
private projectNameColumn = 'td:nth-child(2)';
private projectSpidersColumn = 'td:nth-child(3)';
private projectDescriptionColumn = 'td:nth-child(4)';

async navigate() {
await super.navigate();
await this.page.goto(this.path);
await this.waitForPageLoad();
}

async waitForPageLoad() {
await this.page.waitForSelector(this.listContainer);
}

async clickCreateProject() {
await this.page.click(this.createProjectButton);
}

async searchProject(searchTerm: string) {
await this.page.fill(this.searchInput, searchTerm);
}

async getProjectCount(): Promise<number> {
return await this.page.locator(this.tableRows).count();
}

async getProjectData(rowIndex: number): Promise<{ name: string; spiders: string; description: string }> {
async getTableRow(rowIndex: number) {
const row = this.page.locator(this.tableRows).nth(rowIndex);
return {
name: await row.locator(this.projectNameColumn).innerText(),
spiders: await row.locator(this.projectSpidersColumn).innerText(),
description: await row.locator(this.projectDescriptionColumn).innerText(),
};
}

async viewProject(rowIndex: number) {
const row = this.page.locator(this.tableRows).nth(rowIndex);
await row.locator(this.viewButton).click();
}

async deleteProject(rowIndex: number) {
const row = this.page.locator(this.tableRows).nth(rowIndex);
await row.locator(this.deleteButton).click();
} as Project;
}
}

12 changes: 6 additions & 6 deletions tests/node/nodeList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test.describe('Node List Tests', () => {
const nodeCount = await nodeListPage.getNodeCount();
expect(nodeCount).toBeGreaterThan(0);

const firstNodeData = await nodeListPage.getNodeData(0);
const firstNodeData = await nodeListPage.getTableRow(0);
expect(firstNodeData.type).toContain('Master' || '主节点');
});

Expand All @@ -32,26 +32,26 @@ test.describe('Node List Tests', () => {
const nodeCount = await nodeListPage.getNodeCount();
expect(nodeCount).toBeGreaterThan(0);

const firstNodeData = await nodeListPage.getNodeData(0);
const firstNodeData = await nodeListPage.getTableRow(0);
expect(firstNodeData.status).toBe('Online' || '在线');
});

test('should search for a node by name', async ({ page }) => {
const { name } = await nodeListPage.getNodeData(0);
const { name } = await nodeListPage.getTableRow(0);
const searchTerm = name;
await nodeListPage.searchNode(searchTerm);
await nodeListPage.searchRows(searchTerm);
await page.waitForTimeout(1000); // Add a 1-second wait

const nodeCount = await nodeListPage.getNodeCount();
expect(nodeCount).toBe(1);

const firstNodeData = await nodeListPage.getNodeData(0);
const firstNodeData = await nodeListPage.getTableRow(0);
expect(firstNodeData.name).toContain(searchTerm);
});

test('should search for a non-existent node', async ({ page }) => {
const searchTerm = 'Non-Existent Node';
await nodeListPage.searchNode(searchTerm);
await nodeListPage.searchRows(searchTerm);
await page.waitForTimeout(1000); // Add a 1-second wait

const nodeCount = await nodeListPage.getNodeCount();
Expand Down
24 changes: 12 additions & 12 deletions tests/project/projectList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ test.describe.skip('Project List Tests', () => {
});

test('should display the project list', async () => {
const projectCount = await projectListPage.getProjectCount();
expect(projectCount).toBeGreaterThan(0);
const projectCount = await projectListPage.getTableRowCount();
expect(projectCount).toBeGreaterThanOrEqual(0);
});

test('should create a new project', async ({ page }) => {
const initialCount = await projectListPage.getProjectCount();
await projectListPage.clickCreateProject();
const initialCount = await projectListPage.getTableRowCount();
await projectListPage.clickCreate();

// Here you would interact with the create project dialog
// For this example, we'll assume the dialog is handled elsewhere
Expand All @@ -37,12 +37,12 @@ test.describe.skip('Project List Tests', () => {

test('should search for a project', async ({ page }) => {
const searchTerm = 'Test Project';
await projectListPage.searchProject(searchTerm);
await projectListPage.searchRows(searchTerm);
await page.waitForTimeout(1000); // Wait for search results

const projectCount = await projectListPage.getProjectCount();
const projectCount = await projectListPage.getTableRowCount();
if (projectCount > 0) {
const firstProjectData = await projectListPage.getProjectData(0);
const firstProjectData = await projectListPage.getTableRow(0);
expect(firstProjectData.name).toContain(searchTerm);
} else {
// If no projects found, that's okay too
Expand All @@ -51,9 +51,9 @@ test.describe.skip('Project List Tests', () => {
});

test('should navigate to project detail', async ({ page }) => {
const projectCount = await projectListPage.getProjectCount();
const projectCount = await projectListPage.getTableRowCount();
if (projectCount > 0) {
await projectListPage.viewProject(0);
await projectListPage.navigateToDetail(0);
await page.waitForNavigation();
expect(page.url()).toContain('/projects/');
} else {
Expand All @@ -63,16 +63,16 @@ test.describe.skip('Project List Tests', () => {
});

test('should delete a project', async ({ page }) => {
const projectCount = await projectListPage.getProjectCount();
const projectCount = await projectListPage.getTableRowCount();
if (projectCount > 0) {
const initialCount = projectCount;
await projectListPage.deleteProject(0);
await projectListPage.deleteRow(0);

// Assume there's a confirmation dialog
await page.click('.el-message-box__btns .el-button--primary');

await page.waitForTimeout(1000); // Wait for deletion to process
const newCount = await projectListPage.getProjectCount();
const newCount = await projectListPage.getTableRowCount();
expect(newCount).toBe(initialCount - 1);
} else {
// If no projects, skip this test
Expand Down
21 changes: 17 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {
"@/*": ["./*"]
"@/*": [
"./*"
]
},
"types": ["node", "@playwright/test"]
"types": [
"node",
"@playwright/test"
],
"typeRoots": [
"./node_modules/@types",
"typings"
]
},
"include": ["**/*.ts"],
"exclude": ["node_modules"]
"include": [
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './node';
export * from './project';
8 changes: 8 additions & 0 deletions typings/node.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export declare global {
interface Node {
name: string;
type: string;
status: string;
enabled: string;
}
}
7 changes: 7 additions & 0 deletions typings/project.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export declare global {
interface Project {
name: string;
description: string;
spiders: string;
}
}

0 comments on commit 2a2a983

Please sign in to comment.