Skip to content

Commit aac631e

Browse files
Feature/bcss 20540 refactor main menu click (#72)
<!-- markdownlint-disable-next-line first-line-heading --> ## Description <!-- Describe your changes in detail. --> Improving the reliability of the main menu click method ## Context <!-- Why is this change required? What problem does it solve? --> Some tests would fail as the main menu link was clicked before the page fully loaded. This changes makes it so that once the main menu link is clicked it checks it has been redirected to the correct page. If it has not then it waits 0.2 seconds and tries again. ## Type of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply. --> - [x] Refactoring (non-breaking change) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I am familiar with the [contributing guidelines](https://github.com/nhs-england-tools/playwright-python-blueprint/blob/main/CONTRIBUTING.md) - [x] I have followed the code style of the project - [ ] I have added tests to cover my changes (where appropriate) - [x] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [x] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.
1 parent 074b567 commit aac631e

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

pages/base_page.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class BasePage:
77

88
def __init__(self, page: Page):
99
self.page = page
10+
# Homepage - vars
11+
self.main_menu_string = "Main Menu"
1012
# Homepage/Navigation Bar links
1113
self.sub_menu_link = self.page.get_by_role("link", name="Show Sub-menu")
1214
self.hide_sub_menu_link = self.page.get_by_role("link", name="Hide Sub-menu")
@@ -16,7 +18,7 @@ def __init__(self, page: Page):
1618
self.refresh_alerts_link = self.page.get_by_role("link", name="Refresh alerts")
1719
self.user_guide_link = self.page.get_by_role("link", name="User guide")
1820
self.help_link = self.page.get_by_role("link", name="Help")
19-
self.main_menu_link = self.page.get_by_role("link", name="Main Menu")
21+
self.main_menu_link = self.page.get_by_role("link", name=self.main_menu_string)
2022
self.log_out_link = self.page.get_by_role("link", name="Log-out")
2123
# Main menu - page links
2224
self.contacts_list_page = self.page.get_by_role("link", name="Contacts List")
@@ -54,8 +56,33 @@ def __init__(self, page: Page):
5456

5557
def click_main_menu_link(self) -> None:
5658
"""Click the Base Page 'Main Menu' link if it is visible."""
57-
if self.main_menu_link.is_visible():
58-
self.click(self.main_menu_link)
59+
loops = 0
60+
text = None
61+
62+
while loops <= 3:
63+
if self.main_menu_link.is_visible():
64+
self.click(self.main_menu_link)
65+
try:
66+
if self.main_menu__header.is_visible():
67+
text = self.main_menu__header.text_content()
68+
else:
69+
text = None
70+
except Exception as e:
71+
logging.warning(f"Could not read header text: {e}")
72+
text = None
73+
74+
if text and self.main_menu_string in text:
75+
return # Success
76+
else:
77+
logging.warning("Main Menu click failed, retrying after 0.2 seconds")
78+
# The timeout is in place here to allow the page ample time to load if it has not already been loaded
79+
self.page.wait_for_timeout(200)
80+
81+
loops += 1
82+
# All attempts failed
83+
raise RuntimeError(
84+
f"Failed to navigate to Main Menu after {loops} attempts. Last page title was: '{text or 'unknown'}'"
85+
)
5986

6087
def click_log_out_link(self) -> None:
6188
"""Click the Base Page 'Log-out' link."""
@@ -100,8 +127,11 @@ def bowel_cancer_screening_system_header_is_displayed(self) -> None:
100127
)
101128

102129
def main_menu_header_is_displayed(self) -> None:
103-
"""Asserts that the Main Menu header is displayed."""
104-
expect(self.main_menu__header).to_contain_text("Main Menu")
130+
"""
131+
Asserts that the Main Menu header is displayed.
132+
self.main_menu_string contains the string 'Main Menu'
133+
"""
134+
expect(self.main_menu__header).to_contain_text(self.main_menu_string)
105135

106136
def bowel_cancer_screening_page_title_contains_text(self, text: str) -> None:
107137
"""Asserts that the page title contains the specified text.

0 commit comments

Comments
 (0)