106 lines
4.1 KiB
TypeScript
106 lines
4.1 KiB
TypeScript
|
import { Page, Locator } from "@playwright/test"
|
||
|
import { User } from '../interfaces/User'
|
||
|
import { formatDate, getMembershipTypeById } from "../utils"
|
||
|
|
||
|
|
||
|
export class UserPersonalInformationPage {
|
||
|
readonly page: Page
|
||
|
readonly primaryTitle: Locator
|
||
|
readonly secondaryTitle: Locator
|
||
|
readonly modifyButton: Locator
|
||
|
readonly deactivateButton: Locator
|
||
|
readonly deleteButton: Locator
|
||
|
|
||
|
public constructor(page: Page) {
|
||
|
this.page = page;
|
||
|
|
||
|
this.primaryTitle = page.getByRole('heading', { name: 'User management' });
|
||
|
this.secondaryTitle = page.getByRole('heading', { name: ' User personal information' });
|
||
|
this.modifyButton = page.getByRole('link', { name: 'Modify' })
|
||
|
this.deactivateButton = page.getByRole('link', { name: 'Deactivate' })
|
||
|
this.deleteButton = page.getByRole('link', { name: 'Delete' })
|
||
|
}
|
||
|
|
||
|
async getPrimaryTitle() {
|
||
|
try {
|
||
|
return await this.primaryTitle.innerText();
|
||
|
} catch (error) {
|
||
|
console.error("Failed to get primary title:", error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async getSecondaryTitle() {
|
||
|
try {
|
||
|
return await this.secondaryTitle.innerText();
|
||
|
} catch (error) {
|
||
|
console.error("Failed to get secondary title:", error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async isTheUserInfoValid(user: User): Promise<boolean> {
|
||
|
|
||
|
try {
|
||
|
const firstNameElement = await this.page.waitForSelector('.col-9.text-secondary', { timeout: 6000 });
|
||
|
const fn = firstNameElement ? await this.page.evaluate(el => (el as HTMLElement).innerText, firstNameElement) : '';
|
||
|
|
||
|
const lastNameElement = await this.page.waitForSelector('.card-body > div:nth-of-type(2) > .col-9.text-secondary', { timeout: 6000 });
|
||
|
const ln = lastNameElement ? await this.page.evaluate(el => (el as HTMLElement).innerText, lastNameElement) : '';
|
||
|
|
||
|
const emailElement = await this.page.waitForSelector('.card-body > div:nth-of-type(3) > .col-9.text-secondary', { timeout: 6000 });
|
||
|
const em = emailElement ? await this.page.evaluate(el => (el as HTMLElement).innerText, emailElement) : '';
|
||
|
|
||
|
const membershipTypeElement = await this.page.$$eval('tr > td:nth-of-type(1)', elements => elements.map(el => el.textContent ? el.textContent.trim() : ''));
|
||
|
const mst = membershipTypeElement[0];
|
||
|
|
||
|
const fromDateElement = await this.page.$$eval('tr > td:nth-of-type(2)', elements => elements.map(el => el.textContent ? el.textContent.trim() : ''));
|
||
|
const fromDate = fromDateElement[0];
|
||
|
|
||
|
const toDateElement = await this.page.$$eval('tr > td:nth-of-type(3)', elements => elements.map(el => el.textContent ? el.textContent.trim() : ''));
|
||
|
const toDate = toDateElement[0];
|
||
|
|
||
|
if (user.firstName === fn && user.lastName === ln && user.email === em && getMembershipTypeById(user.membershipType) === mst && formatDate(user.startDate) === fromDate && formatDate(user.endDate) === toDate) {
|
||
|
return true
|
||
|
}
|
||
|
else { return false }
|
||
|
} catch (error) {
|
||
|
console.error("Failed to retrieve user information:", error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async selectModifyUser() {
|
||
|
try {
|
||
|
await this.modifyButton.click({ timeout: 60000 })
|
||
|
} catch (error) {
|
||
|
console.error("Failed to click the Modify button:", error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async selectDeleteUser() {
|
||
|
try {
|
||
|
await this.deleteButton.click();
|
||
|
} catch (error) {
|
||
|
console.error("Failed to click the Delete button:", error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async deleteUser(page: Page) {
|
||
|
try {
|
||
|
await this.deleteButton.click();
|
||
|
await this.page.waitForSelector('.modal-body', { timeout: 5000 });
|
||
|
await this.page.click('a.btn.btn-danger');//delete
|
||
|
//await page.click('button.btn.btn-secondary');//cancel
|
||
|
} catch (error) {
|
||
|
console.error("Failed to delete the user:", error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|