Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait method #59

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pytest_logikal/browser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from typing import Any

from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from termcolor import colored
from xdg_base_dirs import xdg_cache_home

Expand Down Expand Up @@ -189,6 +191,26 @@ def replace_text(self, element: Any, text: str) -> None:
script = f'arguments[0].innerHTML = "{text}"'
self.execute_script(script, element) # type: ignore[no-untyped-call]

def wait_for_element(
self,
by: str,
value: str,
timeout_seconds: int = 10,
poll_frequency: float = 0.5,
) -> None:
"""
Wait until a given element is present.

Args:
by: The selector type to use for locating the element.
value: The selector value to use for locating the element.
timeout_seconds: The maximal time to wait.
poll_frequency: Sleep interval between checks.

"""
wait = WebDriverWait(driver=self, timeout=timeout_seconds, poll_frequency=poll_frequency)
wait.until(expected_conditions.presence_of_element_located((by, value)))

def login(self, user: Any, force: bool = True) -> None:
"""
.. note:: The ``django`` extra must be installed for this method to work.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/pytest_logikal/browser/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def test_replace_text(live_url: LiveURL, browser: Browser) -> None:
browser.check()


@set_browser(scenarios.desktop)
def test_wait(live_url: LiveURL, browser: Browser) -> None:
browser.get(live_url())
browser.wait_for_element(By.CSS_SELECTOR, 'h1')
browser.check()


@set_browser(scenarios.desktop)
def test_login(live_url: LiveURL, browser: Browser, user: User) -> None:
browser.get(live_url('internal'))
Expand Down