Skip to content

Commit 2f66296

Browse files
authored
Merge pull request #782 from NHSDigital/extract-repeated-methods
Extract repeated methods
2 parents bc00547 + 04d3595 commit 2f66296

File tree

9 files changed

+54
-76
lines changed

9 files changed

+54
-76
lines changed

mavis/test/pages/children.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ def click_invite_to_community_clinic(self) -> None:
198198
def click_edit_child_record(self) -> None:
199199
self.edit_child_record_button.click()
200200

201-
def expect_text_in_alert(self, text: str) -> None:
202-
expect(self.page.get_by_role("alert")).to_contain_text(text)
203-
204201
@step("Click on Archive child record")
205202
def click_archive_child_record(self) -> None:
206203
self.archive_child_record_link.click()

mavis/test/pages/nurse_consent.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Parent,
99
Programme,
1010
)
11-
from mavis.test.utils import generate_random_string
11+
from mavis.test.utils import expect_alert_text, generate_random_string
1212

1313

1414
class NurseConsentWizardPage:
@@ -168,9 +168,6 @@ def click_yes_for_nasal_spray(self) -> None:
168168
def click_yes_for_injected_vaccine(self) -> None:
169169
self.online_flu_agree_injection_radio.check()
170170

171-
def expect_text_in_alert(self, text: str) -> None:
172-
expect(self.page.get_by_role("alert")).to_contain_text(text)
173-
174171
def record_parent_positive_consent(
175172
self,
176173
programme: Programme = Programme.HPV,
@@ -274,7 +271,7 @@ def _process_consent_confirmation(
274271
def update_triage_outcome_positive(self) -> None:
275272
self.click_safe_to_vaccinate()
276273
self.click_save_triage()
277-
self.expect_text_in_alert("Triage outcome updated")
274+
expect_alert_text(self.page, "Triage outcome updated")
278275

279276
def click_withdraw_consent(self) -> None:
280277
self.withdraw_consent_button.click()

mavis/test/pages/programmes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from io import StringIO
33

44
import pandas as pd
5-
from playwright.sync_api import Page, expect
5+
from playwright.sync_api import Page
66

77
from mavis.test.annotations import step
88
from mavis.test.data import TestData
@@ -221,6 +221,3 @@ def click_use_duplicate(self) -> None:
221221
@step("Click on Resolve duplicate")
222222
def click_resolve_duplicate(self) -> None:
223223
self.resolve_duplicate_button.click()
224-
225-
def expect_alert_text(self, text: str) -> None:
226-
expect(self.page.get_by_role("alert")).to_contain_text(text)

mavis/test/pages/sessions.py

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
)
2020
from mavis.test.utils import (
2121
MAVIS_NOTE_LENGTH_LIMIT,
22+
expect_alert_text,
23+
expect_details,
2224
get_current_datetime_compact,
2325
get_day_month_year_from_compact_date,
2426
get_offset_date,
@@ -431,24 +433,26 @@ def create_invalid_session(self) -> None:
431433
self.add_or_change_session_dates()
432434
self.fill_date_fields(_invalid_date)
433435
self.click_continue_button()
434-
self.expect_alert_text("Enter a date")
436+
expect_alert_text(self.page, "Enter a date")
435437
self.click_back()
436438

437439
def create_session_in_previous_academic_year(self) -> None:
438440
_previous_year_date = get_offset_date_compact_format(offset_days=-365)
439441
self.add_or_change_session_dates()
440442
self.fill_date_fields(_previous_year_date)
441443
self.click_continue_button()
442-
self.expect_alert_text("Enter a date on or after the start of the school year")
444+
expect_alert_text(
445+
self.page, "Enter a date on or after the start of the school year"
446+
)
443447
self.click_back()
444448

445449
def create_session_in_next_academic_year(self) -> None:
446450
_next_year_date = get_offset_date_compact_format(offset_days=365)
447451
self.add_or_change_session_dates()
448452
self.fill_date_fields(_next_year_date)
449453
self.click_continue_button()
450-
self.expect_alert_text(
451-
"Enter a date on or before the end of the current school year"
454+
expect_alert_text(
455+
self.page, "Enter a date on or before the end of the current school year"
452456
)
453457
self.click_back()
454458

@@ -536,7 +540,8 @@ def schedule_a_valid_session(
536540
if self.keep_session_dates_button.is_visible():
537541
self.click_keep_session_dates() # MAV-2066
538542

539-
self.expect_details(
543+
expect_details(
544+
self.page,
540545
"Session dates",
541546
self._get_day_month_year_with_day_of_week(date_to_format=_future_date),
542547
)
@@ -562,7 +567,8 @@ def schedule_a_valid_mmr_session(
562567
if self.keep_session_dates_button.is_visible():
563568
self.click_keep_session_dates() # MAV-2066
564569

565-
self.expect_details(
570+
expect_details(
571+
self.page,
566572
"Session dates",
567573
self._get_day_month_year_with_day_of_week(date_to_format=_future_date),
568574
)
@@ -582,18 +588,6 @@ def answer_whether_psd_should_be_enabled(self, answer: str) -> None:
582588
),
583589
).get_by_label(answer).check()
584590

585-
def expect_alert_text(self, text: str) -> None:
586-
expect(self.page.get_by_role("alert")).to_contain_text(text)
587-
588-
def expect_details(self, key: str, value: str) -> None:
589-
detail_key = self.page.locator(
590-
".nhsuk-summary-list__key",
591-
has_text=re.compile(f"^{key}$"),
592-
).first
593-
detail_value = detail_key.locator("xpath=following-sibling::*[1]")
594-
595-
expect(detail_value).to_contain_text(value)
596-
597591

598592
class SessionsChildrenPage(SearchBarMixin, SessionsTabsMixin):
599593
def __init__(self, page: Page) -> None:
@@ -860,11 +854,11 @@ def invalidate_parent_refusal(self, parent: Parent) -> None:
860854
self.click_mark_as_invalid_link()
861855
self.fill_notes(invalidation_notes)
862856
self.click_mark_as_invalid_button()
863-
self.expect_details("Response", "Invalid")
864-
self.expect_details("Notes", invalidation_notes)
857+
expect_details(self.page, "Response", "Invalid")
858+
expect_details(self.page, "Notes", invalidation_notes)
865859

866860
self.click_back()
867-
self.expect_details("Response", "Invalid")
861+
expect_details(self.page, "Response", "Invalid")
868862
expect(self.page.get_by_text("No requests have been sent.")).to_be_visible()
869863

870864
@step("Click Back")
@@ -879,15 +873,6 @@ def click_continue_button(self) -> None:
879873
def fill_notes(self, notes: str) -> None:
880874
self.notes_textbox.fill(notes)
881875

882-
def expect_details(self, key: str, value: str) -> None:
883-
detail_key = self.page.locator(
884-
".nhsuk-summary-list__key",
885-
has_text=re.compile(f"^{key}$"),
886-
).first
887-
detail_value = detail_key.locator("xpath=following-sibling::*[1]")
888-
889-
expect(detail_value).to_contain_text(value)
890-
891876
@step("Click on Yes")
892877
def select_identity_confirmed_by_child(self, child: Child) -> None:
893878
self.page.get_by_role(
@@ -967,10 +952,7 @@ def go_back_to_session_for_school(self, school: School) -> None:
967952
expect(self.page.get_by_role("heading", name=school.name).first).to_be_visible()
968953

969954
def verify_triage_updated_for_child(self) -> None:
970-
self.expect_alert_text("Triage outcome updated")
971-
972-
def expect_alert_text(self, text: str) -> None:
973-
expect(self.page.get_by_role("alert")).to_contain_text(text)
955+
expect_alert_text(self.page, "Triage outcome updated")
974956

975957
@step("Triage MMR patient")
976958
def triage_mmr_patient(self, consent_option: ConsentOption) -> None:
@@ -1037,15 +1019,12 @@ def add_note(self, note: str) -> None:
10371019
with self.page.expect_navigation():
10381020
self.click_save_note()
10391021

1040-
self.expect_alert_text("Note added")
1022+
expect_alert_text(self.page, "Note added")
10411023
reload_until_element_is_visible(self.page, self.page.get_by_text(note))
10421024

10431025
def check_session_activity_entry(self, text: str) -> None:
10441026
expect(self.page.get_by_role("heading", name=text).first).to_be_visible()
10451027

1046-
def expect_alert_text(self, text: str) -> None:
1047-
expect(self.page.get_by_role("alert")).to_contain_text(text)
1048-
10491028

10501029
class SessionsVaccinationWizardPage:
10511030
def __init__(self, page: Page) -> None:
@@ -1082,18 +1061,6 @@ def choose_batch(self, batch_name: str) -> None:
10821061
self.page.get_by_role("radio", name=batch_name).check()
10831062
self.click_continue_button()
10841063

1085-
def expect_details(self, key: str, value: str) -> None:
1086-
detail_key = self.page.locator(
1087-
".nhsuk-summary-list__key",
1088-
has_text=re.compile(f"^{key}$"),
1089-
).first
1090-
detail_value = detail_key.locator("xpath=following-sibling::*[1]")
1091-
1092-
expect(detail_value).to_contain_text(value)
1093-
1094-
def expect_alert_text(self, text: str) -> None:
1095-
expect(self.page.get_by_role("alert")).to_contain_text(text)
1096-
10971064
def record_vaccination(
10981065
self,
10991066
vaccination_record: VaccinationRecord,
@@ -1106,9 +1073,9 @@ def record_vaccination(
11061073

11071074
if at_school: # only skips MAV-854
11081075
if psd_option:
1109-
self.expect_details("Protocol", "Patient Specific Direction")
1076+
expect_details(self.page, "Protocol", "Patient Specific Direction")
11101077
else:
1111-
self.expect_details("Protocol", "Patient Group Direction (PGD)")
1078+
expect_details(self.page, "Protocol", "Patient Group Direction (PGD)")
11121079

11131080
self.vaccination_notes.fill(notes)
11141081
self.click_confirm_button()
@@ -1118,6 +1085,7 @@ def record_vaccination(
11181085
self.vaccination_notes.fill("Confirmation notes")
11191086
self.click_confirm_button()
11201087

1121-
self.expect_alert_text(
1122-
f"Vaccination outcome recorded for {vaccination_record.programme}"
1088+
expect_alert_text(
1089+
self.page,
1090+
f"Vaccination outcome recorded for {vaccination_record.programme}",
11231091
)

mavis/test/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,17 @@ def reload_until_element_is_not_visible(
183183
page.reload()
184184
else:
185185
expect(tag).to_be_hidden()
186+
187+
188+
def expect_alert_text(page: Page, text: str) -> None:
189+
expect(page.get_by_role("alert")).to_contain_text(text)
190+
191+
192+
def expect_details(page: Page, key: str, value: str) -> None:
193+
detail_key = page.locator(
194+
".nhsuk-summary-list__key",
195+
has_text=re.compile(f"^{key}$"),
196+
).first
197+
detail_value = detail_key.locator("xpath=following-sibling::*[1]")
198+
199+
expect(detail_value).to_contain_text(value)

tests/test_children.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from mavis.test.annotations import issue
44
from mavis.test.data import ClassFileMapping, CohortsFileMapping, VaccsFileMapping
55
from mavis.test.models import Programme
6-
from mavis.test.utils import get_offset_date
6+
from mavis.test.utils import expect_alert_text, get_offset_date
77

88
pytestmark = pytest.mark.children
99

@@ -169,7 +169,7 @@ def test_invalid_nhs_number_change_is_rejected(
169169
child_edit_page.click_change_nhs_no()
170170
child_edit_page.fill_nhs_no_for_child(child, "9123456789")
171171
child_edit_page.click_continue()
172-
child_record_page.expect_text_in_alert("Enter a valid NHS number")
172+
expect_alert_text(child_record_page.page, "Enter a valid NHS number")
173173

174174

175175
@issue("MAV-1839")
@@ -200,7 +200,7 @@ def test_merge_child_records_does_not_crash(
200200
child_record_page.click_archive_child_record()
201201
child_archive_page.click_its_a_duplicate(child2.nhs_number)
202202
child_archive_page.click_archive_record()
203-
child_record_page.expect_text_in_alert("This record has been archived")
203+
expect_alert_text(child_record_page.page, "This record has been archived")
204204

205205

206206
@pytest.mark.accessibility

tests/test_nurse_consent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from mavis.test.annotations import issue
44
from mavis.test.data import CohortsFileMapping
55
from mavis.test.models import ConsentMethod, DeliverySite, Programme, Vaccine
6-
from mavis.test.utils import MAVIS_NOTE_LENGTH_LIMIT, get_offset_date
6+
from mavis.test.utils import MAVIS_NOTE_LENGTH_LIMIT, expect_alert_text, get_offset_date
77

88
pytestmark = pytest.mark.consent
99

@@ -328,7 +328,7 @@ def test_conflicting_consent_with_gillick_consent(
328328

329329
nurse_consent_wizard_page.select_gillick_competent_child()
330330
nurse_consent_wizard_page.record_child_positive_consent()
331-
nurse_consent_wizard_page.expect_text_in_alert(f"Consent recorded for {child!s}")
331+
expect_alert_text(nurse_consent_wizard_page.page, f"Consent recorded for {child!s}")
332332

333333
sessions_consent_page.select_consent_given_filters_for_programme(Programme.HPV)
334334
sessions_consent_page.search_and_click_child(child)

tests/test_programmes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from mavis.test.annotations import issue
44
from mavis.test.data import CohortsFileMapping
55
from mavis.test.models import Programme, ReportFormat
6+
from mavis.test.utils import expect_alert_text
67

78

89
@pytest.fixture
@@ -174,7 +175,9 @@ def test_edit_vaccination_dose_to_not_given(
174175
edit_vaccination_record_page.click_they_refused_it()
175176
edit_vaccination_record_page.click_continue()
176177
edit_vaccination_record_page.click_save_changes()
177-
programme_children_page.expect_alert_text("Vaccination outcome recorded for HPV")
178+
expect_alert_text(
179+
programme_children_page.page, "Vaccination outcome recorded for HPV"
180+
)
178181

179182

180183
@pytest.mark.reports

tests/test_sessions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from mavis.test.annotations import issue
55
from mavis.test.data import ClassFileMapping
66
from mavis.test.models import ConsentMethod, Programme, VaccinationRecord, Vaccine
7-
from mavis.test.utils import get_offset_date
7+
from mavis.test.utils import expect_alert_text, expect_details, get_offset_date
88

99
pytestmark = pytest.mark.sessions
1010

@@ -325,7 +325,7 @@ def test_consent_refused_and_activity_log(
325325
nurse_consent_wizard_page.select_parent(child.parents[0])
326326
nurse_consent_wizard_page.select_consent_method(ConsentMethod.PAPER)
327327
nurse_consent_wizard_page.record_parent_refuse_consent()
328-
nurse_consent_wizard_page.expect_text_in_alert(str(child))
328+
expect_alert_text(nurse_consent_wizard_page.page, str(child))
329329

330330
sessions_consent_page.select_consent_refused()
331331
sessions_consent_page.search_and_click_child(child)
@@ -406,7 +406,9 @@ def test_verify_excel_export_and_clinic_invitation(
406406
sessions_vaccination_wizard_page.check_location_radio(clinics[0])
407407
sessions_vaccination_wizard_page.click_continue_button()
408408
sessions_vaccination_wizard_page.click_confirm_button()
409-
sessions_patient_page.expect_alert_text("Vaccination outcome recorded for HPV")
409+
expect_alert_text(
410+
sessions_patient_page.page, "Vaccination outcome recorded for HPV"
411+
)
410412
dashboard_page.click_mavis()
411413
dashboard_page.click_sessions()
412414
sessions_search_page.click_session_for_programme_group(school, Programme.HPV)
@@ -459,7 +461,7 @@ def test_editing_session_programmes(
459461
sessions_edit_page.click_change_programmes()
460462
sessions_edit_page.add_programme(Programme.FLU)
461463
sessions_edit_page.click_continue_button()
462-
sessions_edit_page.expect_details("Programmes", "Flu HPV")
464+
expect_details(sessions_edit_page.page, "Programmes", "Flu HPV")
463465
sessions_edit_page.click_save_changes()
464466
sessions_edit_page.expect_session_to_have_programmes([Programme.FLU, Programme.HPV])
465467
sessions_overview_page.click_consent_tab()

0 commit comments

Comments
 (0)