Skip to content

Commit 62bf302

Browse files
committed
Add system test helper for validation errors
This helper provides a convenient way to test form validations with slightly more detail than simply asserting the presence of the error on the page. The helper: - Asserts that the error link appears in the summary box - Locates the error link by the expected id - Locates the error within the expected fieldset - Asserts that the error is a span with the expected id
1 parent c23bd85 commit 62bf302

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

manage_breast_screening/config/system_test_setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
5-
from playwright.sync_api import sync_playwright
5+
from playwright.sync_api import sync_playwright, expect
66

77

88
@pytest.mark.system
@@ -26,3 +26,12 @@ def setUp(self):
2626

2727
def tearDown(self):
2828
self.page.close()
29+
30+
def expect_validation_error(self, id: str, fieldset_legend: str, error_text: str):
31+
summary_box = self.page.locator(".nhsuk-error-summary")
32+
error_link = summary_box.locator(f"a[href='#{id}']")
33+
expect(error_link).to_have_text(error_text)
34+
35+
fieldset = self.page.locator('fieldset').filter(has_text=fieldset_legend)
36+
error_span = fieldset.locator("span").filter(has_text=error_text)
37+
expect(error_span).to_have_id(id)

manage_breast_screening/record_a_mammogram/tests/system/test_user_submits_cannot_go_ahead_form.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ def when_i_submit_the_form(self):
4343
self.page.get_by_role("button", name="Continue").click()
4444

4545
def then_i_should_see_validation_errors(self):
46-
expect(
47-
self.page.locator("#stopped_reasons-error")
48-
).to_have_text(re.compile("A reason for why this appointment cannot continue must be provided"))
46+
self.expect_validation_error(
47+
id="stopped_reasons-error",
48+
fieldset_legend="Why has this appointment been stopped?",
49+
error_text="A reason for why this appointment cannot continue must be provided",
50+
)
51+
self.expect_validation_error(
52+
id="decision-error",
53+
fieldset_legend="Does the appointment need to be rescheduled?",
54+
error_text="Select whether the participant needs to be invited for another appointment"
55+
)
4956

5057
def when_i_select_a_reason_for_the_appointment_being_stopped(self):
5158
self.page.get_by_label("Failed identity check").check()
@@ -58,9 +65,11 @@ def and_i_choose_to_add_the_participant_to_the_reinvite_list(self):
5865
expect(self.page.get_by_label("Yes, add participant to reinvite list")).to_be_checked()
5966

6067
def then_i_see_an_error_for_other_details(self):
61-
expect(
62-
self.page.locator("#other_details-error")
63-
).to_have_text(re.compile("Explain why this appointment cannot proceed"))
68+
self.expect_validation_error(
69+
id="other_details-error",
70+
fieldset_legend="Why has this appointment been stopped?",
71+
error_text="Explain why this appointment cannot proceed",
72+
)
6473

6574
def when_i_fill_in_other_details(self):
6675
self.page.locator("#other_details").fill("Explain other choice")
@@ -72,4 +81,4 @@ def then_i_see_the_clinics_page(self):
7281

7382
def and_the_appointment_is_updated(self):
7483
self.appointment.refresh_from_db()
75-
self.assertEqual(self.appointment.status, Appointment.Status.ATTENDED_NOT_SCREENED)
84+
self.assertEqual(self.appointment.status, Appointment.Status.ATTENDED_NOT_SCREENED)

0 commit comments

Comments
 (0)