Skip to main content

Selocity is a Python library that enhances Selenium's reliability.

Project description

Selocity

Selocity is a Python library that enhances Selenium's reliability by automatically handling stale elements. With its caching decorators and proxy-based approach, your Selenium tests become more robust and less prone to flakiness caused by dynamic web pages.

Features

  • Automatic Re-Fetching: Seamlessly recovers from StaleElementReferenceException by re-fetching WebElements.
  • Decorators for Caching: Use @resilient_cached_webelement and @resilient_cached_webelements to cache elements or lists of elements.
  • Proxy-Based Wrapping: Every WebElement is wrapped in a proxy that logs attribute accesses and method calls.
  • Retry Mechanism: Uses a polling strategy with configurable intervals and timeouts to ensure element stability.

Installation

Install Selocity directly from our GitLab artifact repository. You can add the following to your requirements.txt:

--extra-index-url https://gitlab.developers.cam.ac.uk/api/v4/projects/10297/packages/pypi/simple

selocity==0.2.0

Or install it via pip:

pip install git+https://gitlab.developers.cam.ac.uk/uis/qa/selocity.git#egg=selocity

Usage

Selocity provides two main decorators:

  • @resilient_cached_webelement Caches a single WebElement. If the element becomes stale, the proxy automatically re-fetches and updates the cached reference.
  • @resilient_cached_webelements Caches a list of WebElements and re-fetches them if any element in the list becomes stale.

How They Work

  1. Caching on First Access: When you first access a decorated element method, the library attempts to fetch the element. If it isn’t immediately available (raising a NoSuchElementException), it retries until the element is found or a timeout is reached.

  2. Proxy Wrapping: The fetched element is wrapped in a proxy object. This proxy intercepts all attribute accesses and method calls.

  3. Automatic Recovery: If a StaleElementReferenceException is encountered during any operation, the proxy uses the original locator function to re-fetch the element, ensuring that your tests continue running smoothly.

  4. Logging and Debugging: Detailed logs help you understand when and why elements are being re-fetched, which aids in debugging flaky tests.

Benefits

  • Improved Test Stability: Automatically handles dynamic changes in the DOM.
  • Reduced Flakiness: Ensures that transient issues with stale elements don’t break your tests.
  • Seamless POM Integration: Easily integrate with your Page Object Model (POM) without significant code changes.
  • Efficient Error Recovery: Retries operations with a built-in polling mechanism, making your tests more resilient.

Example: Basic Page Object Model (POM)

Below is a simple example showing how to use the decorators in a basic POM structure:

from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selocity import resilient_cached_webelement, resilient_cached_webelements

class BasePage:
    def __init__(self, driver):
        self.driver = driver

class LoginPage(BasePage):
    @property
    @resilient_cached_webelement
    def username_input(self) -> WebElement:
        return self.driver.find_element(By.ID, "username")

    @property
    @resilient_cached_webelement
    def password_input(self) -> WebElement:
        return self.driver.find_element(By.ID, "password")

    @property
    @resilient_cached_webelement
    def submit_button(self) -> WebElement:
        return self.driver.find_element(By.CSS_SELECTOR, "button[type='submit']")

    def login(self, username: str, password: str):
        self.username_input.send_keys(username)
        self.password_input.send_keys(password)
        self.submit_button.click()

Usage in your test code:

from selenium import webdriver

def test_login():
    driver = webdriver.Chrome()
    login_page = LoginPage(driver)
    login_page.login("myusername", "mypassword")

Contributing

Contributions are welcome! Please open issues or submit pull requests on our GitLab repository.

Contact

For questions or support, please contact jsa34@cam.ac.uk

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

selocity-0.3.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

selocity-0.3.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file selocity-0.3.1.tar.gz.

File metadata

  • Download URL: selocity-0.3.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for selocity-0.3.1.tar.gz
Algorithm Hash digest
SHA256 731d6f4e9d2c30b6d55bf76b3bb653d28603ea46f991e0cf94c5961ae0d66519
MD5 0cab715879fb8d0a906e3d66699756b6
BLAKE2b-256 a33e3e3c70ada986c9e7968f3552fcb43b3262eade4df7c02eb5555abd91be43

See more details on using hashes here.

File details

Details for the file selocity-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: selocity-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for selocity-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d6ae73b53a609f81d638e5dd57c0e1163f87a7ccf2dd87cf080999bfd7d74cc
MD5 98161815864f179cd6c30e1e35a109f9
BLAKE2b-256 843c9e260f1dd0ea7a183e8d5f4018ba609abd3fd838b1e7e21192d7f3e5b482

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page