Skip to content

Commit ba1ae3c

Browse files
test: enhance initial setup in TestHomePage to handle alerts and navigation more robustly
1 parent bbdc712 commit ba1ae3c

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

tests/test_home_page.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,58 @@ class TestHomePage:
1010
def _initial_setup(self, driver):
1111
base_url = os.environ.get('APP_URL', 'http://localhost:3000')
1212
driver.get(base_url)
13+
time.sleep(1) # Allow a brief moment for initial rendering
14+
15+
# 1. Handle initial alert (if any)
1316
try:
14-
# Wait for and click the initial alert button if present
1517
alert_button = WebDriverWait(driver, 10).until(
1618
EC.element_to_be_clickable((By.CSS_SELECTOR, ".alert__button.alert__button--single"))
1719
)
1820
alert_button.click()
21+
# Wait for the alert to disappear or for a subsequent element if needed
22+
WebDriverWait(driver, 5).until(EC.staleness_of(alert_button))
23+
print("Initial alert handled.")
1924
except TimeoutException:
20-
print("Initial alert not found or not clickable within 10s, proceeding with test.")
25+
print("Initial alert not found or not clickable within 10s.")
2126
except Exception as e:
22-
print(f"An unexpected error occurred during initial alert handling: {e}, proceeding with test.")
27+
print(f"Error clicking initial alert: {e}, proceeding.")
28+
29+
# 2. Check if we are on the main page (with grade-input) or need to navigate back
30+
try:
31+
# Try to find a main page element immediately. If present, we are good.
32+
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "grade-input")))
33+
print("Already on the main page with grade input.")
34+
return # Already on the correct page
35+
except TimeoutException:
36+
print("Grade input not immediately visible. Attempting navigation back from potential settings/initial page.")
37+
try:
38+
# If not on main page, try to click a common 'back' or 'home' button
39+
# Using ".nav-bar__button" from the original test_home_page_functional_flow
40+
back_button = WebDriverWait(driver, 10).until(
41+
EC.element_to_be_clickable((By.CSS_SELECTOR, ".nav-bar__button"))
42+
)
43+
back_button.click()
44+
print("Clicked .nav-bar__button.")
45+
# Wait for the main page element to ensure navigation was successful
46+
WebDriverWait(driver, 10).until(
47+
EC.presence_of_element_located((By.ID, "grade-input"))
48+
)
49+
print("Successfully navigated to the main page with grade-input after clicking back.")
50+
except TimeoutException:
51+
print("Failed to find grade-input after attempting back navigation.")
52+
os.makedirs("screenshots", exist_ok=True)
53+
driver.save_screenshot(f"screenshots/error_initial_setup_navigation_timeout_{int(time.time())}.png")
54+
raise Exception("Could not navigate to the main page with grade-input during setup.")
55+
except Exception as e_nav:
56+
print(f"Error during back navigation attempt: {e_nav}")
57+
os.makedirs("screenshots", exist_ok=True)
58+
driver.save_screenshot(f"screenshots/error_initial_setup_navigation_exception_{int(time.time())}.png")
59+
raise e_nav
60+
except Exception as e_main_check:
61+
print(f"Unexpected error during initial page check: {e_main_check}")
62+
os.makedirs("screenshots", exist_ok=True)
63+
driver.save_screenshot(f"screenshots/error_initial_setup_main_check_exception_{int(time.time())}.png")
64+
raise e_main_check
2365

2466
def _add_grade_and_percentage(self, driver, grade, percentage):
2567
grade_input = WebDriverWait(driver, 10).until(

0 commit comments

Comments
 (0)