From b5acc83e30721583c0c95a0d1c5d520023e37ad7 Mon Sep 17 00:00:00 2001 From: MazOneTwoOne <76905544+MazOneTwoOne@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:51:28 +0000 Subject: [PATCH] WIP: - add tests --- fala/apps/adviser/forms.py | 3 +- fala/apps/adviser/tests/page_objects.py | 14 +++++ ...ategory_search_page_journeys_playwright.py | 51 +++++++++++++++++++ ...py => test_single_category_search_view.py} | 0 fala/playwright/setup.py | 13 ++++- 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 fala/apps/adviser/tests/test_single_category_search_page_journeys_playwright.py rename fala/apps/adviser/tests/{test_single_category_search.py => test_single_category_search_view.py} (100%) diff --git a/fala/apps/adviser/forms.py b/fala/apps/adviser/forms.py index ccf5a592..2807daca 100644 --- a/fala/apps/adviser/forms.py +++ b/fala/apps/adviser/forms.py @@ -174,13 +174,12 @@ def clean(self): self.add_error("postcode", _("Enter a postcode")) return data - # Validate postcode and set `_country_from_valid_postcode` if postcode: valid_postcode = self.validate_postcode_and_return_country(postcode) if not valid_postcode: self.add_error("postcode", _("Enter a valid postcode")) else: - self._country_from_valid_postcode = valid_postcode # This is used by the `region` property + self._country_from_valid_postcode = valid_postcode # Check if categories are provided if not categories: diff --git a/fala/apps/adviser/tests/page_objects.py b/fala/apps/adviser/tests/page_objects.py index 33e7f66a..257dbfba 100644 --- a/fala/apps/adviser/tests/page_objects.py +++ b/fala/apps/adviser/tests/page_objects.py @@ -75,5 +75,19 @@ def search(self, postcode): self._page.locator("#searchButton").click() +class SingleCategorySearchPage(FalaPage): + @property + def postcode_input_field(self): + return self._page.get_by_label("Postcode") + + @property + def search_button(self): + return self.item_from_text("Search") + + def search(self, postcode): + self._page.locator("#id_postcode").fill(postcode) + self._page.locator("#searchButton").click() + + class CookiesPage(FalaPage): pass diff --git a/fala/apps/adviser/tests/test_single_category_search_page_journeys_playwright.py b/fala/apps/adviser/tests/test_single_category_search_page_journeys_playwright.py new file mode 100644 index 00000000..7d4d3f45 --- /dev/null +++ b/fala/apps/adviser/tests/test_single_category_search_page_journeys_playwright.py @@ -0,0 +1,51 @@ +from playwright.sync_api import expect +from fala.playwright.setup import PlaywrightTestSetup + + +class SingleCategorySearchPageEndToEndJourneys(PlaywrightTestSetup): + hlpas_front_page_heading = "Find a legal aid adviser for Housing Loss Prevention Advice Service" + + def test_landing_page(self): + page = self.visit_single_category_search_page("categories=hlpas") + expect(page.h1).to_have_text(f"{self.hlpas_front_page_heading}") + + def test_postcode_search_journey(self): + test_cases = [ + "SW1H 9AJ", + "SE11", + ] + for postcode in test_cases: + with self.subTest(postcode=postcode): + page = self.visit_results_page(postcode=postcode) + expect(page.h1).to_have_text("Search results") + expect(page.change_search_grey_box.nth(0)).to_have_text(f"Postcode: {postcode}") + expect(page.item_from_text("in order of closeness")).to_be_visible() + + def test_full_search_journey(self): + page = self.visit_results_page_with_full_search( + "SE11", "Islington Law Centre", ["Housing Loss Prevention Advice Service"] + ) + expect(page.h1).to_have_text("Search results") + # this selector matches multiple things so picking out the things we want using 'nth()' + expect(page.change_search_grey_box.nth(0)).to_have_text("Postcode: SE11") + expect(page.change_search_grey_box.nth(1)).to_have_text("Organisation: Islington Law Centre") + expect(page.change_search_grey_box.nth(2)).to_have_text( + "Legal problem: Housing Loss Prevention Advice Service" + ) + + def test_invalid_postcode_journey(self): + test_cases = [ + "ZZZ1", + "G12 OGJKLJGK", + "LS25 ghjkhjkh", + "IM4 TESTTTTTTTTTTTT", + ] + for postcode in test_cases: + with self.subTest(postcode=postcode): + page = self.browser.new_page() + page.goto(f"{self.live_server_url}") + expect(page.locator("h1")).to_have_text(f"{self.front_page_heading}") + page.get_by_label("Postcode").fill(f"{postcode}") + page.get_by_role("button", name="Search").click() + expect(page.locator("h1")).to_have_text(f"{self.front_page_heading}") + expect(page.locator("css=.govuk-error-summary")).to_be_visible() diff --git a/fala/apps/adviser/tests/test_single_category_search.py b/fala/apps/adviser/tests/test_single_category_search_view.py similarity index 100% rename from fala/apps/adviser/tests/test_single_category_search.py rename to fala/apps/adviser/tests/test_single_category_search_view.py diff --git a/fala/playwright/setup.py b/fala/playwright/setup.py index 2b7c12c9..d38ba8df 100644 --- a/fala/playwright/setup.py +++ b/fala/playwright/setup.py @@ -1,7 +1,13 @@ import os from django.contrib.staticfiles.testing import StaticLiveServerTestCase from playwright.sync_api import sync_playwright -from fala.apps.adviser.tests.page_objects import ResultsPage, OtherRegionPage, SearchPage, CookiesPage +from fala.apps.adviser.tests.page_objects import ( + ResultsPage, + OtherRegionPage, + SearchPage, + CookiesPage, + SingleCategorySearchPage, +) class PlaywrightTestSetup(StaticLiveServerTestCase): @@ -69,6 +75,11 @@ def visit_search_page(self): page.goto(f"{self.live_server_url}") return SearchPage(page) + def visit_single_category_search_page(self, url_params): + page = self.browser.new_page() + page.goto(f"{self.live_server_url}/?single-category-search/{url_params}") + return SingleCategorySearchPage(page) + def visit_cookies_page_from_footer(self): page = self.browser.new_page() page.goto(f"{self.live_server_url}")