Skip to content

Commit

Permalink
Implement logic to change language on Gitea to Norwegian
Browse files Browse the repository at this point in the history
  • Loading branch information
wrt95 committed Feb 6, 2024
1 parent b4bcd7d commit 9cf8251
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
3 changes: 3 additions & 0 deletions frontend/testing/playwright/enum/Language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum Language {
NORWEGIAN = 'Norsk',
}
40 changes: 25 additions & 15 deletions frontend/testing/playwright/pages/LoginPage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +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<string, string> = {
login: 'logg inn',
username: 'Brukernavn eller epost',
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.',
error_message: 'Ugyldig brukernavn eller passord.',
links: 'Links',
};

export class LoginPage extends BasePage {
Expand All @@ -32,27 +30,39 @@ export class LoginPage extends BasePage {
await this.page.getByRole('button', { name: loginPageTexts['login'] }).click();
}

public async writeUsername(username: string, isAfterLogout: boolean = false): Promise<void> {
const textKey = isAfterLogout ? 'username_after_logout' : 'username';
return await this.page.getByLabel(loginPageTexts[textKey]).fill(username);
public async writeUsername(username: string): Promise<void> {
return await this.page.getByLabel(loginPageTexts['username']).fill(username);
}

public async writePassword(password: string, isAfterLogout: boolean = false): Promise<void> {
const textKey = isAfterLogout ? 'password_after_logout' : 'password';
return await this.page.getByLabel(loginPageTexts[textKey]).fill(password);
public async writePassword(password: string): Promise<void> {
return await this.page.getByLabel(loginPageTexts['password']).fill(password);
}

public async clickLoginButton(isAfterLogout: boolean = false): Promise<void> {
const textKey = isAfterLogout ? 'login_after_logout' : 'login';
return await this.page.getByRole('button', { name: loginPageTexts[textKey] }).click();
public async clickLoginButton(): Promise<void> {
return await this.page.getByRole('button', { name: loginPageTexts['login'] }).click();
}

public async confirmSuccessfulLogin(): Promise<void> {
await this.page.waitForURL(this.getRoute('dashboard'));
}

public async checkThatErrorMessageIsVisible(): Promise<void> {
await this.page.getByText(loginPageTexts['error_message_after_logout']).isVisible();
await this.page.getByText(loginPageTexts['error_message']).isVisible();
}

public async getLanguage(): Promise<string> {
return await this.page
.getByRole('group', { name: loginPageTexts['links'] })
.getByRole('menu')
.innerText();
}

public async clickOnLanguageMenu(): Promise<void> {
await this.page.getByRole('group', { name: loginPageTexts['links'] }).getByRole('menu').click();
}

public async clickOnNorwegianLanguageOption(): Promise<void> {
await this.page.getByRole('menuitem', { name: Language.NORWEGIAN }).click();
}

public async addSessionToSharableStorage() {
Expand Down
5 changes: 2 additions & 3 deletions frontend/testing/playwright/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,20 @@ export default defineConfig<ExtendedTestOptions>({
// Add ALL other test names here to make sure that the log out test is the last test to be executed
dependencies: [
TestNames.SETUP,
TestNames.CREATE_APP_ONLY,
/*TestNames.CREATE_APP_ONLY,
TestNames.DATA_MODEL,
TestNames.DASHBOARD,
TestNames.MAIN_NAVIGATION_BETWEEN_SUB_APPS,
TestNames.GIT_SYNC,
TestNames.UI_EDITOR,
TestNames.SETTINGS_MODAL,
TestNames.SETTINGS_MODAL,*/
],
testDir: './tests/logout-and-invalid-login-only/',
testMatch: '*.spec.ts',
use: {
...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.
},
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,20 +28,27 @@ 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<void> => {
const loginPage = new LoginPage(page);
console.log('locale', locale);

await loginPage.goToAltinnLoginPage();
await loginPage.goToGiteaLoginPage();

// Gitea login page is in Norwegian locally, but english in dev and prod
const isEnglish: boolean = locale === 'en';
const lang = await loginPage.getLanguage();

await loginPage.writeUsername(process.env.PLAYWRIGHT_USER, isEnglish);
await loginPage.writePassword('123', isEnglish);
if (lang !== Language.NORWEGIAN) {
await loginPage.clickOnLanguageMenu();
await loginPage.clickOnNorwegianLanguageOption();

await loginPage.clickLoginButton(isEnglish);
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');

await loginPage.clickLoginButton();
await loginPage.checkThatErrorMessageIsVisible();
});

0 comments on commit 9cf8251

Please sign in to comment.