Skip to content

Commit 1360783

Browse files
authored
(test) Create fixtures for automatic setup and teardown in e2e tests (#1640)
1 parent 3479393 commit 1360783

7 files changed

+38
-77
lines changed

e2e/core/test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { type APIRequestContext, type Page, test as base } from '@playwright/test';
22
import { api } from '../fixtures';
3+
import { type Patient } from '../types';
4+
import { generateRandomPatient, deletePatient } from '../commands';
35

46
// This file sets up our custom test harness using the custom fixtures.
57
// See https://playwright.dev/docs/test-fixtures#creating-a-fixture for details.
@@ -9,6 +11,7 @@ import { api } from '../fixtures';
911

1012
export interface CustomTestFixtures {
1113
loginAsAdmin: Page;
14+
patient: Patient;
1215
}
1316

1417
export interface CustomWorkerFixtures {
@@ -17,4 +20,16 @@ export interface CustomWorkerFixtures {
1720

1821
export const test = base.extend<CustomTestFixtures, CustomWorkerFixtures>({
1922
api: [api, { scope: 'worker' }],
23+
patient: [
24+
async ({ api }, use) => {
25+
const patient = await generateRandomPatient(api);
26+
await use(patient);
27+
try {
28+
if (patient) await deletePatient(api, patient.uuid);
29+
} catch (e) {
30+
console.warn('Failed to delete patient:', e);
31+
}
32+
},
33+
{ scope: 'test', auto: true },
34+
],
2035
});

e2e/specs/active-visits.spec.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,22 @@ import { expect } from '@playwright/test';
22
import type { Visit } from '@openmrs/esm-framework';
33
import { test } from '../core';
44

5-
import {
6-
createEncounter,
7-
deleteEncounter,
8-
deletePatient,
9-
endVisit,
10-
generateRandomPatient,
11-
getProvider,
12-
startVisit,
13-
} from '../commands';
5+
import { createEncounter, deleteEncounter, endVisit, getProvider, startVisit } from '../commands';
146
import { HomePage } from '../pages';
157
import { type Encounter, type Patient, type Provider } from '../types';
168

17-
let patient: Patient;
189
let visit: Visit;
1910
let encounter: Encounter;
2011
let provider: Provider;
2112
const encounterNote = 'This is a test note';
2213

23-
test.beforeEach(async ({ api }) => {
24-
patient = await generateRandomPatient(api);
14+
test.beforeEach(async ({ api, patient }) => {
2515
visit = await startVisit(api, patient.uuid);
2616
provider = await getProvider(api);
2717
encounter = await createEncounter(api, patient.uuid, provider.uuid, encounterNote);
2818
});
2919

30-
test('View active visits', async ({ page }) => {
20+
test('View active visits', async ({ page, patient }) => {
3121
const homePage = new HomePage(page);
3222
const openmrsIdentifier = patient.identifiers[0].display.split('=')[1].trim();
3323
const firstName = patient.person.display.split(' ')[0];
@@ -55,8 +45,7 @@ test('View active visits', async ({ page }) => {
5545
});
5646
});
5747

58-
test.afterEach(async ({ api }) => {
48+
test.afterEach(async ({ api, patient }) => {
5949
await endVisit(api, patient.uuid);
6050
await deleteEncounter(api, encounter.uuid);
61-
await deletePatient(api, patient.uuid);
6251
});

e2e/specs/appointments.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import { expect } from '@playwright/test';
22
import { type Visit } from '@openmrs/esm-framework';
3-
import { generateRandomPatient, deletePatient, startVisit, endVisit } from '../commands';
4-
import { type Patient } from '../types';
3+
import { startVisit, endVisit } from '../commands';
54
import { test } from '../core';
65
import { AppointmentsPage } from '../pages';
76
import dayjs from 'dayjs';
87

9-
let patient: Patient;
108
let visit: Visit;
119

12-
test.beforeEach(async ({ api }) => {
13-
patient = await generateRandomPatient(api);
10+
test.beforeEach(async ({ api, patient }) => {
1411
visit = await startVisit(api, patient.uuid);
1512
});
1613

17-
test('Add, edit and cancel an appointment', async ({ page, api }) => {
14+
test('Add, edit and cancel an appointment', async ({ page, patient }) => {
1815
const appointmentsPage = new AppointmentsPage(page);
1916

2017
await test.step('When I go to the Appointments page in the patient chart', async () => {
@@ -139,5 +136,4 @@ test('Add, edit and cancel an appointment', async ({ page, api }) => {
139136

140137
test.afterEach(async ({ api }) => {
141138
await endVisit(api, visit.uuid);
142-
await deletePatient(api, patient.uuid);
143139
});

e2e/specs/edit-patient.spec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import dayjs from 'dayjs';
22
import { expect } from '@playwright/test';
33
import { test } from '../core';
4-
import { deletePatient, generateRandomPatient, getPatient } from '../commands';
4+
import { getPatient } from '../commands';
55
import { RegistrationAndEditPage } from '../pages';
6-
import { type Patient, type PatientRegistrationFormValues } from '../types';
7-
8-
let patient: Patient;
9-
10-
test.beforeEach(async ({ api }) => {
11-
patient = await generateRandomPatient(api);
12-
});
6+
import { type PatientRegistrationFormValues } from '../types';
137

148
const formValues: PatientRegistrationFormValues = {
159
givenName: `Johnny`,
@@ -27,7 +21,7 @@ const formValues: PatientRegistrationFormValues = {
2721
email: 'johnnyronny@example.com',
2822
};
2923

30-
test('Edit a patient', async ({ page, api }) => {
24+
test('Edit a patient', async ({ page, api, patient }) => {
3125
test.setTimeout(5 * 60 * 1000);
3226
const patientEditPage = new RegistrationAndEditPage(page);
3327

@@ -101,7 +95,3 @@ test('Edit a patient', async ({ page, api }) => {
10195
expect(person.attributes[0].display).toBe(formValues.phone);
10296
});
10397
});
104-
105-
test.afterEach(async ({ api }) => {
106-
await deletePatient(api, patient.uuid);
107-
});

e2e/specs/patient-list.spec.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22
import { test } from '../core';
33
import { PatientListsPage } from '../pages';
44
import { expect } from '@playwright/test';
5-
import {
6-
addPatientToCohort,
7-
deleteCohort,
8-
deletePatient,
9-
generateRandomCohort,
10-
generateRandomPatient,
11-
removePatientFromCohort,
12-
} from '../commands';
5+
import { addPatientToCohort, deleteCohort, generateRandomCohort, removePatientFromCohort } from '../commands';
136
import { type Cohort, type CohortMember, type Patient } from '../types';
147

158
let cohortMember: CohortMember;
169
let cohortUuid: string;
1710
let cohort: Cohort;
18-
let patient: Patient;
1911

2012
test.beforeEach(async ({ api }) => {
21-
patient = await generateRandomPatient(api);
2213
cohort = await generateRandomCohort(api);
2314
});
2415

@@ -67,7 +58,7 @@ test.skip('Create and edit a patient list', async ({ page }) => {
6758
});
6859
});
6960

70-
test.skip('Manage patients in a list', async ({ api, page }) => {
61+
test.skip('Manage patients in a list', async ({ api, page, patient }) => {
7162
const patientListPage = new PatientListsPage(page);
7263

7364
await test.step("When I visit a specific patient list's page", async () => {
@@ -94,6 +85,5 @@ test.afterEach(async ({ api }) => {
9485
await removePatientFromCohort(api, cohortMember.uuid);
9586
}
9687
await deleteCohort(api, cohortUuid);
97-
await deletePatient(api, patient.uuid);
9888
await deleteCohort(api, cohort.uuid);
9989
});

e2e/specs/patient-search.spec.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { expect } from '@playwright/test';
22
import { test } from '../core';
33
import { HomePage } from '../pages';
4-
import { generateRandomPatient, deletePatient } from '../commands';
5-
import { type Patient } from '../types';
64

7-
let patient: Patient;
8-
9-
test.beforeEach(async ({ api }) => {
10-
patient = await generateRandomPatient(api);
11-
});
12-
13-
test('Search patient by patient identifier', async ({ page, api }) => {
5+
test('Search patient by patient identifier', async ({ page, patient }) => {
146
// extract details from the created patient
157
const openmrsIdentifier = patient.identifiers[0].display.split('=')[1].trim();
168
const firstName = patient.person.display.split(' ')[0];
@@ -43,7 +35,7 @@ test('Search patient by patient identifier', async ({ page, api }) => {
4335
});
4436
});
4537

46-
test('Search patient by full name', async ({ page, api }) => {
38+
test('Search patient by full name', async ({ page, patient }) => {
4739
// extract details from the created patient
4840
const openmrsIdentifier = patient.identifiers[0].display.split('=')[1].trim();
4941
const firstName = patient.person.display.split(' ')[0];
@@ -84,7 +76,3 @@ test('Search patient by full name', async ({ page, api }) => {
8476
await expect(page).toHaveURL(`${process.env.E2E_BASE_URL}/spa/home`);
8577
});
8678
});
87-
88-
test.afterEach(async ({ api }) => {
89-
await deletePatient(api, patient.uuid);
90-
});

e2e/specs/return-to-patient-list.spec.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,18 @@
22
import { test } from '../core';
33
import { PatientListsPage } from '../pages';
44
import { expect } from '@playwright/test';
5-
import {
6-
addPatientToCohort,
7-
deleteCohort,
8-
deletePatient,
9-
generateRandomCohort,
10-
generateRandomPatient,
11-
removePatientFromCohort,
12-
} from '../commands';
5+
import { addPatientToCohort, deleteCohort, generateRandomCohort, removePatientFromCohort } from '../commands';
136
import { type Cohort, type CohortMember, type Patient } from '../types';
147

158
let cohortMembership: CohortMember;
169
let cohort: Cohort;
17-
let patient: Patient;
1810

19-
test.beforeEach(async ({ api }) => {
20-
patient = await generateRandomPatient(api);
11+
test.beforeEach(async ({ api, patient }) => {
2112
cohort = await generateRandomCohort(api);
2213
cohortMembership = await addPatientToCohort(api, cohort.uuid, patient.uuid);
2314
});
2415

25-
test.skip('Return to patient list from the patient chart', async ({ page }) => {
16+
test.skip('Return to patient list from the patient chart', async ({ page, patient }) => {
2617
const patientListPage = new PatientListsPage(page);
2718

2819
await test.step('When I navigate to the patient list', async () => {
@@ -48,7 +39,10 @@ test.skip('Return to patient list from the patient chart', async ({ page }) => {
4839
});
4940
});
5041

51-
test.skip('Return to patient list after navigating to visits page from the patient chart', async ({ page }) => {
42+
test.skip('Return to patient list after navigating to visits page from the patient chart', async ({
43+
page,
44+
patient,
45+
}) => {
5246
const patientListPage = new PatientListsPage(page);
5347

5448
await test.step('When I navigate to the patient list', async () => {
@@ -82,7 +76,7 @@ test.skip('Return to patient list after navigating to visits page from the patie
8276
});
8377
});
8478

85-
test.skip('Return to patient list after navigating to visits and refreshing the page', async ({ page }) => {
79+
test.skip('Return to patient list after navigating to visits and refreshing the page', async ({ page, patient }) => {
8680
const patientListPage = new PatientListsPage(page);
8781

8882
await test.step('When I navigate to the patient list', async () => {
@@ -120,7 +114,7 @@ test.skip('Return to patient list after navigating to visits and refreshing the
120114
});
121115
});
122116

123-
test.skip('Return to patient list from the patient chart on a new tab', async ({ page, context }) => {
117+
test.skip('Return to patient list from the patient chart on a new tab', async ({ page, context, patient }) => {
124118
const patientListPage = new PatientListsPage(page);
125119
const locator = page.locator('table tbody tr td:nth-child(1) a');
126120
const pagePromise = context.waitForEvent('page');
@@ -163,6 +157,5 @@ test.skip('Return to patient list from the patient chart on a new tab', async ({
163157

164158
test.afterEach(async ({ api }) => {
165159
await removePatientFromCohort(api, cohortMembership.uuid);
166-
await deletePatient(api, patient.uuid);
167160
await deleteCohort(api, cohort.uuid);
168161
});

0 commit comments

Comments
 (0)