Based on my analysis of the DOM Testing Library JavaScript codebase, here's a plan for a simple Python port using tdom.Node types:
The library would provide accessibility-focused query functions that work with tdom's Node, Element, Text, and
Fragment types. The main API would follow DOM Testing Library's philosophy: "The more your tests resemble the way
your software is used, the more confidence they can give you."
- Query elements by ARIA role (button, textbox, heading, etc.)
- Support role attributes like
name,description,level(for headings) - Use Python's
aria-queryequivalent or custom role mapping - Try to match the W3C specification on ARIA roles
- Find elements containing specific text content
- Support exact/fuzzy matching with normalization options
- Handle text content extraction from tdom nodes
- Automatically call
.strip()on string values
- Find form controls by their associated label
- Support
<label>elements andaria-labelledby - Automatically call
.strip()on string values
- Query form inputs by placeholder attribute
- Automatically call
.strip()on string values
- Find elements by
data-testidattribute - Configurable test ID attribute name
from tdom import Node, Element
from typing import Optional, Union, Callable, Pattern
from enum import Enum
class QueryVariant(Enum):
GET = "get" # Returns single element, throws if not found/multiple
QUERY = "query" # Returns single element or None
FIND = "find" # Async version with waiting
GET_ALL = "get_all" # Returns list, throws if empty
QUERY_ALL = "query_all" # Returns list (can be empty)
FIND_ALL = "find_all" # Async list version
# Core query functions
def get_by_role(container: Node, role: str, *, name: Optional[str] = None,
level: Optional[int] = None, **kwargs) -> Element: ...
def query_by_text(container: Node, text: Union[str, Pattern], *,
exact: bool = True, normalize: bool = True) -> Optional[Element]: ...
# Convenience function that binds all queries to a container
def within(container: Node) -> QueryMethods: ...- Text extraction - Function to get text content from tdom nodes
- Attribute querying - Generic attribute-based element finding
- Normalization - Text matching with whitespace/case handling
- Error messages - Helpful debugging when queries fail
- By Role - Most important for accessibility testing
- By Text - Universal content-based queries
- By Test ID - Escape hatch for difficult cases
- By Label Text - Critical for form testing
- By Placeholder - Input testing convenience
- By Display Value - Current form field values
- Multiple element variants (
get_all_by_*) - Waiting/async queries (
find_by_*) - Custom matchers - Regex and function-based matching
- Pretty printing - Enhanced error messages with DOM context
- Pure Python - No browser/DOM dependencies, works with tdom trees
- Type safety - Full type hints using tdom's Node types
- Accessibility first - Prioritize ARIA-aware queries over CSS selectors
- Simple API - Follow DOM Testing Library conventions closely
- Configurable - Allow customization of text normalization, test ID attributes
- Error-friendly - Clear error messages suggesting alternative queries
This approach provides a lightweight, accessibility-focused testing library that integrates naturally with tdom's component-based architecture while maintaining the proven patterns from the JavaScript ecosystem.