This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
IdHub_E2E_testing/src/page-objects/AD_TemplatesPage.ts
2024-03-25 12:31:43 +01:00

218 lines
7.6 KiB
TypeScript

import { Page, Locator, expect } from "@playwright/test"
import { clickViewSchemaOnLeftMenu } from "../steps"
import { URL_SCHEMAS } from "../constants/env_constants"
export class TemplatesPage {
readonly page: Page
readonly primaryTitle: Locator
readonly secondaryTitle: Locator
readonly createdTitle: Locator
readonly fileSchemaTitle: Locator
readonly nameTitle: Locator
readonly descriptionTitle: Locator
readonly viewSchema: Locator
readonly deleteSchema: Locator
readonly addTemplateButton: Locator
constructor(page: Page) {
this.page = page;
this.primaryTitle = page.getByRole('heading', { name: 'Template management' });
this.secondaryTitle = page.getByRole('heading', { name: 'View credential templates' });
this.createdTitle = page.getByRole('button', { name: 'Created' })
this.fileSchemaTitle = page.getByRole('button', { name: 'File schema' })
this.nameTitle = page.getByRole('button', { name: 'Name' })
this.descriptionTitle = page.getByRole('button', { name: 'Description' })
this.addTemplateButton = page.getByRole('link', { name: 'Add template ' })
}
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 getCreatedTitle() {
try {
return this.createdTitle;
} catch (error) {
console.error("Failed to get Created title:", error);
throw error;
}
}
async getFileSchemaTitle() {
try {
return this.getFileSchemaTitle;
} catch (error) {
console.error("Failed to get File schema title:", error);
throw error;
}
}
async getNameTitle() {
try {
return this.nameTitle;
} catch (error) {
console.error("Failed to get Name title:", error);
throw error;
}
}
async getDescriptionTitle() {
try {
return this.descriptionTitle;
} catch (error) {
console.error("Failed to get Description title:", error);
throw error;
}
}
async getaddTemplateButton() {
try {
return this.addTemplateButton;
} catch (error) {
console.error("Failed to get add template button", error);
throw error;
}
}
async checkSchemaNamesAgainstCorrespondingJSON(page: Page): Promise<boolean> {
try {
// Get the count of table rows
const rowCount = await page.locator('table tbody tr').count();
for (let i = 0; i < rowCount; i++) {
// Get the second cell (file schema name) of the current row
const fileSchemaCell = page.locator(`table tbody tr:nth-child(${i + 1}) td:nth-child(2)`);
const fileSchemaValue = await fileSchemaCell.innerText();
// Click the corresponding element in the fifth cell (eye icon)
await clickViewSchemaOnLeftMenu(page, fileSchemaValue)
// Parse the JSON content of the page
const jsonContent = await page.evaluate(() => JSON.parse(document.body.innerText));
// Extract the last component of the $id path
const idUrl = new URL(jsonContent["$id"]);
const lastPathSegment = idUrl.pathname.split("/").pop();
// Check if the last component of the $id path matches the second cell value (File schema)
expect(lastPathSegment).toBe(fileSchemaValue);
await page.goto(URL_SCHEMAS);
}
return true;
} catch (error) {
console.error("Failed checking the schema against json:", error);
throw error;
}
}
async schemaIsAvailableInView(schemaName: string): Promise<boolean> {
try {
// Wait for the table to appear and contain at least one row
await this.page.waitForFunction(() => document.querySelectorAll('tr').length > 0, { timeout: 10000 });
const templateExists = await this.page.$$eval('td:nth-child(2)', (tds, schemaName) => {
return tds.some(td => (td as HTMLElement).innerText === schemaName);
}, schemaName);
return templateExists;
} catch (error) {
console.error("A problem while loading the list of templates.", error);
throw error;
}
}
async gotoViewSchemaPage(template: String) {
try {
const row = this.page.locator(`tr:has-text('${template}')`);
await row.locator('i.bi.bi-eye').click();
} catch (error) {
console.error("Failed to go to the View schemas page", error);
throw error;
}
}
async gotoAddTemplatePage() {
try {
await this.addTemplateButton.click();
} catch (error) {
console.error("Failed to add the template: ", error);
throw error;
}
}
async gotoDeleteAndConfirmInModal(template: String): Promise <boolean>{
try {
const row = this.page.locator(`tr:has-text('${template}')`);
const trashLocator = row.locator('i.bi.bi-trash');
// Check if the trash icon exists
if (await trashLocator.count() > 0) {
await trashLocator.click();
//Find the modal that corresponds to the specific template (see html)
const modal = this.page.locator(`div.modal:has-text("${template}")`)
.first();
// Ensure the modal is visible before trying to interact with it
await expect(modal).toBeVisible();
// Click the delete button within the modal
await modal.locator('.btn.btn-danger').click();
return true;
} else {
console.log(`The schema ${template} can not be deleted`)
return false;
}
} catch (error) {
console.error("A problem while trying to delete the template.", error);
throw error;
}
}
async gotoDeleteAndCancelInModal(template: String): Promise <boolean>{
try {
const row = this.page.locator(`tr:has-text('${template}')`);
const trashLocator = row.locator('i.bi.bi-trash');
// Check if the trash icon exists
if (await trashLocator.count() > 0) {
await trashLocator.click();
//Find the modal that corresponds to the specific template (see html)
const modal = this.page.locator(`div.modal:has-text("${template}")`)
.first();
// Ensure the modal is visible before trying to interact with it
await expect(modal).toBeVisible();
// Click the delete button within the modal
await modal.locator('.btn.btn-secondary').click();
return true;
} else {
console.log(`The schema ${template} can not be deleted`)
return false;
}
} catch (error) {
console.error("A problem while trying to delete the template.", error);
throw error;
}
}
}