Skip to content

Commit

Permalink
test_state_inheritance: use polling cuz life too short for flaky tests
Browse files Browse the repository at this point in the history
kthnxbai ❤️
  • Loading branch information
masenf authored and benedikt-bartscher committed Feb 24, 2024
1 parent 3532d4e commit 733c67e
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions integration/test_state_inheritance.py
@@ -1,14 +1,29 @@
"""Test state inheritance."""

import time
from contextlib import suppress
from typing import Generator

import pytest
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By

from reflex.testing import DEFAULT_TIMEOUT, AppHarness, WebDriver


def get_alert_or_none(driver: WebDriver) -> Alert | None:
"""Switch to an alert if present.
Args:
driver: WebDriver instance.
Returns:
The alert if present, otherwise None.
"""
with suppress(NoAlertPresentException):
return driver.switch_to.alert


def raises_alert(driver: WebDriver, element: str) -> None:
"""Click an element and check that an alert is raised.
Expand All @@ -18,8 +33,8 @@ def raises_alert(driver: WebDriver, element: str) -> None:
"""
btn = driver.find_element(By.ID, element)
btn.click()
time.sleep(0.2) # wait for the alert to appear
alert = driver.switch_to.alert
alert = AppHarness._poll_for(lambda: get_alert_or_none(driver))
assert isinstance(alert, Alert)
assert alert.text == "clicked"
alert.accept()

Expand Down

0 comments on commit 733c67e

Please sign in to comment.