Skip to content

Commit

Permalink
(test) O3-2823: Reuse signed in state in E2E tests (#921)
Browse files Browse the repository at this point in the history
* O3-2823: Reuse signed in state in esm-core E2E tests excluding login test

* Add step to access the home page
  • Loading branch information
kdaud authored Feb 13, 2024
1 parent b05ba1a commit 97b0b50
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
32 changes: 32 additions & 0 deletions e2e/core/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { request } from '@playwright/test';
import * as dotenv from 'dotenv';

dotenv.config();

/**
* This configuration is to reuse the signed-in state in the tests
* by log in only once using the API and then skip the log in step for all the tests.
*
* https://playwright.dev/docs/auth#reuse-signed-in-state
*/

async function globalSetup() {
const requestContext = await request.newContext();
const token = Buffer.from(`${process.env.E2E_USER_ADMIN_USERNAME}:${process.env.E2E_USER_ADMIN_PASSWORD}`).toString(
'base64',
);
await requestContext.post(`${process.env.E2E_BASE_URL}/ws/rest/v1/session`, {
data: {
sessionLocation: process.env.E2E_LOGIN_DEFAULT_LOCATION_UUID,
locale: 'en',
},
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${token}`,
},
});
await requestContext.storageState({ path: 'e2e/storageState.json' });
await requestContext.dispose();
}

export default globalSetup;
9 changes: 9 additions & 0 deletions e2e/pages/home-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { type Page } from '@playwright/test';

export class HomePage {
constructor(readonly page: Page) {}

async goto() {
await this.page.goto('home');
}
}
1 change: 1 addition & 0 deletions e2e/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './login-page';
export * from './home-page';
9 changes: 0 additions & 9 deletions e2e/pages/login-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,4 @@ export class LoginPage {
async goto() {
await this.page.goto(`${process.env.E2E_BASE_URL}/spa/login`);
}

async enterLoginCredentials() {
await this.page.getByLabel(/username/i).fill(`${process.env.E2E_USER_ADMIN_USERNAME}`);
await this.page.getByText(/continue/i).click();
await this.page.getByLabel(/password/i).fill(`${process.env.E2E_USER_ADMIN_PASSWORD}`);
await this.page.getByRole('button', { name: /log in/i }).click();
await this.page.getByText(/inpatient ward/i).click();
await this.page.getByRole('button', { name: /confirm/i }).click();
}
}
2 changes: 2 additions & 0 deletions e2e/specs/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { test } from '../core';
import { expect } from '@playwright/test';
import { LoginPage } from '../pages';

test.use({ storageState: { cookies: [], origins: [] } });

test('Login as Admin user', async ({ page }) => {
const loginPage = new LoginPage(page);

Expand Down
18 changes: 5 additions & 13 deletions e2e/specs/logout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { test } from '../core';
import { expect } from '@playwright/test';
import { LoginPage } from '../pages';
import { HomePage } from '../pages';

test('Logout as Admin user', async ({ page }) => {
const loginPage = new LoginPage(page);
const homePage = new HomePage(page);

await test.step('When I go to Login page', async () => {
await loginPage.goto();
await test.step('When I visit the home page', async () => {
await homePage.goto();
});

await test.step('And I enter the login credentials', async () => {
await loginPage.enterLoginCredentials();
});

await test.step('Then I should be on the Home page', async () => {
await expect(page).toHaveURL(`${process.env.E2E_BASE_URL}/spa/home`);
});

await test.step('When I click the `User` button', async () => {
await test.step('And I click the `User` button', async () => {
await page.getByRole('button', { name: /user/i }).click();
});

Expand Down
2 changes: 2 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ const config: PlaywrightTestConfig = {
forbidOnly: !!process.env.CI,
retries: 0,
reporter: process.env.CI ? [['junit', { outputFile: 'results.xml' }], ['html']] : [['html']],
globalSetup: require.resolve('./e2e/core/global-setup'),
use: {
baseURL: `${process.env.E2E_BASE_URL}/spa/`,
trace: 'retain-on-failure',
storageState: 'e2e/storageState.json',
video: 'retain-on-failure',
},
projects: [
Expand Down

0 comments on commit 97b0b50

Please sign in to comment.