-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-list.page.ts
More file actions
211 lines (171 loc) · 8.59 KB
/
base-list.page.ts
File metadata and controls
211 lines (171 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { Page, Locator } from '@playwright/test';
/**
* Base List Page Object
*
* Contains shared logic for ALL list/table pages in this application:
* - Navigation
* - Table row access
* - Search / clear search
* - Pagination (next, previous, page size)
* - CRUD action buttons (Create, Edit, Delete)
* - Permission checks (visibility of action buttons)
*
* Extend this class for each entity's list page:
* class DepartmentListPage extends BaseListPage { ... }
* class PositionListPage extends BaseListPage { ... }
*
* Minimal subclass example (just set URL + entity name):
* export class DepartmentListPage extends BaseListPage {
* constructor(page: Page) { super(page, '/departments', 'departments'); }
* }
*/
export class BaseListPage {
protected readonly page: Page;
protected readonly url: string;
protected readonly entityName: string;
// ── Table locators ──────────────────────────────────────────────────────
readonly pageTitle: Locator;
readonly table: Locator;
readonly rows: Locator;
// ── Search locators ─────────────────────────────────────────────────────
readonly searchInput: Locator;
readonly clearSearchButton: Locator;
// ── Action locators ─────────────────────────────────────────────────────
readonly createButton: Locator;
// ── Pagination locators ─────────────────────────────────────────────────
readonly paginator: Locator;
readonly nextPageButton: Locator;
readonly previousPageButton: Locator;
readonly pageSizeSelector: Locator;
// ── State locators ──────────────────────────────────────────────────────
readonly loadingIndicator: Locator;
constructor(page: Page, url: string, entityName: string) {
this.page = page;
this.url = url;
this.entityName = entityName;
this.pageTitle = page.locator('h1, h2, h3').filter({ hasText: new RegExp(entityName, 'i') });
this.table = page.locator('table, mat-table').first();
this.rows = page.locator('tr, mat-row');
this.searchInput = page.locator('input[placeholder*="Search"], input[name*="search"]').first();
this.clearSearchButton = page.locator('button[aria-label*="clear"], .clear-search');
this.createButton = page.locator('button').filter({ hasText: /create|add|new/i }).first();
this.paginator = page.locator('mat-paginator, .pagination').first();
this.nextPageButton = page.locator('button[aria-label*="Next"]').first();
this.previousPageButton = page.locator('button[aria-label*="Previous"]').first();
this.pageSizeSelector = page.locator('mat-select[aria-label*="Items per page"]');
this.loadingIndicator = page.locator('mat-spinner, .spinner, .loading');
}
// ── Navigation ──────────────────────────────────────────────────────────
async goto() {
await this.page.goto(this.url);
await this.page.waitForLoadState('networkidle');
}
async waitForLoad() {
await this.table.waitFor({ state: 'visible', timeout: 5000 });
await this.loadingIndicator.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {});
}
// ── Row access ──────────────────────────────────────────────────────────
/**
* Get a data row by zero-based index (automatically skips the header row).
*/
getRow(index: number): Locator {
return this.rows.nth(index + 1); // +1 to skip <thead> / header mat-row
}
/**
* Get the first row whose text matches the given string.
*/
getRowByText(text: string): Locator {
return this.rows.filter({ hasText: text }).first();
}
async getRowCount(): Promise<number> {
const count = await this.rows.count();
return count > 1 ? count - 1 : count; // subtract header row
}
async getCellText(rowIndex: number, cellIndex: number): Promise<string> {
const cell = this.getRow(rowIndex).locator('td, mat-cell').nth(cellIndex);
return await cell.textContent() || '';
}
async getRowData(rowIndex: number): Promise<string[]> {
const cells = this.getRow(rowIndex).locator('td, mat-cell');
const count = await cells.count();
const data: string[] = [];
for (let i = 0; i < count; i++) {
data.push(await cells.nth(i).textContent() || '');
}
return data;
}
// ── CRUD actions ────────────────────────────────────────────────────────
async clickCreate() {
await this.createButton.click();
await this.page.waitForTimeout(1000);
}
async clickRow(index: number) {
await this.getRow(index).click();
await this.page.waitForTimeout(1000);
}
async clickEdit(index: number) {
const editButton = this.getRow(index).locator('button, a').filter({ hasText: /edit|update/i }).first();
await editButton.click();
await this.page.waitForTimeout(1000);
}
async clickDelete(index: number) {
const deleteButton = this.getRow(index).locator('button').filter({ hasText: /delete|remove/i }).first();
await deleteButton.click();
await this.page.waitForTimeout(1000);
}
// ── Search ──────────────────────────────────────────────────────────────
async search(searchText: string) {
const isVisible = await this.searchInput.isVisible({ timeout: 2000 }).catch(() => false);
if (!isVisible) return; // No search input on this page — skip silently
await this.searchInput.fill(searchText);
await this.page.waitForTimeout(1000); // debounce delay
}
async clearSearch() {
const hasClearButton = await this.clearSearchButton.isVisible({ timeout: 1000 }).catch(() => false);
if (hasClearButton) {
await this.clearSearchButton.click();
} else {
await this.searchInput.clear();
}
await this.page.waitForTimeout(1000);
}
// ── Pagination ──────────────────────────────────────────────────────────
async goToNextPage() {
await this.nextPageButton.click();
await this.page.waitForTimeout(1000);
}
async goToPreviousPage() {
await this.previousPageButton.click();
await this.page.waitForTimeout(1000);
}
async changePageSize(size: number) {
await this.pageSizeSelector.click();
await this.page.waitForTimeout(500);
await this.page.locator('mat-option, option')
.filter({ hasText: new RegExp(`^${size}$`) })
.first()
.click();
await this.page.waitForTimeout(1000);
}
async getPaginationInfo(): Promise<string> {
const info = this.page.locator('text=/\\d+-\\d+ of \\d+|page \\d+ of \\d+/i').first();
return await info.textContent() || '';
}
// ── Permission checks ───────────────────────────────────────────────────
async hasCreatePermission(): Promise<boolean> {
return await this.createButton.isVisible({ timeout: 2000 }).catch(() => false);
}
async hasEditPermission(): Promise<boolean> {
const editButton = this.rows.nth(1).locator('button').filter({ hasText: /edit/i });
return await editButton.isVisible({ timeout: 2000 }).catch(() => false);
}
async hasDeletePermission(): Promise<boolean> {
const deleteButton = this.rows.nth(1).locator('button').filter({ hasText: /delete/i });
return await deleteButton.isVisible({ timeout: 2000 }).catch(() => false);
}
// ── State ───────────────────────────────────────────────────────────────
async isEmptyStateVisible(): Promise<boolean> {
const emptyState = this.page.locator('text=/no.*results|no.*records|empty/i').first();
return await emptyState.isVisible({ timeout: 2000 }).catch(() => false);
}
}