Fix login flaky tests

This commit is contained in:
mildred 2024-03-08 23:38:18 +01:00
parent 31915928f5
commit e0771c1f3f
8 changed files with 63 additions and 63 deletions

View File

@ -1,9 +1,10 @@
/*Login*/ /*Login*/
export const ENCRYPTION_KEY = "1234"
export const ADMIN_EMAIL = "idhub_admin@pangea.org" export const ADMIN_EMAIL = "idhub_admin@pangea.org"
export const ADMIN_K = "1234" export const ADMIN_K = "1234"
export const KO_ADMIN_K = "876" export const KO_ADMIN_K = "876"
export const URL_IDHUB = "https://idhub1-autotest.demo.pangea.org" export const URL_IDHUB = "https://idhub1-autotest.demo.pangea.org"
//export const URL_IDHUB = "https://idhub-nightly.demo.pangea.org" //export const URL_IDHUB = "https://idhub1-nightly.demo.pangea.org"
export const USER1_EMAIL = "user1@example.org" export const USER1_EMAIL = "user1@example.org"
export const USER2_EMAIL = "user2@example.org" export const USER2_EMAIL = "user2@example.org"

View File

@ -131,22 +131,18 @@ export class AddMembershipPage {
async alertUserCreationMessageIsValid(): Promise<boolean> { async alertUserCreationMessageIsValid(): Promise<boolean> {
await this.page.waitForSelector('.alert.alert-success.alert-dismissible'); try {
const element = await this.page.$('.alert.alert-success.alert-dismissible'); await this.page.locator('.alert.alert-success.alert-dismissible').waitFor({ state: 'visible', timeout: 5000 });
// If the success message is found and visible, retrieve its text content
const message = await this.page.locator('.alert.alert-success.alert-dismissible').textContent();
if (element !== null) { // Compare the retrieved text with the expected error message
const isVisible = await element.isVisible(); return message?.trim() === ALERT_USER_CREATED_SUCESSFULLY_MESSAGE;
if (isVisible) {
const text = await element.innerText();
console.log(text);
if (text === ALERT_USER_CREATED_SUCESSFULLY_MESSAGE) {
return true;
}
}
}
return false; } catch (error) {
console.error('Failed to check success message:', error);
throw error;
}
} }
} }

View File

@ -102,20 +102,18 @@ export class ImportDataPage {
} }
async alertFileImportedUnsuccessfully(expectedMessage: string): Promise<boolean> { async alertFileImportedUnsuccessfully(expectedMessage: string): Promise<boolean> {
try { try {
const element = this.page.locator('.alert.alert-danger.alert-dismissible'); await this.page.locator('.alert.alert-danger.alert-dismissible').waitFor({ state: 'visible', timeout: 5000 });
if (element) {
const isVisible = await element.isVisible(); // If the success message is found and visible, retrieve its text content
if (isVisible) { const message = await this.page.locator('.alert.alert-danger.alert-dismissible').textContent();
const text = await element.innerText();
console.log(text); // Compare the retrieved text with the expected error message
expect(text).toBe(expectedMessage); return message?.trim() === expectedMessage;
return true;
}
}
return false;
} catch (error) { } catch (error) {
console.error("Failed to check for unsuccessful file import alert:", error); console.error('Failed to check for unsuccessful file import alert:', error);
throw error; throw error;
} }
} }

View File

@ -134,24 +134,17 @@ export class ViewImportedDataPage {
async alertFileImportedSuccessfully(): Promise<boolean> { async alertFileImportedSuccessfully(): Promise<boolean> {
try { try {
await this.page.waitForSelector('.alert.alert-success.alert-dismissible'); await this.page.locator('.alert.alert-success.alert-dismissible').waitFor({ state: 'visible', timeout: 5000 });
const element = await this.page.$('.alert.alert-success.alert-dismissible');
if (element !== null) { // If the success message is found and visible, retrieve its text content
const isVisible = await element.isVisible(); const message = await this.page.locator('.alert.alert-success.alert-dismissible').textContent();
if (isVisible) {
const text = await element.innerText(); // Compare the retrieved text with the expected error message
console.log(text); return message?.trim() === ALERT_FILE_IMPORTED_SUCCESSFULLY;
expect(text).toBe(ALERT_FILE_IMPORTED_SUCCESSFULLY);
return true;
}
}
return false;
} catch (error) { } catch (error) {
console.error("Failed to check for successful file import alert:", error); console.error('Failed to check for successful file import alert:', error);
throw error; throw error;
} }
} }
} }

View File

@ -1,4 +1,4 @@
import { type Page, type Locator } from '@playwright/test'; import { type Page, type Locator, expect } from '@playwright/test';
import { ERROR_INCORRECT_EMAIL_PASSWORD } from '../constants/constants'; import { ERROR_INCORRECT_EMAIL_PASSWORD } from '../constants/constants';
export class LogInPage { export class LogInPage {
@ -41,18 +41,25 @@ export class LogInPage {
} }
} }
async errorMessageIsValid(): Promise<boolean> { async errorMessageIsValid() {
try { try {
const isVisible = await this.page.locator('.well.well-small.text-error').isVisible();
if (!isVisible) { try {
return false; await this.page.locator('.well.well-small.text-error').waitFor({ state: 'visible', timeout: 5000 });
} catch (error) {
console.error('Error message not found:', error);
return false; // Return false if the error message is not found
} }
// If the error message is found and visible, retrieve its text content
const errorText = await this.page.locator('.well.well-small.text-error').textContent(); const errorText = await this.page.locator('.well.well-small.text-error').textContent();
return errorText === ERROR_INCORRECT_EMAIL_PASSWORD;
// Compare the retrieved text with the expected error message
return errorText?.trim() === ERROR_INCORRECT_EMAIL_PASSWORD;
} catch (error) { } catch (error) {
console.error('Failed to check error message:', error); console.error('Failed to check error message:', error);
throw error; throw error;
} }
} }
} }

View File

@ -6,7 +6,7 @@ import { CREDENTIAL_TYPE_DATASTORE_UNDEFINED, CREDENTIAL_TYPES_DATA_STORE } from
import { FinancialVulnerabilityCredential_fields } from './interfaces/credential_interfaces' import { FinancialVulnerabilityCredential_fields } from './interfaces/credential_interfaces'
import { CREDENTIAL_TYPES_FIELDS_LIST, CREDENTIAL_TYPES_REQUIRED_FIELDS_LIST } from './constants/credential_fields' import { CREDENTIAL_TYPES_FIELDS_LIST, CREDENTIAL_TYPES_REQUIRED_FIELDS_LIST } from './constants/credential_fields'
import { LogInPage } from './page-objects/COMM_LoginPage' import { LogInPage } from './page-objects/COMM_LoginPage'
import { ADMIN_EMAIL, ADMIN_K, USER_K } from './constants/env_constants' import { ADMIN_EMAIL, ADMIN_K, ENCRYPTION_KEY, USER_K } from './constants/env_constants'
import { LeftMenuAdminPage } from './page-objects/AD_LeftMenuAdminPage' import { LeftMenuAdminPage } from './page-objects/AD_LeftMenuAdminPage'
import { LeftMenuUserPage } from './page-objects/US_LeftMenuUserPage' import { LeftMenuUserPage } from './page-objects/US_LeftMenuUserPage'
import { TemplatesPage } from './page-objects/AD_TemplatesPage' import { TemplatesPage } from './page-objects/AD_TemplatesPage'
@ -16,31 +16,37 @@ import { UserPersonalInformationPage } from './page-objects/AD_UserPersonalInfor
import { ImportTemplatePage } from './page-objects/AD_ImportTemplatePage' import { ImportTemplatePage } from './page-objects/AD_ImportTemplatePage'
import { ViewImportedDataPage } from './page-objects/AD_ViewImportedDataPage' import { ViewImportedDataPage } from './page-objects/AD_ViewImportedDataPage'
import { User } from './interfaces/User' import { User } from './interfaces/User'
import { EncryptionKeyPage } from './page-objects/AD_EncryptionKeyPage'
import { DataProtectionPage } from './page-objects/COMM_DataProtectionPage'
export async function loginAsAdmin(page: Page, url: string) { export async function loginAsAdmin(page: Page, url: string) {
try { try {
const loginPage = new LogInPage(page); const loginPage = new LogInPage(page);
const encryptionKeyPage = new EncryptionKeyPage(page);
const dataProtectionPage = new DataProtectionPage(page);
await loginPage.visit(url); await loginPage.visit(url);
await loginPage.login(ADMIN_EMAIL, ADMIN_K); await loginPage.login(ADMIN_EMAIL, ADMIN_K);
let currentTitle = await page.title(); let currentTitle = await page.title();
console.log("current title: ", currentTitle);
if (currentTitle === 'Encryption Key IdHub') { if (currentTitle === 'Encryption Key IdHub') {
//code to set Encription Key //code to set Encription Key
page.getByPlaceholder('Key for encrypt the secrets').click(); await encryptionKeyPage.enterKey(ENCRYPTION_KEY);
await page.getByPlaceholder('Key for encrypt the secrets').fill('1234'); await encryptionKeyPage.clickSaveButton();
await page.getByRole('button', { name: 'Save' }).click();
await expect(page).toHaveTitle('Data protection IdHub'); await expect(page).toHaveTitle('Data protection IdHub');
currentTitle = await page.title(); currentTitle = await page.title();
} }
if (currentTitle === 'Data protection IdHub') { if (currentTitle === 'Data protection IdHub') {
// Code to accept terms and conditions // Code to accept terms and conditions
await page.click('#id_accept_privacy'); await dataProtectionPage.checkAcceptPrivacyCheckBox();
await page.click('#id_accept_legal'); await dataProtectionPage.checkAcceptCookiesCheckBox();
await page.click('#id_accept_cookies'); await dataProtectionPage.checkAcceptLegalCheckBox();
await page.click('a[type="button"]'); await dataProtectionPage.clickConfirmButton();
} }
await expect(page).toHaveTitle('Dashboard IdHub'); await expect(page).toHaveTitle('Dashboard IdHub');

View File

@ -6,13 +6,12 @@ import { ADMIN_EMAIL, KO_ADMIN_K, KO_USER_K, URL_IDHUB, URL_PASS_RESET, USER1_EM
test.describe('Admin login functionality', () => { test.describe('Admin login functionality', () => {
test('Successful login as admin', async ({ page }) => { test('Successful login as admin', async ({ page }) => {
await loginAsAdmin(page, URL_IDHUB); await loginAsAdmin(page, URL_IDHUB);
await expect.soft(page).toHaveTitle('Dashboard IdHub');
}) })
test('Unsuccessful login as admin', async ({ page }) => { test('Unsuccessful login as admin', async ({ page }) => {
const loginPage = new LogInPage(page) const loginPage = new LogInPage(page)
await loginPage.visit(URL_IDHUB); await loginPage.visit(URL_IDHUB);
await loginPage.login(ADMIN_EMAIL, KO_ADMIN_K) await loginPage.login(ADMIN_EMAIL, KO_ADMIN_K)
expect.soft(loginPage.errorMessageIsValid()).toBeTruthy(); expect(loginPage.errorMessageIsValid()).toBeTruthy();
}) })
test('Navigate to Forgot password page from login page', async ({ page }) => { test('Navigate to Forgot password page from login page', async ({ page }) => {
const loginPage = new LogInPage(page) const loginPage = new LogInPage(page)