@@ -26,6 +26,7 @@ class HomePageTest(unittest.TestCase): # Changed base class
2626 FIRST_TIME_ALERT_BUTTON_SELECTOR = ".alert__button.alert__button--single" # For "Configurar" on first-time alert
2727 ALERT_OVERLAY_SELECTOR = "div.alert__overlay"
2828 NAV_BACK_BUTTON_XPATH = "//button[contains(@class, 'nav-bar__button') and .//span[contains(@class, 'back-icon')]/svg[contains(@class, 'lucide-chevron-left')]]"
29+ HOME_CONTAINER_SELECTOR = "div.home__container" # Selector for a main container on the home page
2930
3031 @classmethod
3132 def setUpClass (cls ):
@@ -70,59 +71,80 @@ def _take_screenshot(self, name_suffix):
7071 except Exception as e :
7172 logger .error (f"Error saving screenshot { screenshot_name } : { e } " )
7273
73- def _initial_setup (self ): # Removed driver argument
74- self .driver .get (self .BASE_URL ) # Use BASE_URL
75-
76- first_time_alert_handled = False
74+ def _initial_setup (self ):
75+ self .driver .get (self .BASE_URL )
76+ logger . info ( f"Navigated to base URL: { self . BASE_URL } " )
77+
7778 try :
79+ # 1. Attempt to handle the first-time alert
7880 logger .info (f"Attempting to handle first-time alert with button '{ self .FIRST_TIME_ALERT_BUTTON_SELECTOR } '." )
79- alert_button = WebDriverWait (self .driver , 5 ).until ( # Using wait_short's timeout
81+ alert_button = WebDriverWait (self .driver , 10 ).until (
8082 EC .element_to_be_clickable ((By .CSS_SELECTOR , self .FIRST_TIME_ALERT_BUTTON_SELECTOR ))
8183 )
8284 alert_button .click ()
83- logger .info (f"Clicked first-time alert button: '{ self .FIRST_TIME_ALERT_BUTTON_SELECTOR } '." );
85+ logger .info (f"Clicked first-time alert button: '{ self .FIRST_TIME_ALERT_BUTTON_SELECTOR } '." )
8486
85- # Wait for the overlay to become invisible
86- WebDriverWait (self .driver , 5 ).until ( # Using wait_short's timeout
87+ WebDriverWait (self .driver , 5 ).until (
8788 EC .invisibility_of_element_located ((By .CSS_SELECTOR , self .ALERT_OVERLAY_SELECTOR ))
8889 )
89- logger .info (f"Alert overlay '{ self .ALERT_OVERLAY_SELECTOR } ' is no longer visible." )
90- first_time_alert_handled = True ;
91-
92- except TimeoutException :
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
95- except Exception as e :
96- logger .warning (f"An unexpected error occurred while trying to handle first-time alert: { e } " )
97- self ._take_screenshot ("error_initial_alert_handling" )
90+ logger .info (f"Alert overlay '{ self .ALERT_OVERLAY_SELECTOR } ' is no longer visible. App should be on Settings page." )
9891
99- if first_time_alert_handled :
92+ # 2. Attempt to navigate back from Settings page to Home page
10093 try :
101- logger .info ("First-time alert was handled, app should be on Settings page. Attempting to navigate back to Home ." )
94+ logger .info ("Attempting to navigate back from Settings to Home using nav-bar back button ." )
10295 nav_back_button = WebDriverWait (self .driver , 10 ).until (
10396 EC .element_to_be_clickable ((By .XPATH , self .NAV_BACK_BUTTON_XPATH ))
10497 )
10598 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
99+ logger .info ("Clicked navigation bar 'Atras' button." )
100+
101+ # Wait for an element that indicates we are back on the Home page
102+ WebDriverWait (self .driver , 10 ).until (
103+ EC .presence_of_element_located ((By .CSS_SELECTOR , self .HOME_CONTAINER_SELECTOR ))
104+ )
105+ logger .info (f"Successfully navigated back to Home page (found '{ self .HOME_CONTAINER_SELECTOR } ')." )
106+ except Exception as nav_exc :
107+ logger .error (f"Navigation back to Home page failed: { nav_exc } . Attempting recovery." )
108+ self ._take_screenshot ("error_nav_back_failed_recovery_attempt" )
109+ # Recovery: Go back to base URL and try to click alert again (if it reappears or if it was a different issue)
110+ self .driver .get (self .BASE_URL )
111+ logger .info (f"Recovery: Navigated back to base URL: { self .BASE_URL } " )
112+ try :
113+ # This alert click might not be necessary if localStorage is already set,
114+ # but follows the pattern of the user's original recovery logic.
115+ alert_button_recovery = WebDriverWait (self .driver , 5 ).until (
116+ EC .element_to_be_clickable ((By .CSS_SELECTOR , self .FIRST_TIME_ALERT_BUTTON_SELECTOR ))
117+ )
118+ alert_button_recovery .click ()
119+ logger .info ("Recovery: Clicked first-time alert button again." )
120+ WebDriverWait (self .driver , 5 ).until (
121+ EC .invisibility_of_element_located ((By .CSS_SELECTOR , self .ALERT_OVERLAY_SELECTOR ))
122+ )
123+ logger .info ("Recovery: Alert overlay is no longer visible after second attempt." )
124+ # After this, the final verification for GRADE_INPUT_SELECTOR will run.
125+ # If this recovery path is taken, and we are not on settings, the next check for GRADE_INPUT_SELECTOR should pass.
126+ # If we are on settings, the check will fail, which is an unrecoverable state for this setup.
127+ except TimeoutException :
128+ logger .info ("Recovery: First-time alert button not found on second attempt. Assuming Home page or non-first-time state." )
129+ except Exception as recovery_alert_exc :
130+ logger .warning (f"Recovery: Error clicking alert button on second attempt: { recovery_alert_exc } " )
131+
132+ except TimeoutException :
133+ logger .info (f"First-time alert (button '{ self .FIRST_TIME_ALERT_BUTTON_SELECTOR } ') not found. Assuming not first time or alert already dismissed." )
134+ # No action needed if the alert isn't there, proceed to final verification.
135+ except Exception as e :
136+ logger .warning (f"An unexpected error occurred during initial alert handling phase: { e } " )
137+ self ._take_screenshot ("error_initial_alert_phase" )
118138
119139 # Final verification: Ensure the Home page grade input is present
120140 try :
121- self .wait_long .until (EC .presence_of_element_located ((By .CSS_SELECTOR , self .GRADE_INPUT_SELECTOR )))
141+ WebDriverWait (self .driver , 15 ).until ( # Using self.wait_long effectively
142+ EC .presence_of_element_located ((By .CSS_SELECTOR , self .GRADE_INPUT_SELECTOR ))
143+ )
122144 logger .info (f"Grade input ('{ self .GRADE_INPUT_SELECTOR } ') found on page. Setup complete." )
123145 except TimeoutException :
124146 current_url = self .driver .current_url
125- logger .error (f"Failed to find grade input ('{ self .GRADE_INPUT_SELECTOR } ') after setup attempts. Current URL: { current_url } " )
147+ logger .error (f"FINAL SETUP FAILURE: Failed to find grade input ('{ self .GRADE_INPUT_SELECTOR } ') after all attempts. Current URL: { current_url } " )
126148 self ._take_screenshot ("error_final_grade_input_not_found" )
127149 raise Exception (f"Could not find the grade input ('{ self .GRADE_INPUT_SELECTOR } ') after setup. Current URL: { current_url } " )
128150
0 commit comments