-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemployee-form.page.ts
More file actions
217 lines (185 loc) · 9.18 KB
/
employee-form.page.ts
File metadata and controls
217 lines (185 loc) · 9.18 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
212
213
214
215
216
217
import { Page, Locator } from '@playwright/test';
import { BaseFormPage } from './base-form.page';
/**
* Employee Form Page Object
*
* Employee-specific extension of BaseFormPage.
* Adds field locators and fill/select methods for the create/edit employee form.
*
* Shared logic (submit, cancel, validation, success verification) lives in BaseFormPage.
*/
export class EmployeeFormPage extends BaseFormPage {
// ── Employee-specific field locators ────────────────────────────────────
readonly firstNameInput: Locator;
readonly lastNameInput: Locator;
readonly emailInput: Locator;
readonly employeeNumberInput: Locator;
readonly phoneNumberInput: Locator;
readonly salaryInput: Locator;
readonly dateOfBirthInput: Locator;
readonly positionSelect: Locator;
readonly departmentSelect: Locator;
readonly genderSelect: Locator;
readonly hireDateInput: Locator;
readonly requiredFieldError: Locator;
readonly emailFormatError: Locator;
constructor(page: Page) {
super(page, '/employees');
this.firstNameInput = page.locator('input[name*="firstName"], input[formControlName="firstName"]');
this.lastNameInput = page.locator('input[name*="lastName"], input[formControlName="lastName"]');
this.emailInput = page.locator('input[name*="email"], input[formControlName="email"]');
this.employeeNumberInput = page.locator('input[name*="employeeNumber"], input[formControlName="employeeNumber"]');
this.phoneNumberInput = page.locator('input[name*="phone"], input[formControlName="phoneNumber"]');
this.salaryInput = page.locator('input[name*="salary"], input[formControlName="salary"]');
this.dateOfBirthInput = page.locator('input[name*="dateOfBirth"], input[formControlName="dateOfBirth"], input[type="date"]');
this.positionSelect = page.locator('mat-select[formControlName="positionId"], select[name*="position"]');
this.departmentSelect = page.locator('mat-select[formControlName="departmentId"], select[name*="department"]');
this.genderSelect = page.locator('mat-select[formControlName="gender"], select[name*="gender"]');
this.hireDateInput = page.locator('input[name*="hireDate"], input[formControlName="hireDate"]');
this.requiredFieldError = page.locator('.mat-error, .error').filter({ hasText: /required/i });
this.emailFormatError = page.locator('.mat-error, .error').filter({ hasText: /email|valid|format/i });
}
// ── Employee-specific form-filled check ─────────────────────────────────
protected async isFormStillFilled(): Promise<boolean> {
const firstNameValue = await this.page.getByLabel('First Name').inputValue().catch(() => '');
const lastNameValue = await this.page.getByLabel('Last Name').inputValue().catch(() => '');
return firstNameValue.length > 0 && lastNameValue.length > 0;
}
// ── Navigation ──────────────────────────────────────────────────────────
async gotoCreate() {
await this.page.goto('/employees/create');
await this.page.waitForLoadState('networkidle');
}
async gotoEdit(employeeId: number) {
await this.page.goto(`/employees/${employeeId}/edit`);
await this.page.waitForLoadState('networkidle');
}
// ── Field fill methods ──────────────────────────────────────────────────
async fillFirstName(firstName: string) {
await this.firstNameInput.fill(firstName);
}
async fillLastName(lastName: string) {
await this.lastNameInput.fill(lastName);
}
async fillEmail(email: string) {
await this.emailInput.fill(email);
}
async fillEmployeeNumber(employeeNumber: string) {
const isVisible = await this.employeeNumberInput.isVisible({ timeout: 2000 }).catch(() => false);
if (isVisible) await this.employeeNumberInput.fill(employeeNumber);
}
async fillPhoneNumber(phoneNumber: string) {
try {
await this.page.getByLabel('Phone Number').fill(phoneNumber);
await this.page.waitForTimeout(300);
} catch {
try { await this.phoneNumberInput.fill(phoneNumber, { timeout: 2000 }); } catch {}
}
}
async fillDateOfBirth(dateOfBirth: string) {
try {
await this.page.getByLabel('Date of Birth').fill(dateOfBirth);
await this.page.waitForTimeout(300);
} catch {
try { await this.dateOfBirthInput.fill(dateOfBirth, { timeout: 2000 }); } catch {}
}
}
async fillSalary(salary: number | string) {
const isVisible = await this.salaryInput.isVisible({ timeout: 2000 }).catch(() => false);
if (isVisible) await this.salaryInput.fill(salary.toString());
}
async fillHireDate(date: string) {
const isVisible = await this.hireDateInput.isVisible({ timeout: 2000 }).catch(() => false);
if (isVisible) await this.hireDateInput.fill(date);
}
// ── Dropdown selection methods ──────────────────────────────────────────
// Uses .nth(1) by default to skip blank placeholder option
async selectPosition(positionName: string | number = 1) {
await this.selectDropdown(this.positionSelect, positionName);
}
async selectDepartment(departmentName: string | number = 1) {
await this.selectDropdown(this.departmentSelect, departmentName);
}
async selectGender(genderName: string | number = 1) {
await this.selectDropdown(this.genderSelect, genderName);
}
// ── fillForm convenience method ─────────────────────────────────────────
async fillForm(employeeData: {
firstName: string;
lastName: string;
email: string;
employeeNumber?: string;
phoneNumber?: string;
dateOfBirth?: string;
salary?: number;
position?: string | number;
department?: string | number;
gender?: string | number;
hireDate?: string;
}) {
await this.fillFirstName(employeeData.firstName);
await this.fillLastName(employeeData.lastName);
await this.fillEmail(employeeData.email);
if (employeeData.employeeNumber) await this.fillEmployeeNumber(employeeData.employeeNumber);
if (employeeData.dateOfBirth) await this.fillDateOfBirth(employeeData.dateOfBirth);
if (employeeData.phoneNumber) await this.fillPhoneNumber(employeeData.phoneNumber);
if (employeeData.salary) await this.fillSalary(employeeData.salary);
if (employeeData.position !== undefined) await this.selectPosition(employeeData.position);
if (employeeData.department !== undefined) await this.selectDepartment(employeeData.department);
if (employeeData.gender !== undefined) await this.selectGender(employeeData.gender);
if (employeeData.hireDate) await this.fillHireDate(employeeData.hireDate);
}
// ── Field utilities ─────────────────────────────────────────────────────
async clearField(fieldName: 'firstName' | 'lastName' | 'email' | 'employeeNumber' | 'phoneNumber' | 'salary') {
const fieldMap = {
firstName: this.firstNameInput,
lastName: this.lastNameInput,
email: this.emailInput,
employeeNumber: this.employeeNumberInput,
phoneNumber: this.phoneNumberInput,
salary: this.salaryInput,
};
await fieldMap[fieldName].clear();
}
async blurField(fieldName: 'firstName' | 'lastName' | 'email' | 'employeeNumber' | 'phoneNumber' | 'salary') {
const fieldMap = {
firstName: this.firstNameInput,
lastName: this.lastNameInput,
email: this.emailInput,
employeeNumber: this.employeeNumberInput,
phoneNumber: this.phoneNumberInput,
salary: this.salaryInput,
};
await fieldMap[fieldName].blur();
await this.page.waitForTimeout(500);
}
async getFieldValue(fieldName: 'firstName' | 'lastName' | 'email' | 'employeeNumber' | 'phoneNumber' | 'salary'): Promise<string> {
const fieldMap = {
firstName: this.firstNameInput,
lastName: this.lastNameInput,
email: this.emailInput,
employeeNumber: this.employeeNumberInput,
phoneNumber: this.phoneNumberInput,
salary: this.salaryInput,
};
return await fieldMap[fieldName].inputValue();
}
async isEditMode(): Promise<boolean> {
const firstNameValue = await this.firstNameInput.inputValue();
const lastNameValue = await this.lastNameInput.inputValue();
return firstNameValue.length > 0 || lastNameValue.length > 0;
}
async getFormData(): Promise<{
firstName: string; lastName: string; email: string;
employeeNumber: string; phoneNumber: string; salary: string;
}> {
return {
firstName: await this.getFieldValue('firstName'),
lastName: await this.getFieldValue('lastName'),
email: await this.getFieldValue('email'),
employeeNumber: await this.getFieldValue('employeeNumber'),
phoneNumber: await this.getFieldValue('phoneNumber'),
salary: await this.getFieldValue('salary'),
};
}
}