Skip to content

Commit

Permalink
Implementing temporary solution for solving logout tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wrt95 committed Feb 6, 2024
1 parent 0009090 commit e5f7caa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
24 changes: 16 additions & 8 deletions frontend/testing/playwright/pages/LoginPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
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 {
Expand All @@ -27,24 +32,27 @@ export class LoginPage extends BasePage {
await this.page.getByRole('button', { name: loginPageTexts['login'] }).click();
}

public async writeUsername(username: string): Promise<void> {
return await this.page.getByLabel(loginPageTexts['username']).fill(username);
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 writePassword(password: string): Promise<void> {
return await this.page.getByLabel(loginPageTexts['password']).fill(password);
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 clickLoginButton(): Promise<void> {
return await this.page.getByRole('button', { name: loginPageTexts['login'] }).click();
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 confirmSuccessfulLogin(): Promise<void> {
await this.page.waitForURL(this.getRoute('dashboard'));
}

public async checkThatErrorMessageIsVisible(): Promise<void> {
await this.page.getByText(/ugyldig brukernavn eller passord./i).isVisible();
await this.page.getByText(loginPageTexts['error_message_after_logout']).isVisible();
}

public async addSessionToSharableStorage() {
Expand Down
1 change: 1 addition & 0 deletions frontend/testing/playwright/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export default defineConfig<ExtendedTestOptions>({
...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
28 changes: 15 additions & 13 deletions frontend/testing/playwright/tests/dashboard/dashboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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();
});

0 comments on commit e5f7caa

Please sign in to comment.