Skip to content

Commit 80e0d87

Browse files
test: enhance initial setup in HomePageTest to handle first-time alerts and navigation more robustly
1 parent ec62edf commit 80e0d87

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

tests/test_home_page.py

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ class HomePageTest(unittest.TestCase): # Changed base class
2020
PERCENTAGE_INPUT_SELECTOR = "input.home__input[placeholder=\"0\"][type=\"number\"]"
2121
ADD_GRADE_BUTTON_SELECTOR = "button.home__add-button" # Selector for "Agregar nota"
2222
GRADES_LIST_ITEM_SELECTOR = "div.home__grades-container > div.home__grade-row"
23-
# Example: If there\'s a specific element for the grade value in a list item:
24-
# GRADE_VALUE_IN_LIST_SELECTOR = ".grade-value-class" # Placeholder
25-
# PERCENTAGE_VALUE_IN_LIST_SELECTOR = ".percentage-value-class" # Placeholder
2623
CALCULATE_BUTTON_SELECTOR = "button.home__calculate-button" # Selector for "Calcular
2724

25+
# Selectors for alerts and navigation
26+
FIRST_TIME_ALERT_BUTTON_SELECTOR = ".alert__button.alert__button--single" # For "Configurar" on first-time alert
27+
ALERT_OVERLAY_SELECTOR = "div.alert__overlay"
28+
NAV_BACK_BUTTON_XPATH = "//button[contains(@class, 'nav-bar__button') and .//span[contains(@class, 'back-icon')]/svg[contains(@class, 'lucide-chevron-left')]]"
2829

2930
@classmethod
3031
def setUpClass(cls):
@@ -72,32 +73,58 @@ def _take_screenshot(self, name_suffix):
7273
def _initial_setup(self): # Removed driver argument
7374
self.driver.get(self.BASE_URL) # Use BASE_URL
7475

75-
# Attempt to handle an initial alert/overlay
76+
first_time_alert_handled = False
7677
try:
77-
# Assuming the button to close the alert has the selector ".alert__button.alert__button--single"
78-
# This selector might need to be confirmed if issues persist.
79-
alert_button_selector = ".alert__button.alert__button--single"
78+
logger.info(f"Attempting to handle first-time alert with button '{self.FIRST_TIME_ALERT_BUTTON_SELECTOR}'.")
8079
alert_button = WebDriverWait(self.driver, 5).until( # Using wait_short's timeout
81-
EC.element_to_be_clickable((By.CSS_SELECTOR, alert_button_selector))
80+
EC.element_to_be_clickable((By.CSS_SELECTOR, self.FIRST_TIME_ALERT_BUTTON_SELECTOR))
8281
)
8382
alert_button.click()
84-
logger.info(f"Initial alert with button '{alert_button_selector}' handled.")
85-
# Wait a very short moment for the overlay to disappear
86-
time.sleep(0.5)
83+
logger.info(f"Clicked first-time alert button: '{self.FIRST_TIME_ALERT_BUTTON_SELECTOR}'.");
84+
85+
# Wait for the overlay to become invisible
86+
WebDriverWait(self.driver, 5).until( # Using wait_short's timeout
87+
EC.invisibility_of_element_located((By.CSS_SELECTOR, self.ALERT_OVERLAY_SELECTOR))
88+
)
89+
logger.info(f"Alert overlay '{self.ALERT_OVERLAY_SELECTOR}' is no longer visible.")
90+
first_time_alert_handled = True;
91+
8792
except TimeoutException:
88-
logger.info("Initial alert/overlay button not found or not clickable within timeout, proceeding.")
93+
logger.info(f"First-time alert (button '{self.FIRST_TIME_ALERT_BUTTON_SELECTOR}' or overlay '{self.ALERT_OVERLAY_SELECTOR}') not found or not handled within timeout. Assuming not first time or alert already dismissed.")
94+
# self._take_screenshot("debug_first_time_alert_timeout") # Optional: for debugging if this path is unexpected
8995
except Exception as e:
90-
logger.warning(f"An unexpected error occurred while trying to handle initial alert: {e}")
91-
self._take_screenshot("error_initial_alert_handling") # Optional: screenshot if alert handling fails unexpectedly
96+
logger.warning(f"An unexpected error occurred while trying to handle first-time alert: {e}")
97+
self._take_screenshot("error_initial_alert_handling")
9298

99+
if first_time_alert_handled:
100+
try:
101+
logger.info("First-time alert was handled, app should be on Settings page. Attempting to navigate back to Home.")
102+
nav_back_button = WebDriverWait(self.driver, 10).until(
103+
EC.element_to_be_clickable((By.XPATH, self.NAV_BACK_BUTTON_XPATH))
104+
)
105+
nav_back_button.click()
106+
logger.info("Clicked navigation bar 'Atras' button to return to Home page.")
107+
# Add a short wait for page transition if necessary, before checking for home page elements
108+
time.sleep(0.5) # Allow a moment for navigation
109+
except TimeoutException:
110+
logger.error(f"Failed to find or click the navigation bar 'Atras' button (XPATH: {self.NAV_BACK_BUTTON_XPATH}) after handling first-time alert.")
111+
self._take_screenshot("error_nav_back_button_not_found")
112+
# This is a critical failure in the setup flow if the first-time alert was handled
113+
raise Exception("Failed to navigate back from Settings page during initial setup.")
114+
except Exception as e:
115+
logger.error(f"An error occurred clicking the navigation bar 'Atras' button: {e}")
116+
self._take_screenshot("error_nav_back_button_click")
117+
raise
118+
119+
# Final verification: Ensure the Home page grade input is present
93120
try:
94121
self.wait_long.until(EC.presence_of_element_located((By.CSS_SELECTOR, self.GRADE_INPUT_SELECTOR)))
95-
logger.info(f"Grade input (\'{self.GRADE_INPUT_SELECTOR}\') found on page load at {self.BASE_URL}.")
122+
logger.info(f"Grade input ('{self.GRADE_INPUT_SELECTOR}') found on page. Setup complete.")
96123
except TimeoutException:
97124
current_url = self.driver.current_url
98-
logger.error(f"Failed to find grade input (\'{self.GRADE_INPUT_SELECTOR}\') on initial load. URL: {current_url}")
99-
self._take_screenshot("error_initial_setup_grade_input_not_found")
100-
raise Exception(f"Could not find the grade input (\'{self.GRADE_INPUT_SELECTOR}\') during setup. Current URL: {current_url}")
125+
logger.error(f"Failed to find grade input ('{self.GRADE_INPUT_SELECTOR}') after setup attempts. Current URL: {current_url}")
126+
self._take_screenshot("error_final_grade_input_not_found")
127+
raise Exception(f"Could not find the grade input ('{self.GRADE_INPUT_SELECTOR}') after setup. Current URL: {current_url}")
101128

102129
def _add_grade_and_percentage(self, grade, percentage): # Removed driver argument
103130
# Find the *first* available grade and percentage input.
@@ -213,7 +240,7 @@ def test_us01_add_multiple_valid_grades(self, request=None):
213240
def test_us01_validate_grade_input_below_range(self, request=None):
214241
test_name = request.node.name if request else self._testMethodName
215242
logger.info(f"Running test: {test_name}")
216-
self._initial_setup() # Already called by self.setUp()
243+
# self._initial_setup() # Removed redundant call, already called by self.setUp()
217244

218245
initial_item_count = self._get_grades_list_item_count()
219246
self._add_grade_and_percentage("-1.0", "20") # Invalid grade (below min 0)
@@ -228,7 +255,7 @@ def test_us01_validate_grade_input_below_range(self, request=None):
228255
def test_us01_validate_grade_input_above_range(self, request=None):
229256
test_name = request.node.name if request else self._testMethodName
230257
logger.info(f"Running test: {test_name}")
231-
self._initial_setup()
258+
# self._initial_setup() # Removed redundant call
232259

233260
initial_item_count = self._get_grades_list_item_count()
234261
self._add_grade_and_percentage("8.0", "20") # Invalid grade (above max 7)
@@ -242,7 +269,7 @@ def test_us01_validate_grade_input_above_range(self, request=None):
242269
def test_us01_validate_percentage_input_negative(self, request=None):
243270
test_name = request.node.name if request else self._testMethodName
244271
logger.info(f"Running test: {test_name}")
245-
self._initial_setup()
272+
# self._initial_setup() # Removed redundant call
246273

247274
initial_item_count = self._get_grades_list_item_count()
248275
self._add_grade_and_percentage("4.0", "-10") # Invalid percentage
@@ -256,7 +283,7 @@ def test_us01_validate_percentage_input_negative(self, request=None):
256283
def test_us01_validate_percentage_input_non_numeric(self, request=None):
257284
test_name = request.node.name if request else self._testMethodName
258285
logger.info(f"Running test: {test_name}")
259-
self._initial_setup()
286+
# self._initial_setup() # Removed redundant call
260287

261288
initial_item_count = self._get_grades_list_item_count()
262289
self._add_grade_and_percentage("4.0", "abc") # Non-numeric percentage

0 commit comments

Comments
 (0)