Skip to content

Wait conditions

Wait conditions in Selenium are mechanisms that allow to wait for certain conditions before performing the next actions. They are used for synchronization between the test and the web application to ensure that elements or pages are fully loaded and ready for interaction. This helps to avoid errors related to elements not yet appearing on the page or not being fully loaded.

Pomcorn includes several custom waiting conditions that complement the built-in Selenium waiting list. Read about Selenium wait conditions.

Package wait conditions

element_not_exists_in_dom(element)

Represent wait condition to check that element not exists in DOM.

Source code in pomcorn/waits_conditions.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def element_not_exists_in_dom(
    element: PomcornElement[TLocator_co] | TLocator_co,
) -> Callable[[WebDriver], bool]:
    """Represent `wait condition` to check that element not exists in DOM."""

    def check_the_match(driver: WebDriver) -> bool:
        target = element

        if isinstance(target, PomcornElement):
            return not target.exists_in_dom

        try:
            # Check if driver can find element.
            driver.find_element(*target)
        except (NoSuchElementException, StaleElementReferenceException):
            # In the case of NoSuchElement, returns true because the element is
            # not present in DOM.
            # In the case of StaleElementReference, returns true because stale
            # element reference implies that element is no longer visible.
            return True
        # We return `False` because if `driver` can find element without
        # exception, that means element exists in the DOM.
        return False

    return check_the_match

text_in_element_changes(element, old_text)

Represent wait condition to check that text doesn't match old text.

Parameters:

Name Type Description Default
element PomcornElement[TLocator_co] | TLocator_co

The element in which the text will be checked.

required
old_text str

The old text that should disappear.

required
Source code in pomcorn/waits_conditions.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def text_in_element_changes(
    element: PomcornElement[TLocator_co] | TLocator_co,
    old_text: str,
) -> Callable[[WebDriver], bool]:
    """Represent `wait condition` to check that text doesn't match old text.

    Args:
        element: The element in which the text will be checked.
        old_text: The old text that should disappear.

    """

    def check_the_match(driver: WebDriver) -> bool:
        target = element

        if isinstance(target, PomcornElement):
            return old_text != target.get_text()

        return old_text != driver.find_element(*target).text

    return check_the_match

text_not_to_be_present_in_element_attribute(locator, attribute_, text_)

Represent wait condition that checks that text is not in attribute.

NOTE: "_" in argument names added for consistent with selenium's built-in wait condition text_to_be_present_in_element_attribute

Parameters:

Name Type Description Default
locator tuple[str, str]

A tuple with a By instance and a string value. You can get them from the Locators classes.

required
attribute_ str

Attribute name.

required
text_ str

Text that should disappear.

required
Source code in pomcorn/waits_conditions.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def text_not_to_be_present_in_element_attribute(
    locator: tuple[str, str],
    attribute_: str,
    text_: str,
) -> Callable[[WebDriverOrWebElement], bool]:
    """Represent `wait condition` that checks that text is not in attribute.

    NOTE: "_" in argument names added for consistent with selenium's built-in
    wait condition `text_to_be_present_in_element_attribute`

    Args:
        locator: A tuple with a By instance and a string value. You can get
            them from the Locators classes.
        attribute_: Attribute name.
        text_: Text that should disappear.

    """

    def check_the_match(driver: WebDriverOrWebElement) -> bool:
        try:
            attribute_text = driver.find_element(*locator).get_attribute(
                name=attribute_,
            )
            if attribute_text is None:
                # If attribute is empty, it means that it contains no text.
                return True
            return text_ not in attribute_text
        except StaleElementReferenceException:
            # If element doesn't exist, it means that its attribute doesn't
            # contain text.
            return True

    return check_the_match

url_not_matches(pattern)

Represent wait condition to check that url doesn't match pattern.

Source code in pomcorn/waits_conditions.py
19
20
21
22
23
24
25
def url_not_matches(pattern: str) -> Callable[[WebDriver], bool]:
    """Represent `wait condition` to check that url doesn't match pattern."""

    def check_the_match(driver: WebDriver) -> bool:
        return re.search(pattern, driver.current_url) is None

    return check_the_match