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
StaleElementReferenceExceptionby re-fetching WebElements. - Decorators for Caching: Use
@resilient_cached_webelementand@resilient_cached_webelementsto 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_webelementCaches a singleWebElement. If the element becomes stale, the proxy automatically re-fetches and updates the cached reference.@resilient_cached_webelementsCaches a list ofWebElements and re-fetches them if any element in the list becomes stale.
How They Work
-
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. -
Proxy Wrapping: The fetched element is wrapped in a proxy object. This proxy intercepts all attribute accesses and method calls.
-
Automatic Recovery: If a
StaleElementReferenceExceptionis encountered during any operation, the proxy uses the original locator function to re-fetch the element, ensuring that your tests continue running smoothly. -
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
Metadata
Release files for selocity 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| selocity-0.3.1.tar.gz | 5.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| selocity-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.4 kB
Release files / selocity-0.3.1.tar.gz
| Download URL | selocity-0.3.1.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
731d6f4e9d2c30b6d55bf76b3bb653d28603ea46f991e0cf94c5961ae0d66519
|
|
BLAKE2b-256 checksum How to use checksums |
a33e3e3c70ada986c9e7968f3552fcb43b3262eade4df7c02eb5555abd91be43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / selocity-0.3.1-py3-none-any.whl
| Download URL | selocity-0.3.1-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d6ae73b53a609f81d638e5dd57c0e1163f87a7ccf2dd87cf080999bfd7d74cc
|
|
BLAKE2b-256 checksum How to use checksums |
843c9e260f1dd0ea7a183e8d5f4018ba609abd3fd838b1e7e21192d7f3e5b482
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Security for selocity 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.