Skip to content

Commit f7455f1

Browse files
committed
feat: use shared authentication context in playwright
1 parent f2888c6 commit f7455f1

File tree

9 files changed

+63
-43
lines changed

9 files changed

+63
-43
lines changed

packages/e2e-tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules/
33
/playwright-report/
44
/blob-report/
55
/playwright/.cache/
6+
7+
playwright/.auth

packages/e2e-tests/playwright.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,21 @@ export default defineConfig({
3333
/* Configure projects for major browsers */
3434
projects: process.env.CI
3535
? [
36+
37+
{
38+
name: 'setup',
39+
testMatch: /.*\.setup\.ts/,
40+
},
3641
{
3742
name: 'chromium',
3843
use: { ...devices['Desktop Chrome'] },
44+
dependencies: ['setup'],
3945
},
4046

4147
{
4248
name: 'firefox',
4349
use: { ...devices['Desktop Firefox'] },
50+
dependencies: ['setup'],
4451
},
4552

4653
// FIXME: tests not working in webkit
@@ -70,14 +77,20 @@ export default defineConfig({
7077
]
7178
: [
7279
// NOTE: WebKit is only supported on Debian and Ubuntu in Playwright.
80+
{
81+
name: 'setup',
82+
testMatch: /.*\.setup\.ts/,
83+
},
7384
{
7485
name: 'chromium',
7586
use: { ...devices['Desktop Chrome'] },
87+
dependencies: ['setup'],
7688
},
7789

7890
{
7991
name: 'firefox',
8092
use: { ...devices['Desktop Firefox'] },
93+
dependencies: ['setup'],
8194
},
8295
],
8396

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test as setup } from '@playwright/test';
2+
import { login } from '#utils/auth';
3+
const authFile = 'playwright/.auth/user.json';
4+
5+
setup('authenticate', async ({ page }) => {
6+
if (process.env.PLAYWRIGHT_USER_NAME && process.env.PLAYWRIGHT_USER_PASSWORD) {
7+
await login(page, process.env.PLAYWRIGHT_USER_NAME, process.env.PLAYWRIGHT_USER_PASSWORD);
8+
}
9+
10+
// End of authentication steps.
11+
await page.context().storageState({ path: authFile });
12+
});

packages/e2e-tests/tests/example.spec.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/e2e-tests/tests/field_report/fieldReport.spec.ts renamed to packages/e2e-tests/tests/field-report/fieldReport.spec.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import { expect, test } from '@playwright/test';
2-
import { login } from '../../utils/auth';
3-
import { formatNumber } from '../../utils/common';
2+
import { formatNumber } from '#utils/common';
43
import fixtureData from './fieldReport.json';
54

6-
test.describe('test suite for Field report', async () => {
7-
test.beforeEach('login credentials', async ({ page }) => {
8-
await login(
9-
page,
10-
process.env.PLAYWRIGHT_APP_BASE_URL,
11-
process.env.PLAYWRIGHT_USER_NAME,
12-
process.env.PLAYWRIGHT_USER_PASSWORD,
13-
);
14-
});
5+
test.use({ storageState: 'playwright/.auth/user.json' });
156

16-
test('Field report for Event', async ({ page }) => {
7+
test.describe('Field Report', () => {
8+
test('should create a new event type field report', async ({ page }) => {
179
const {
1810
formName,
1911
country,
@@ -84,6 +76,7 @@ test.describe('test suite for Field report', async () => {
8476
visibiltyOptTwo,
8577
} = fixtureData;
8678

79+
await page.goto('/');
8780
await page.getByRole('button', { name: 'Create a Report' }).click();
8881
await page.getByRole('link', { name: 'New Field Report' }).click();
8982
await expect(page.locator('h1')).toContainText(formName);

packages/e2e-tests/tests/loginLogout.spec.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
import { expect, test } from '@playwright/test';
2-
import { login } from '../utils/auth';
2+
import { login } from '#utils/auth';
33

4-
test('Login', async ({ page }) => {
4+
test('should login', async ({ page }) => {
55
await login(
66
page,
7-
process.env.PLAYWRIGHT_APP_BASE_URL,
87
process.env.PLAYWRIGHT_USER_NAME,
98
process.env.PLAYWRIGHT_USER_PASSWORD,
109
);
11-
await expect(page.getByRole('navigation')).toContainText(
12-
process.env.PLAYWRIGHT_USER_NAME,
13-
);
14-
await page
15-
.getByRole('button', { name: process.env.PLAYWRIGHT_USER_NAME })
16-
.click();
10+
await page.waitForURL('/');
11+
await expect(page.getByRole('button', { name: 'Create a Report' })).toBeVisible();
1712
});
1813

19-
test('logout', async ({ page }) => {
14+
test('should logout', async ({ page }) => {
2015
await login(
2116
page,
22-
process.env.PLAYWRIGHT_APP_BASE_URL,
2317
process.env.PLAYWRIGHT_USER_NAME,
2418
process.env.PLAYWRIGHT_USER_PASSWORD,
2519
);
26-
await expect(page.getByRole('navigation')).toContainText(
27-
process.env.PLAYWRIGHT_USER_NAME,
28-
);
20+
await page.waitForURL('/');
2921
await page
3022
.getByRole('button', { name: process.env.PLAYWRIGHT_USER_NAME })
3123
.click();

packages/e2e-tests/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"#utils/*": ["./utils/*"],
6+
},
7+
"esModuleInterop": true,
8+
"resolveJsonModule": true,
9+
},
10+
"include": ["tests"]
11+
}

packages/e2e-tests/utils/auth.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
export async function login(page, baseurl, username, password) {
2-
await page.goto(baseurl);
3-
await page.getByRole('link', { name: 'Login' }).click();
1+
import { type Page, expect } from '@playwright/test';
2+
export async function login(page: Page, username: string, password: string) {
3+
await page.goto('/login');
4+
5+
//FIXME: page.fill is discouraged. We should use locator based fill.
6+
// @ifrc/go-ui should be updated to support locators
47
await page.fill('input[name="username"]', username);
58
await page.fill('input[name="password"]', password);
9+
610
await page.getByRole('button', { name: 'Login' }).click();
7-
}
11+
// Wait until the page receives the cookies.
12+
// Sometimes login flow sets cookies in the process of several redirects.
13+
// Wait for the final URL to ensure that the cookies are actually set.
14+
await page.waitForURL('/');
15+
// Alternatively, you can wait until the page reaches a state where all cookies are set.
16+
await expect(page.getByRole('button', { name: 'Create a Report' })).toBeVisible();
17+
}

0 commit comments

Comments
 (0)