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 Nov 1, 2024
1 parent bcd48bc commit 61f9f08
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 73 deletions.
9 changes: 9 additions & 0 deletions page-objects/layout/baseLayoutPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { Page } from '@playwright/test';

export default abstract class BaseLayoutPage {
protected page: Page;
protected confirmButton = '[custom-class~="visible"] #confirm-btn';

constructor(page: Page) {
this.page = page;
}

async confirm() {
await this.page.click(this.confirmButton);
}

async selectOption(selector: string, value: string, dropdownSelector = '.el-select-dropdown') {
await this.page.click(selector);
await this.page.click(`${dropdownSelector} [data-test="${value}"]`);
Expand All @@ -16,4 +21,8 @@ export default abstract class BaseLayoutPage {
await this.page.click(selector);
await this.page.click(`${dropdownSelector} [data-test-text="${text}"]`);
}

async clickContextMenuItem(selector: string) {
await this.page.click(`.context-menu[aria-hidden="false"] ${selector}`);
}
}
20 changes: 14 additions & 6 deletions page-objects/layout/listLayoutPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import NormalLayoutPage from '@/page-objects/layout/normalLayoutPage';
import { FormPage } from '@/page-objects/components/form/formPage';
import { Page } from '@playwright/test';

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

Expand All @@ -15,10 +15,10 @@ export default abstract class ListLayoutPage<T> extends NormalLayoutPage {

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

Expand Down Expand Up @@ -49,13 +49,14 @@ export default abstract class ListLayoutPage<T> extends NormalLayoutPage {
await this.page.click(this.createButton);
}

async confirm() {
await this.page.click(this.confirmButton);
async clickShowMore(rowIndex: number) {
const row = this.page.locator(this.tableRows).nth(rowIndex);
await row.locator(this.showMoreButton).click();
}

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

Expand All @@ -65,6 +66,13 @@ export default abstract class ListLayoutPage<T> extends NormalLayoutPage {
await this.confirm();
}

async createRowWithRandomName(form: T) {
const randomName = `${form.name} ${Date.now()}`;
const formWithRandomName: T = { ...form, name: randomName };
await this.createRow(formWithRandomName);
return formWithRandomName;
}

async getTableRowCount(): Promise<number> {
return await this.page.locator(this.tableRows).count();
}
Expand Down
5 changes: 0 additions & 5 deletions page-objects/views/project/projectListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ export class ProjectListPage extends ListLayoutPage<Project> {
private spidersColumn = 'td:nth-child(3)';
private descriptionColumn = 'td:nth-child(4)';

async createRow(form: Project) {
await this.clickCreate();
await this.formPage.fillForm(form);
}

async getTableRow(rowIndex: number) {
const row = this.page.locator(this.tableRows).nth(rowIndex);
return {
Expand Down
26 changes: 16 additions & 10 deletions page-objects/views/spider/spiderListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@ export class SpiderListPage extends ListLayoutPage<Spider> {
private descriptionColumn = '.description';
private runButton = '.run-btn';
private uploadFilesButton = '.upload-files-btn';
private viewTasksButton = '.view-tasks-btn';
private viewSchedulesButton = '.view-schedules-btn';
private viewDataButton = '.view-data-btn';
private projectFilterSelector = '#filter-select-project';

async filterByProject(projectName: string) {
await this.selectOptionByText(this.projectFilterSelector, projectName);
}

async isCreateEditSpiderDialogVisible() {
return await this.page.isVisible('.create-edit-spider-dialog');
}

async isRunSpiderDialogVisible() {
return await this.page.isVisible('[custom-class*="run-spider-dialog"]');
}

async isUploadSpiderFilesDialogVisible() {
return await this.page.isVisible('[custom-class*="upload-files-dialog"]');
}
Expand All @@ -48,11 +42,23 @@ export class SpiderListPage extends ListLayoutPage<Spider> {
}

async clickUploadFiles(rowIndex: number) {
await this.page.locator(this.tableRows).nth(rowIndex).locator(this.uploadFilesButton).click();
await this.clickShowMore(rowIndex);
await this.clickContextMenuItem(this.uploadFilesButton);
}

async clickViewTasks(rowIndex: number) {
await this.clickShowMore(rowIndex);
await this.clickContextMenuItem(this.viewTasksButton);
}

async clickViewSchedules(rowIndex: number) {
await this.clickShowMore(rowIndex);
await this.clickContextMenuItem(this.viewSchedulesButton);
}

async clickViewData(rowIndex: number) {
await this.page.locator(this.tableRows).nth(rowIndex).locator(this.viewDataButton).click();
await this.clickShowMore(rowIndex);
await this.clickContextMenuItem(this.viewDataButton);
}

async getTableRow(rowIndex: number) {
Expand Down
39 changes: 19 additions & 20 deletions tests/project/projectList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { TAG_PRIORITY_HIGH, TAG_PRIORITY_MEDIUM } from '@/constants/priority';
import projectData from '@/fixtures/projectData.json';
import { CATEGORY_CREATE_DELETE_ROW, CATEGORY_FILTER_ROWS, CATEGORY_ROW_ACTIONS } from '@/constants/category';

let project: Project;

test.beforeAll(async ({ browser }) => {
const page = await browser.newPage();
const projectListPage = new ProjectListPage(page);
await projectListPage.navigate();
await projectListPage.createRow(projectData.single as Project);
project = await projectListPage.createRowWithRandomName(projectData.single as Project);
});

test.describe('Project List Tests', () => {
Expand All @@ -23,37 +25,34 @@ test.describe('Project List Tests', () => {
});

test.describe.serial(CATEGORY_CREATE_DELETE_ROW, { tag: TAG_PRIORITY_HIGH }, () => {
test('should create a new project', async ({ page }) => {
const initialCount = await projectListPage.getTableRowCount();
await projectListPage.clickCreate();
let project: Project;

test('should create a new project', async ({ page }) => {
// Fill out the project form
await projectFormPage.fillForm(projectData.single as Project);

// Submit the form (you might need to implement this method in ProjectListPage)
await projectListPage.confirm();

// Wait for the new project to appear in the list
project = await projectListPage.createRowWithRandomName(projectData.single as Project);
await page.waitForTimeout(1000);

const newCount = await projectListPage.getTableRowCount();
expect(newCount).toBe(initialCount + 1);
// Search for the new project
await projectListPage.searchRows(project.name);
await page.waitForTimeout(1000); // Wait for search results

// Verify the new project appears in the list
const lastProjectData = await projectListPage.getTableRow(0);
expect(lastProjectData.name).toBe(projectData.single.name);
expect(lastProjectData.description).toBe(projectData.single.description);
expect(lastProjectData.name).toBe(project.name);
expect(lastProjectData.description).toBe(project.description);
});

test('should delete a project', async ({ page }) => {
const projectCount = await projectListPage.getTableRowCount();
expect(projectCount).toBeGreaterThan(0);
const initialCount = projectCount;
await projectListPage.deleteRow(0);
// Search for the project to delete
await projectListPage.searchRows(project.name);
await page.waitForTimeout(1000); // Wait for search results

// Delete the project
await projectListPage.deleteRow(0);
await page.waitForTimeout(1000); // Wait for deletion to process
const newCount = await projectListPage.getTableRowCount();
expect(newCount).toBe(initialCount - 1);

// Verify the project has been deleted
expect(await projectListPage.getTableRowCount()).toBe(0);
});
});

Expand Down
108 changes: 83 additions & 25 deletions tests/spider/spiderList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,56 @@ import path from 'path';
import projectData from '@/fixtures/projectData.json';
import { ProjectListPage } from '@/page-objects/views/project/projectListPage';

let spider: Spider;
let project: Project;

test.beforeAll(async ({ browser }) => {
const page = await browser.newPage();

// Create a project to associate with the spider
const projectListPage = new ProjectListPage(page);
project = await projectListPage.createRowWithRandomName(projectData.single as Project);

// Create a spider
const spiderListPage = new SpiderListPage(page);
await spiderListPage.navigate();
await spiderListPage.createRow(spiderData.single as Spider);
spider = await spiderListPage.createRowWithRandomName({
...spiderData.single as Spider,
project: project.name,
});

// Create a project to associate with the spider
// Close the browser
await page.close();
});

test.afterAll(async ({ browser }) => {
test.slow();

// Open a new page
const page = await browser.newPage();

// Delete the spider
const spiderListPage = new SpiderListPage(page);
await spiderListPage.navigate();
await spiderListPage.searchRows(spider.name);
await page.waitForTimeout(1000); // Wait for search results
if (await spiderListPage.getTableRowCount() > 0) {
await spiderListPage.deleteRow(0);
await page.waitForTimeout(1000); // Wait for deletion to process
}

// Delete the project
const projectListPage = new ProjectListPage(page);
await projectListPage.createRow(projectData.single as Project);
await projectListPage.navigate();
await projectListPage.searchRows(project.name);
await page.waitForTimeout(1000); // Wait for search results
if (await projectListPage.getTableRowCount() > 0) {
await projectListPage.deleteRow(0);
await page.waitForTimeout(1000); // Wait for deletion to process
}

// Close the browser
await page.close();
});

test.describe('Spider List Tests', () => {
Expand All @@ -28,17 +69,19 @@ test.describe('Spider List Tests', () => {
});

test.describe.serial(CATEGORY_CREATE_DELETE_ROW, { tag: TAG_PRIORITY_CRITICAL }, () => {
test('should create a new spider', async ({ page }) => {
const spider = spiderData.create as Spider;
const initialCount = await spiderListPage.getTotalCount();
let spider: Spider;

await spiderListPage.createRow(spider);
test('should create a new spider', async ({ page }) => {
// Create a new spider
spider = await spiderListPage.createRowWithRandomName(spiderData.single as Spider);

// Wait for the new spider to appear in the list
await page.reload();
await page.waitForTimeout(1000);

const newCount = await spiderListPage.getTotalCount();
expect(newCount).toBe(initialCount + 1);
// Search for the new spider
await spiderListPage.searchRows(spider.name);
await page.waitForTimeout(1000); // Wait for search results

// Verify the new spider appears in the list
const lastSpiderData = await spiderListPage.getTableRow(0);
Expand All @@ -48,42 +91,42 @@ test.describe('Spider List Tests', () => {
});

test('should delete a spider', async ({ page }) => {
const initialCount = await spiderListPage.getTotalCount();
expect(initialCount).toBeGreaterThan(0);

await spiderListPage.searchRows(spiderData.create.name);
// Search for the spider to delete
await spiderListPage.searchRows(spider.name);
await page.waitForTimeout(1000); // Wait for search results

// Delete the spider
await spiderListPage.deleteRow(0);
await page.waitForTimeout(1000); // Wait for deletion to process
await page.reload();
await page.waitForTimeout(1000); // Wait for the page to reload

const newCount = await spiderListPage.getTotalCount();
expect(newCount).toBe(initialCount - 1);
// Verify the spider has been deleted
expect(await spiderListPage.getTableRowCount()).toBe(0);
});
});

test.describe(CATEGORY_ROW_ACTIONS, { tag: TAG_PRIORITY_HIGH }, () => {
test('should run a spider', async ({ page }) => {
test.slow();

const spider = spiderData.single as Spider;

// Search for the spider and run it
await spiderListPage.searchRows(spider.name);
const spiderCount = await spiderListPage.getTableRowCount();
expect(spiderCount).toBeGreaterThan(0);

await spiderListPage.searchRows(spider.name);
await spiderListPage.runSpider(0);

// Refresh the page to get the latest data
// Reload the page and verify the spider has been run
await page.reload();
await page.waitForTimeout(1000); // Wait for the page to reload

// Verify the spider has been run
await spiderListPage.searchRows(spider.name);
await page.waitForTimeout(1000); // Wait for search results
const lastSpider = await spiderListPage.getTableRow(0);
expect(lastSpider.last_status).toBeTruthy();
expect(lastSpider.stats).toBeTruthy();

// Delete the spider
await spiderListPage.deleteRow(0);
});

test('should navigate to spider detail', async ({ page }) => {
Expand All @@ -94,6 +137,22 @@ test.describe('Spider List Tests', () => {
expect(page.url()).toMatch(/\/spiders\/[0-9a-f]{24}/);
});

test('should navigate to spider tasks view', async ({ page }) => {
const spiderCount = await spiderListPage.getTableRowCount();
expect(spiderCount).toBeGreaterThan(0);
await spiderListPage.clickViewTasks(0);
await page.waitForSelector('.detail-layout');
expect(page.url()).toMatch(/\/spiders\/[0-9a-f]{24}\/tasks/);
});

test('should navigate to spider schedules view', async ({ page }) => {
const spiderCount = await spiderListPage.getTableRowCount();
expect(spiderCount).toBeGreaterThan(0);
await spiderListPage.clickViewSchedules(0);
await page.waitForSelector('.detail-layout');
expect(page.url()).toMatch(/\/spiders\/[0-9a-f]{24}\/schedules/);
});

test('should navigate to spider data view', async ({ page }) => {
const spiderCount = await spiderListPage.getTableRowCount();
expect(spiderCount).toBeGreaterThan(0);
Expand Down Expand Up @@ -167,14 +226,13 @@ test.describe('Spider List Tests', () => {
});

test('should filter spiders by project', async ({ page }) => {
const projectName = 'Test Project';
await spiderListPage.filterByProject(projectName);
await spiderListPage.filterByProject(spider.project);
await page.waitForTimeout(1000); // Wait for filter results

const spiderCount = await spiderListPage.getTableRowCount();
if (spiderCount > 0) {
const firstSpiderData = await spiderListPage.getTableRow(0);
expect(firstSpiderData.project).toBe(projectName);
expect(firstSpiderData.project).toBe(spider.project);
} else {
// If no spiders found, that's okay too
expect(spiderCount).toBe(0);
Expand Down
5 changes: 5 additions & 0 deletions typings/models/base.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare global {
interface BaseModel {
name: string;
}
}
Loading

0 comments on commit 61f9f08

Please sign in to comment.