From e5f7caab0ef74a5f43100a27a00d0513f366f91c Mon Sep 17 00:00:00 2001 From: WilliamThorenfeldt Date: Tue, 6 Feb 2024 17:29:29 +0100 Subject: [PATCH] Implementing temporary solution for solving logout tests --- .../testing/playwright/pages/LoginPage.ts | 24 ++++++++++------ .../testing/playwright/playwright.config.ts | 1 + .../tests/dashboard/dashboard.spec.ts | 28 ++++++++++--------- .../logout-and-invalid-login-only.spec.ts | 11 ++++++-- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/frontend/testing/playwright/pages/LoginPage.ts b/frontend/testing/playwright/pages/LoginPage.ts index 0cb3cc67181..99d85165dc3 100644 --- a/frontend/testing/playwright/pages/LoginPage.ts +++ b/frontend/testing/playwright/pages/LoginPage.ts @@ -4,8 +4,13 @@ import { BasePage } from '../helpers/BasePage'; // Since this page is a Razor page, it's not using the nb/en.json files, which are used in the frontend. const loginPageTexts: Record = { login: 'logg inn', - username: 'Brukernavn eller epost', + username: 'Brukernavn eller epost', // SHOULD THIS BE "Username or Email Address" password: 'Passord', + // After loging out, the language on the page changes from Norwegian to English + login_after_logout: 'Sign in', + username_after_logout: 'Username or Email Address', + password_after_logout: 'Password', + error_message_after_logout: 'Username or password is incorrect.', }; export class LoginPage extends BasePage { @@ -27,16 +32,19 @@ export class LoginPage extends BasePage { await this.page.getByRole('button', { name: loginPageTexts['login'] }).click(); } - public async writeUsername(username: string): Promise { - return await this.page.getByLabel(loginPageTexts['username']).fill(username); + public async writeUsername(username: string, isAfterLogout: boolean = false): Promise { + const textKey = isAfterLogout ? 'username_after_logout' : 'username'; + return await this.page.getByLabel(loginPageTexts[textKey]).fill(username); } - public async writePassword(password: string): Promise { - return await this.page.getByLabel(loginPageTexts['password']).fill(password); + public async writePassword(password: string, isAfterLogout: boolean = false): Promise { + const textKey = isAfterLogout ? 'password_after_logout' : 'password'; + return await this.page.getByLabel(loginPageTexts[textKey]).fill(password); } - public async clickLoginButton(): Promise { - return await this.page.getByRole('button', { name: loginPageTexts['login'] }).click(); + public async clickLoginButton(isAfterLogout: boolean = false): Promise { + const textKey = isAfterLogout ? 'login_after_logout' : 'login'; + return await this.page.getByRole('button', { name: loginPageTexts[textKey] }).click(); } public async confirmSuccessfulLogin(): Promise { @@ -44,7 +52,7 @@ export class LoginPage extends BasePage { } public async checkThatErrorMessageIsVisible(): Promise { - await this.page.getByText(/ugyldig brukernavn eller passord./i).isVisible(); + await this.page.getByText(loginPageTexts['error_message_after_logout']).isVisible(); } public async addSessionToSharableStorage() { diff --git a/frontend/testing/playwright/playwright.config.ts b/frontend/testing/playwright/playwright.config.ts index 6110c5bce62..35c27753771 100644 --- a/frontend/testing/playwright/playwright.config.ts +++ b/frontend/testing/playwright/playwright.config.ts @@ -124,6 +124,7 @@ export default defineConfig({ ...devices['Desktop Chrome'], storageState: '.playwright/auth/user.json', headless: true, + locale: process.env.CI ? 'en' : 'no', // Our solution runs Gitea page in Norwegian locally and English in dev and prod. }, }, ], diff --git a/frontend/testing/playwright/tests/dashboard/dashboard.spec.ts b/frontend/testing/playwright/tests/dashboard/dashboard.spec.ts index 4b4b831b247..c5d20a80b34 100644 --- a/frontend/testing/playwright/tests/dashboard/dashboard.spec.ts +++ b/frontend/testing/playwright/tests/dashboard/dashboard.spec.ts @@ -11,16 +11,8 @@ const getExtraAppName = (appName: string): string => `extra-app-${appName}`; // Before the tests starts, we need to create the dashboard app test.beforeAll(async ({ testAppName, request, storageState }) => { - // Create 2 apps - const firstApp = await createApp(testAppName, request, storageState as StorageState); - const secondApp = await createApp( - getExtraAppName(testAppName), - request, - storageState as StorageState, - ); - - expect(firstApp.ok()).toBeTruthy(); - expect(secondApp.ok()).toBeTruthy(); + const response = await createApp(testAppName, request, storageState as StorageState); + expect(response.ok()).toBeTruthy(); }); test.afterAll(async ({ request, testAppName }) => { @@ -84,14 +76,24 @@ test('It is possible to change context and view only Testdepartementet apps', as await dashboardPage.checkThatTTDApplicationsHeaderIsVisible(); }); -test('It is possible to search an app by name', async ({ page, testAppName }) => { +test('It is possible to search an app by name', async ({ + page, + testAppName, + request, + storageState, +}) => { const dashboardPage = await setupAndVerifyDashboardPage(page, testAppName); - const testAppName2 = `${testAppName}2`; + const testAppName2 = getExtraAppName(testAppName); + + // Need to wait a bit to make sure that Gitea does not crash + dashboardPage.waitForXAmountOfMilliseconds(3000); + const response = await createApp(testAppName2, request, storageState as StorageState); + expect(response.ok()).toBeTruthy(); await dashboardPage.checkThatAppIsVisible(testAppName); await dashboardPage.checkThatAppIsVisible(testAppName2); - await dashboardPage.typeInSearchField('2'); + await dashboardPage.typeInSearchField('extra'); await dashboardPage.checkThatAppIsHidden(testAppName); await dashboardPage.checkThatAppIsVisible(testAppName2); }); diff --git a/frontend/testing/playwright/tests/logout-and-invalid-login-only/logout-and-invalid-login-only.spec.ts b/frontend/testing/playwright/tests/logout-and-invalid-login-only/logout-and-invalid-login-only.spec.ts index 4d37fdccb87..a2d071d1d74 100644 --- a/frontend/testing/playwright/tests/logout-and-invalid-login-only/logout-and-invalid-login-only.spec.ts +++ b/frontend/testing/playwright/tests/logout-and-invalid-login-only/logout-and-invalid-login-only.spec.ts @@ -26,15 +26,20 @@ test('That it is possible to login with valid user credentials, and then log out test('That it is not possible to login with invalid credentials', async ({ page, + locale, }): Promise => { const loginPage = new LoginPage(page); + console.log('locale', locale); await loginPage.goToAltinnLoginPage(); await loginPage.goToGiteaLoginPage(); - await loginPage.writeUsername(process.env.PLAYWRIGHT_USER); - await loginPage.writePassword('123'); + // Gitea login page is in Norwegian locally, but english in dev and prod + const isEnglish: boolean = locale === 'en'; - await loginPage.clickLoginButton(); + await loginPage.writeUsername(process.env.PLAYWRIGHT_USER, isEnglish); + await loginPage.writePassword('123', isEnglish); + + await loginPage.clickLoginButton(isEnglish); await loginPage.checkThatErrorMessageIsVisible(); });