From 10340e50e418bb1136b9c4eee6a67d377def1b9f Mon Sep 17 00:00:00 2001 From: WilliamThorenfeldt <133344438+WilliamThorenfeldt@users.noreply.github.com> Date: Tue, 6 Feb 2024 20:33:35 +0100 Subject: [PATCH] Implementing temporary solution for solving logout tests in Playwright (#12261) * Implementing temporary solution for solving logout tests * removing comment * Implement logic to change language on Gitea to Norwegian * Implement logic to change language on Gitea to Norwegian * fixing enum --- frontend/testing/playwright/enum/Language.ts | 3 ++ .../testing/playwright/pages/LoginPage.ts | 20 ++++++++++++- .../tests/dashboard/dashboard.spec.ts | 28 ++++++++++--------- .../logout-and-invalid-login-only.spec.ts | 14 ++++++++++ 4 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 frontend/testing/playwright/enum/Language.ts diff --git a/frontend/testing/playwright/enum/Language.ts b/frontend/testing/playwright/enum/Language.ts new file mode 100644 index 00000000000..534640c0552 --- /dev/null +++ b/frontend/testing/playwright/enum/Language.ts @@ -0,0 +1,3 @@ +export enum Language { + Norwegian = 'Norsk', +} diff --git a/frontend/testing/playwright/pages/LoginPage.ts b/frontend/testing/playwright/pages/LoginPage.ts index 0cb3cc67181..c6884535e7d 100644 --- a/frontend/testing/playwright/pages/LoginPage.ts +++ b/frontend/testing/playwright/pages/LoginPage.ts @@ -1,11 +1,14 @@ import type { Page } from '@playwright/test'; import { BasePage } from '../helpers/BasePage'; +import { Language } from '../enum/Language'; // 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', password: 'Passord', + error_message: 'Ugyldig brukernavn eller passord.', + links: 'Links', }; export class LoginPage extends BasePage { @@ -44,7 +47,22 @@ 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']).isVisible(); + } + + public async getLanguage(): Promise { + return await this.page + .getByRole('group', { name: loginPageTexts['links'] }) + .getByRole('menu') + .innerText(); + } + + public async clickOnLanguageMenu(): Promise { + await this.page.getByRole('group', { name: loginPageTexts['links'] }).getByRole('menu').click(); + } + + public async clickOnNorwegianLanguageOption(): Promise { + await this.page.getByRole('menuitem', { name: Language.Norwegian }).click(); } public async addSessionToSharableStorage() { 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..596ff01e7a9 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 @@ -1,6 +1,8 @@ import { test } from '../../extenders/testExtend'; import { LoginPage } from '../../pages/LoginPage'; import { DashboardPage } from '../../pages/DashboardPage'; +import { expect } from '@playwright/test'; +import { Language } from '../../enum/Language'; // This line must be there to ensure that the tests do not run in parallell, and // that the before all call is being executed before we start the tests @@ -32,6 +34,18 @@ test('That it is not possible to login with invalid credentials', async ({ await loginPage.goToAltinnLoginPage(); await loginPage.goToGiteaLoginPage(); + const lang = await loginPage.getLanguage(); + + if (lang !== Language.Norwegian) { + await loginPage.clickOnLanguageMenu(); + await loginPage.clickOnNorwegianLanguageOption(); + + const langAfterchange = await loginPage.getLanguage(); + expect(langAfterchange).toBe(Language.Norwegian); + } else { + expect(lang).toBe(Language.Norwegian); + } + await loginPage.writeUsername(process.env.PLAYWRIGHT_USER); await loginPage.writePassword('123');