|
|
|
|
@@ -12,6 +12,14 @@ from starlette.testclient import TestClient
|
|
|
|
|
|
|
|
|
|
from myfasthtml.core.commands import mount_commands
|
|
|
|
|
|
|
|
|
|
verbs = {
|
|
|
|
|
'hx_get': 'GET',
|
|
|
|
|
'hx_post': 'POST',
|
|
|
|
|
'hx_put': 'PUT',
|
|
|
|
|
'hx_delete': 'DELETE',
|
|
|
|
|
'hx_patch': 'PATCH',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class MyFT:
|
|
|
|
|
@@ -29,7 +37,7 @@ class TestableElement:
|
|
|
|
|
or verifying element properties.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, client, source):
|
|
|
|
|
def __init__(self, client, source, tag=None):
|
|
|
|
|
"""
|
|
|
|
|
Initialize a testable element.
|
|
|
|
|
|
|
|
|
|
@@ -39,17 +47,34 @@ class TestableElement:
|
|
|
|
|
"""
|
|
|
|
|
self.client = client
|
|
|
|
|
if isinstance(source, str):
|
|
|
|
|
self.html_fragment = source
|
|
|
|
|
tag = BeautifulSoup(source, 'html.parser').find()
|
|
|
|
|
self.ft = MyFT(tag.name, tag.attrs)
|
|
|
|
|
self.html_fragment = source.strip()
|
|
|
|
|
elif isinstance(source, Tag):
|
|
|
|
|
self.html_fragment = str(source)
|
|
|
|
|
self.ft = MyFT(source.name, source.attrs)
|
|
|
|
|
self.html_fragment = str(source).strip()
|
|
|
|
|
elif isinstance(source, FT):
|
|
|
|
|
self.ft = source
|
|
|
|
|
self.html_fragment = to_xml(source).strip()
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(f"Invalid source '{source}' for TestableElement.")
|
|
|
|
|
|
|
|
|
|
self.tag, self.element, self.my_ft = self._parse(tag, self.html_fragment)
|
|
|
|
|
self.fields_mapping = {} # link between the input label and the input name
|
|
|
|
|
self.fields = {} # Values of the fields {name: value}
|
|
|
|
|
self.select_fields = {} # list of possible options for 'select' input fields
|
|
|
|
|
|
|
|
|
|
self._update_fields_mapping()
|
|
|
|
|
self._update_fields()
|
|
|
|
|
|
|
|
|
|
def fill(self, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Fill the form with the given data.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
**kwargs: Field names and their values to fill in the form.
|
|
|
|
|
"""
|
|
|
|
|
for name, value in kwargs.items():
|
|
|
|
|
field_name = self._translate(name)
|
|
|
|
|
if field_name not in self.fields:
|
|
|
|
|
raise ValueError(f"Invalid field name '{name}'.")
|
|
|
|
|
self.fields[self._translate(name)] = value
|
|
|
|
|
|
|
|
|
|
def click(self):
|
|
|
|
|
"""Click the element (to be implemented)."""
|
|
|
|
|
@@ -59,6 +84,26 @@ class TestableElement:
|
|
|
|
|
"""Check if element matches given FastHTML element (to be implemented)."""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def _translate(self, field):
|
|
|
|
|
"""
|
|
|
|
|
Translate a given field using a predefined mapping. If the field is not found
|
|
|
|
|
in the mapping, the original field is returned unmodified.
|
|
|
|
|
|
|
|
|
|
:param field: The field name to be translated.
|
|
|
|
|
:type field: str
|
|
|
|
|
:return: The translated field name if present in the mapping, or the original
|
|
|
|
|
field name if no mapping exists for it.
|
|
|
|
|
:rtype: str
|
|
|
|
|
"""
|
|
|
|
|
return self.fields_mapping.get(field, field)
|
|
|
|
|
|
|
|
|
|
def _support_htmx(self):
|
|
|
|
|
"""Check if the element supports HTMX."""
|
|
|
|
|
return ('hx_get' in self.my_ft.attrs or
|
|
|
|
|
'hx-get' in self.my_ft.attrs or
|
|
|
|
|
'hx_post' in self.my_ft.attrs or
|
|
|
|
|
'hx-post' in self.my_ft.attrs)
|
|
|
|
|
|
|
|
|
|
def _send_htmx_request(self, json_data: dict | None = None, data: dict | None = None) -> Response:
|
|
|
|
|
"""
|
|
|
|
|
Simulates an HTMX request in Python for unit testing.
|
|
|
|
|
@@ -83,16 +128,13 @@ class TestableElement:
|
|
|
|
|
method = "GET" # HTMX defaults to GET if not specified
|
|
|
|
|
url = None
|
|
|
|
|
|
|
|
|
|
verbs = {
|
|
|
|
|
'hx_get': 'GET',
|
|
|
|
|
'hx_post': 'POST',
|
|
|
|
|
'hx_put': 'PUT',
|
|
|
|
|
'hx_delete': 'DELETE',
|
|
|
|
|
'hx_patch': 'PATCH',
|
|
|
|
|
}
|
|
|
|
|
if data is not None:
|
|
|
|
|
headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
|
|
|
elif json_data is not None:
|
|
|
|
|
headers['Content-Type'] = 'application/json'
|
|
|
|
|
|
|
|
|
|
# .props contains the kwargs passed to the object (e.g., hx_post="/url")
|
|
|
|
|
element_attrs = self.ft.attrs or {}
|
|
|
|
|
element_attrs = self.my_ft.attrs or {}
|
|
|
|
|
|
|
|
|
|
# Build the attributes
|
|
|
|
|
for key, value in element_attrs.items():
|
|
|
|
|
@@ -124,180 +166,13 @@ class TestableElement:
|
|
|
|
|
# Sanity check
|
|
|
|
|
if url is None:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"The <{self.ft.tag}> element has no HTMX verb attribute "
|
|
|
|
|
f"The <{self.my_ft.tag}> element has no HTMX verb attribute "
|
|
|
|
|
"(e.g., hx_get, hx_post) to define a URL."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Send the request
|
|
|
|
|
return self.client.send_request(method, url, headers=headers, data=data, json_data=json_data)
|
|
|
|
|
|
|
|
|
|
def _support_htmx(self):
|
|
|
|
|
"""Check if the element supports HTMX."""
|
|
|
|
|
return ('hx_get' in self.ft.attrs or
|
|
|
|
|
'hx-get' in self.ft.attrs or
|
|
|
|
|
'hx_post' in self.ft.attrs or
|
|
|
|
|
'hx-post' in self.ft.attrs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestableForm(TestableElement):
|
|
|
|
|
"""
|
|
|
|
|
Represents an HTML form that can be filled and submitted in tests.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, client, source):
|
|
|
|
|
"""
|
|
|
|
|
Initialize a testable form.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
client: The MyTestClient instance.
|
|
|
|
|
source: The source HTML string containing a form.
|
|
|
|
|
"""
|
|
|
|
|
super().__init__(client, source)
|
|
|
|
|
self.form = BeautifulSoup(self.html_fragment, 'html.parser').find('form')
|
|
|
|
|
self.fields_mapping = {} # link between the input label and the input name
|
|
|
|
|
self.fields = {} # field name; field value
|
|
|
|
|
self.select_fields = {} # list of possible options for 'select' input fields
|
|
|
|
|
|
|
|
|
|
self._update_fields_mapping()
|
|
|
|
|
self.update_fields()
|
|
|
|
|
|
|
|
|
|
def update_fields(self):
|
|
|
|
|
"""
|
|
|
|
|
Update the fields dictionary with current form values and their proper types.
|
|
|
|
|
|
|
|
|
|
This method processes all input and select elements in the form:
|
|
|
|
|
- Determines the appropriate Python type (str, int, float, bool) based on
|
|
|
|
|
the HTML input type attribute and/or the value itself
|
|
|
|
|
- For select elements, populates self.select_fields with available options
|
|
|
|
|
- Stores the final typed values in self.fields
|
|
|
|
|
|
|
|
|
|
Type conversion priority:
|
|
|
|
|
1. HTML type attribute (checkbox → bool, number → int/float, etc.)
|
|
|
|
|
2. Value analysis fallback for ambiguous types (text/hidden/absent type)
|
|
|
|
|
"""
|
|
|
|
|
self.fields = {}
|
|
|
|
|
self.select_fields = {}
|
|
|
|
|
|
|
|
|
|
# Process input fields
|
|
|
|
|
for input_field in self.form.find_all('input'):
|
|
|
|
|
name = input_field.get('name')
|
|
|
|
|
if not name:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
input_type = input_field.get('type', 'text').lower()
|
|
|
|
|
raw_value = input_field.get('value', '')
|
|
|
|
|
|
|
|
|
|
# Type conversion based on input type
|
|
|
|
|
if input_type == 'checkbox':
|
|
|
|
|
# Checkbox: bool based on 'checked' attribute
|
|
|
|
|
self.fields[name] = input_field.has_attr('checked')
|
|
|
|
|
|
|
|
|
|
elif input_type == 'radio':
|
|
|
|
|
# Radio: str value (only if checked)
|
|
|
|
|
if input_field.has_attr('checked'):
|
|
|
|
|
self.fields[name] = raw_value
|
|
|
|
|
elif name not in self.fields:
|
|
|
|
|
# If no radio is checked yet, don't set a default
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
elif input_type == 'number':
|
|
|
|
|
# Number: int or float based on value
|
|
|
|
|
self.fields[name] = self._convert_number(raw_value)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# Other types (text, hidden, email, password, etc.): analyze value
|
|
|
|
|
self.fields[name] = self._convert_value(raw_value)
|
|
|
|
|
|
|
|
|
|
# Process select fields
|
|
|
|
|
for select_field in self.form.find_all('select'):
|
|
|
|
|
name = select_field.get('name')
|
|
|
|
|
if not name:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Extract all options
|
|
|
|
|
options = []
|
|
|
|
|
selected_value = None
|
|
|
|
|
|
|
|
|
|
for option in select_field.find_all('option'):
|
|
|
|
|
option_value = option.get('value', option.get_text(strip=True))
|
|
|
|
|
option_text = option.get_text(strip=True)
|
|
|
|
|
|
|
|
|
|
options.append({
|
|
|
|
|
'value': option_value,
|
|
|
|
|
'text': option_text
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# Track selected option
|
|
|
|
|
if option.has_attr('selected'):
|
|
|
|
|
selected_value = option_value
|
|
|
|
|
|
|
|
|
|
# Store options list
|
|
|
|
|
self.select_fields[name] = options
|
|
|
|
|
|
|
|
|
|
# Store selected value (or first option if none selected)
|
|
|
|
|
if selected_value is not None:
|
|
|
|
|
self.fields[name] = selected_value
|
|
|
|
|
elif options:
|
|
|
|
|
self.fields[name] = options[0]['value']
|
|
|
|
|
|
|
|
|
|
def fill(self, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Fill the form with the given data.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
**kwargs: Field names and their values to fill in the form.
|
|
|
|
|
"""
|
|
|
|
|
for name, value in kwargs.items():
|
|
|
|
|
field_name = self.translate(name)
|
|
|
|
|
if field_name not in self.fields:
|
|
|
|
|
raise ValueError(f"Invalid field name '{name}'.")
|
|
|
|
|
self.fields[self.translate(name)] = value
|
|
|
|
|
|
|
|
|
|
def submit(self):
|
|
|
|
|
"""
|
|
|
|
|
Submit the form.
|
|
|
|
|
|
|
|
|
|
This method handles both HTMX-enabled forms and classic HTML form submissions:
|
|
|
|
|
- If the form supports HTMX (has hx_post, hx_get, etc.), uses HTMX request
|
|
|
|
|
- Otherwise, simulates a classic browser form submission using the form's
|
|
|
|
|
action and method attributes
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The response from the form submission.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ValueError: If the form has no action attribute for classic submission.
|
|
|
|
|
"""
|
|
|
|
|
# Check if the form supports HTMX
|
|
|
|
|
if self._support_htmx():
|
|
|
|
|
return self._send_htmx_request(data=self.fields)
|
|
|
|
|
|
|
|
|
|
# Classic form submission
|
|
|
|
|
action = self.form.get('action')
|
|
|
|
|
if not action or action.strip() == '':
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"The form has no 'action' attribute. "
|
|
|
|
|
"Cannot submit a classic form without a target URL."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
method = self.form.get('method', 'post').upper()
|
|
|
|
|
|
|
|
|
|
# Prepare headers for classic form submission
|
|
|
|
|
headers = {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Send the request via the client
|
|
|
|
|
return self.client.send_request(
|
|
|
|
|
method=method,
|
|
|
|
|
url=action,
|
|
|
|
|
headers=headers,
|
|
|
|
|
data=self.fields
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def translate(self, field):
|
|
|
|
|
return self.fields_mapping.get(field, field)
|
|
|
|
|
|
|
|
|
|
def _update_fields_mapping(self):
|
|
|
|
|
"""
|
|
|
|
|
Build a mapping between label text and input field names.
|
|
|
|
|
@@ -319,16 +194,16 @@ class TestableForm(TestableElement):
|
|
|
|
|
unnamed_counter = 0
|
|
|
|
|
|
|
|
|
|
# Get all inputs in the form
|
|
|
|
|
all_inputs = self.form.find_all('input')
|
|
|
|
|
all_inputs = self.element.find_all('input')
|
|
|
|
|
|
|
|
|
|
# Priority 1 & 2: Explicit association (for/id) and implicit (nested)
|
|
|
|
|
for label in self.form.find_all('label'):
|
|
|
|
|
for label in self.element.find_all('label'):
|
|
|
|
|
label_text = label.get_text(strip=True)
|
|
|
|
|
|
|
|
|
|
# Check for explicit association via 'for' attribute
|
|
|
|
|
label_for = label.get('for')
|
|
|
|
|
if label_for:
|
|
|
|
|
input_field = self.form.find('input', id=label_for)
|
|
|
|
|
input_field = self.element.find('input', id=label_for)
|
|
|
|
|
if input_field:
|
|
|
|
|
input_name = self._get_input_identifier(input_field, unnamed_counter)
|
|
|
|
|
if input_name.startswith('unnamed_'):
|
|
|
|
|
@@ -348,7 +223,7 @@ class TestableForm(TestableElement):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Priority 3 & 4: Parent-level associations
|
|
|
|
|
for label in self.form.find_all('label'):
|
|
|
|
|
for label in self.element.find_all('label'):
|
|
|
|
|
label_text = label.get_text(strip=True)
|
|
|
|
|
|
|
|
|
|
# Skip if this label was already processed
|
|
|
|
|
@@ -391,6 +266,89 @@ class TestableForm(TestableElement):
|
|
|
|
|
unnamed_counter += 1
|
|
|
|
|
self.fields_mapping[input_name] = input_name
|
|
|
|
|
|
|
|
|
|
def _update_fields(self):
|
|
|
|
|
"""
|
|
|
|
|
Update the fields dictionary with current form values and their proper types.
|
|
|
|
|
|
|
|
|
|
This method processes all input and select elements in the form:
|
|
|
|
|
- Determines the appropriate Python type (str, int, float, bool) based on
|
|
|
|
|
the HTML input type attribute and/or the value itself
|
|
|
|
|
- For select elements, populates self.select_fields with available options
|
|
|
|
|
- Stores the final typed values in self.fields
|
|
|
|
|
|
|
|
|
|
Type conversion priority:
|
|
|
|
|
1. HTML type attribute (checkbox → bool, number → int/float, etc.)
|
|
|
|
|
2. Value analysis fallback for ambiguous types (text/hidden/absent type)
|
|
|
|
|
"""
|
|
|
|
|
self.fields = {}
|
|
|
|
|
self.select_fields = {}
|
|
|
|
|
|
|
|
|
|
# Process input fields
|
|
|
|
|
for input_field in self.element.find_all('input'):
|
|
|
|
|
name = input_field.get('name')
|
|
|
|
|
if not name:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
input_type = input_field.get('type', 'text').lower()
|
|
|
|
|
raw_value = input_field.get('value', '')
|
|
|
|
|
|
|
|
|
|
# Type conversion based on input type
|
|
|
|
|
if input_type == 'checkbox':
|
|
|
|
|
# Checkbox: bool based on 'checked' attribute
|
|
|
|
|
self.fields[name] = input_field.has_attr('checked')
|
|
|
|
|
|
|
|
|
|
elif input_type == 'radio':
|
|
|
|
|
# Radio: str value (only if checked)
|
|
|
|
|
if input_field.has_attr('checked'):
|
|
|
|
|
self.fields[name] = raw_value
|
|
|
|
|
elif name not in self.fields:
|
|
|
|
|
# If no radio is checked yet, don't set a default
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
elif input_type == 'number':
|
|
|
|
|
# Number: int or float based on value
|
|
|
|
|
self.fields[name] = self._convert_number(raw_value)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# Other types (text, hidden, email, password, etc.): analyze value
|
|
|
|
|
self.fields[name] = self._convert_value(raw_value)
|
|
|
|
|
|
|
|
|
|
# Process select fields
|
|
|
|
|
for select_field in self.element.find_all('select'):
|
|
|
|
|
name = select_field.get('name')
|
|
|
|
|
if not name:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Extract all options
|
|
|
|
|
options = []
|
|
|
|
|
selected_value = None
|
|
|
|
|
|
|
|
|
|
for option in select_field.find_all('option'):
|
|
|
|
|
option_value = option.get('value', option.get_text(strip=True))
|
|
|
|
|
option_text = option.get_text(strip=True)
|
|
|
|
|
|
|
|
|
|
options.append({
|
|
|
|
|
'value': option_value,
|
|
|
|
|
'text': option_text
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# Track selected option
|
|
|
|
|
if option.has_attr('selected'):
|
|
|
|
|
selected_value = option_value
|
|
|
|
|
|
|
|
|
|
# Store options list
|
|
|
|
|
self.select_fields[name] = options
|
|
|
|
|
|
|
|
|
|
# Store selected value (or first option if none selected)
|
|
|
|
|
if selected_value is not None:
|
|
|
|
|
self.fields[name] = selected_value
|
|
|
|
|
elif options:
|
|
|
|
|
self.fields[name] = options[0]['value']
|
|
|
|
|
|
|
|
|
|
def _get_my_ft(self, element: Tag):
|
|
|
|
|
_inner = element.find(self.tag) if self.tag and self.tag != element.name else element
|
|
|
|
|
return MyFT(_inner.name, _inner.attrs)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _get_input_identifier(input_field, counter):
|
|
|
|
|
"""
|
|
|
|
|
@@ -473,8 +431,458 @@ class TestableForm(TestableElement):
|
|
|
|
|
|
|
|
|
|
# Default to string
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _get_element(html_fragment: str):
|
|
|
|
|
html_fragment = html_fragment.strip()
|
|
|
|
|
if (not html_fragment.startswith('<div') and
|
|
|
|
|
not html_fragment.startswith('<form')):
|
|
|
|
|
html_fragment = "<div>" + html_fragment + "</div>"
|
|
|
|
|
return BeautifulSoup(html_fragment, 'html.parser').find()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _parse(tag, html_fragment: str):
|
|
|
|
|
elt = BeautifulSoup(html_fragment, 'html.parser')
|
|
|
|
|
|
|
|
|
|
if len(elt) == 0:
|
|
|
|
|
raise ValueError(f"No HTML element found in: {html_fragment}")
|
|
|
|
|
|
|
|
|
|
if len(elt) == 1:
|
|
|
|
|
elt = elt.find()
|
|
|
|
|
elt_tag = elt.name
|
|
|
|
|
if tag is not None and tag != elt_tag:
|
|
|
|
|
raise ValueError(f"Tag '{tag}' does not match with '{html_fragment}'.")
|
|
|
|
|
my_ft = MyFT(elt_tag, elt.attrs)
|
|
|
|
|
if elt_tag != "form":
|
|
|
|
|
elt = BeautifulSoup(f"<form>{html_fragment}</form>", 'html.parser')
|
|
|
|
|
return elt_tag, elt, my_ft
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
if tag is None:
|
|
|
|
|
raise ValueError(f"Multiple elements found in {html_fragment}. Please specify a tag.")
|
|
|
|
|
|
|
|
|
|
elt = BeautifulSoup(f"<form>{html_fragment}</form>", 'html.parser')
|
|
|
|
|
_inner = elt.find(tag)
|
|
|
|
|
my_ft = MyFT(_inner.name, _inner.attrs)
|
|
|
|
|
return tag, elt.find(), my_ft
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestableForm(TestableElement):
|
|
|
|
|
"""
|
|
|
|
|
Represents an HTML form that can be filled and submitted in tests.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, client, source):
|
|
|
|
|
"""
|
|
|
|
|
Initialize a testable form.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
client: The MyTestClient instance.
|
|
|
|
|
source: The source HTML string containing a form.
|
|
|
|
|
"""
|
|
|
|
|
super().__init__(client, source, "form")
|
|
|
|
|
# self.form = BeautifulSoup(self.html_fragment, 'html.parser').find('form')
|
|
|
|
|
# self.fields_mapping = {} # link between the input label and the input name
|
|
|
|
|
# self.fields = {} # field name; field value
|
|
|
|
|
# self.select_fields = {} # list of possible options for 'select' input fields
|
|
|
|
|
#
|
|
|
|
|
# self._update_fields_mapping()
|
|
|
|
|
# self.update_fields()
|
|
|
|
|
|
|
|
|
|
# def update_fields(self):
|
|
|
|
|
# """
|
|
|
|
|
# Update the fields dictionary with current form values and their proper types.
|
|
|
|
|
#
|
|
|
|
|
# This method processes all input and select elements in the form:
|
|
|
|
|
# - Determines the appropriate Python type (str, int, float, bool) based on
|
|
|
|
|
# the HTML input type attribute and/or the value itself
|
|
|
|
|
# - For select elements, populates self.select_fields with available options
|
|
|
|
|
# - Stores the final typed values in self.fields
|
|
|
|
|
#
|
|
|
|
|
# Type conversion priority:
|
|
|
|
|
# 1. HTML type attribute (checkbox → bool, number → int/float, etc.)
|
|
|
|
|
# 2. Value analysis fallback for ambiguous types (text/hidden/absent type)
|
|
|
|
|
# """
|
|
|
|
|
# self.fields = {}
|
|
|
|
|
# self.select_fields = {}
|
|
|
|
|
#
|
|
|
|
|
# # Process input fields
|
|
|
|
|
# for input_field in self.form.find_all('input'):
|
|
|
|
|
# name = input_field.get('name')
|
|
|
|
|
# if not name:
|
|
|
|
|
# continue
|
|
|
|
|
#
|
|
|
|
|
# input_type = input_field.get('type', 'text').lower()
|
|
|
|
|
# raw_value = input_field.get('value', '')
|
|
|
|
|
#
|
|
|
|
|
# # Type conversion based on input type
|
|
|
|
|
# if input_type == 'checkbox':
|
|
|
|
|
# # Checkbox: bool based on 'checked' attribute
|
|
|
|
|
# self.fields[name] = input_field.has_attr('checked')
|
|
|
|
|
#
|
|
|
|
|
# elif input_type == 'radio':
|
|
|
|
|
# # Radio: str value (only if checked)
|
|
|
|
|
# if input_field.has_attr('checked'):
|
|
|
|
|
# self.fields[name] = raw_value
|
|
|
|
|
# elif name not in self.fields:
|
|
|
|
|
# # If no radio is checked yet, don't set a default
|
|
|
|
|
# pass
|
|
|
|
|
#
|
|
|
|
|
# elif input_type == 'number':
|
|
|
|
|
# # Number: int or float based on value
|
|
|
|
|
# self.fields[name] = self._convert_number(raw_value)
|
|
|
|
|
#
|
|
|
|
|
# else:
|
|
|
|
|
# # Other types (text, hidden, email, password, etc.): analyze value
|
|
|
|
|
# self.fields[name] = self._convert_value(raw_value)
|
|
|
|
|
#
|
|
|
|
|
# # Process select fields
|
|
|
|
|
# for select_field in self.form.find_all('select'):
|
|
|
|
|
# name = select_field.get('name')
|
|
|
|
|
# if not name:
|
|
|
|
|
# continue
|
|
|
|
|
#
|
|
|
|
|
# # Extract all options
|
|
|
|
|
# options = []
|
|
|
|
|
# selected_value = None
|
|
|
|
|
#
|
|
|
|
|
# for option in select_field.find_all('option'):
|
|
|
|
|
# option_value = option.get('value', option.get_text(strip=True))
|
|
|
|
|
# option_text = option.get_text(strip=True)
|
|
|
|
|
#
|
|
|
|
|
# options.append({
|
|
|
|
|
# 'value': option_value,
|
|
|
|
|
# 'text': option_text
|
|
|
|
|
# })
|
|
|
|
|
#
|
|
|
|
|
# # Track selected option
|
|
|
|
|
# if option.has_attr('selected'):
|
|
|
|
|
# selected_value = option_value
|
|
|
|
|
#
|
|
|
|
|
# # Store options list
|
|
|
|
|
# self.select_fields[name] = options
|
|
|
|
|
#
|
|
|
|
|
# # Store selected value (or first option if none selected)
|
|
|
|
|
# if selected_value is not None:
|
|
|
|
|
# self.fields[name] = selected_value
|
|
|
|
|
# elif options:
|
|
|
|
|
# self.fields[name] = options[0]['value']
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def submit(self):
|
|
|
|
|
"""
|
|
|
|
|
Submit the form.
|
|
|
|
|
|
|
|
|
|
This method handles both HTMX-enabled forms and classic HTML form submissions:
|
|
|
|
|
- If the form supports HTMX (has hx_post, hx_get, etc.), uses HTMX request
|
|
|
|
|
- Otherwise, simulates a classic browser form submission using the form's
|
|
|
|
|
action and method attributes
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The response from the form submission.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ValueError: If the form has no action attribute for classic submission.
|
|
|
|
|
"""
|
|
|
|
|
# Check if the form supports HTMX
|
|
|
|
|
if self._support_htmx():
|
|
|
|
|
return self._send_htmx_request(data=self.fields)
|
|
|
|
|
|
|
|
|
|
# Classic form submission
|
|
|
|
|
action = self.element.get('action')
|
|
|
|
|
if not action or action.strip() == '':
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"The form has no 'action' attribute. "
|
|
|
|
|
"Cannot submit a classic form without a target URL."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
method = self.element.get('method', 'post').upper()
|
|
|
|
|
|
|
|
|
|
# Prepare headers for classic form submission
|
|
|
|
|
headers = {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Send the request via the client
|
|
|
|
|
return self.client.send_request(
|
|
|
|
|
method=method,
|
|
|
|
|
url=action,
|
|
|
|
|
headers=headers,
|
|
|
|
|
data=self.fields
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# def _translate(self, field):
|
|
|
|
|
# """
|
|
|
|
|
# Translate a given field using a predefined mapping. If the field is not found
|
|
|
|
|
# in the mapping, the original field is returned unmodified.
|
|
|
|
|
#
|
|
|
|
|
# :param field: The field name to be translated.
|
|
|
|
|
# :type field: str
|
|
|
|
|
# :return: The translated field name if present in the mapping, or the original
|
|
|
|
|
# field name if no mapping exists for it.
|
|
|
|
|
# :rtype: str
|
|
|
|
|
# """
|
|
|
|
|
# return self.fields_mapping.get(field, field)
|
|
|
|
|
#
|
|
|
|
|
# def _update_fields_mapping(self):
|
|
|
|
|
# """
|
|
|
|
|
# Build a mapping between label text and input field names.
|
|
|
|
|
#
|
|
|
|
|
# This method finds all labels in the form and associates them with their
|
|
|
|
|
# corresponding input fields using the following priority order:
|
|
|
|
|
# 1. Explicit association via 'for' attribute matching input 'id'
|
|
|
|
|
# 2. Implicit association (label contains the input)
|
|
|
|
|
# 3. Parent-level association with 'for'/'id'
|
|
|
|
|
# 4. Proximity association (siblings in same parent)
|
|
|
|
|
# 5. No label (use input name as key)
|
|
|
|
|
#
|
|
|
|
|
# The mapping is stored in self.fields_mapping as {label_text: input_name}.
|
|
|
|
|
# For inputs without a name, the id is used. If neither exists, a generic
|
|
|
|
|
# key like "unnamed_0" is generated.
|
|
|
|
|
# """
|
|
|
|
|
# self.fields_mapping = {}
|
|
|
|
|
# processed_inputs = set()
|
|
|
|
|
# unnamed_counter = 0
|
|
|
|
|
#
|
|
|
|
|
# # Get all inputs in the form
|
|
|
|
|
# all_inputs = self.form.find_all('input')
|
|
|
|
|
#
|
|
|
|
|
# # Priority 1 & 2: Explicit association (for/id) and implicit (nested)
|
|
|
|
|
# for label in self.form.find_all('label'):
|
|
|
|
|
# label_text = label.get_text(strip=True)
|
|
|
|
|
#
|
|
|
|
|
# # Check for explicit association via 'for' attribute
|
|
|
|
|
# label_for = label.get('for')
|
|
|
|
|
# if label_for:
|
|
|
|
|
# input_field = self.form.find('input', id=label_for)
|
|
|
|
|
# if input_field:
|
|
|
|
|
# input_name = self._get_input_identifier(input_field, unnamed_counter)
|
|
|
|
|
# if input_name.startswith('unnamed_'):
|
|
|
|
|
# unnamed_counter += 1
|
|
|
|
|
# self.fields_mapping[label_text] = input_name
|
|
|
|
|
# processed_inputs.add(id(input_field))
|
|
|
|
|
# continue
|
|
|
|
|
#
|
|
|
|
|
# # Check for implicit association (label contains input)
|
|
|
|
|
# input_field = label.find('input')
|
|
|
|
|
# if input_field:
|
|
|
|
|
# input_name = self._get_input_identifier(input_field, unnamed_counter)
|
|
|
|
|
# if input_name.startswith('unnamed_'):
|
|
|
|
|
# unnamed_counter += 1
|
|
|
|
|
# self.fields_mapping[label_text] = input_name
|
|
|
|
|
# processed_inputs.add(id(input_field))
|
|
|
|
|
# continue
|
|
|
|
|
#
|
|
|
|
|
# # Priority 3 & 4: Parent-level associations
|
|
|
|
|
# for label in self.form.find_all('label'):
|
|
|
|
|
# label_text = label.get_text(strip=True)
|
|
|
|
|
#
|
|
|
|
|
# # Skip if this label was already processed
|
|
|
|
|
# if label_text in self.fields_mapping:
|
|
|
|
|
# continue
|
|
|
|
|
#
|
|
|
|
|
# parent = label.parent
|
|
|
|
|
# if parent:
|
|
|
|
|
# input_found = False
|
|
|
|
|
#
|
|
|
|
|
# # Priority 3: Look for sibling input with matching for/id
|
|
|
|
|
# label_for = label.get('for')
|
|
|
|
|
# if label_for:
|
|
|
|
|
# for sibling in parent.find_all('input'):
|
|
|
|
|
# if sibling.get('id') == label_for and id(sibling) not in processed_inputs:
|
|
|
|
|
# input_name = self._get_input_identifier(sibling, unnamed_counter)
|
|
|
|
|
# if input_name.startswith('unnamed_'):
|
|
|
|
|
# unnamed_counter += 1
|
|
|
|
|
# self.fields_mapping[label_text] = input_name
|
|
|
|
|
# processed_inputs.add(id(sibling))
|
|
|
|
|
# input_found = True
|
|
|
|
|
# break
|
|
|
|
|
#
|
|
|
|
|
# # Priority 4: Fallback to proximity if no input found yet
|
|
|
|
|
# if not input_found:
|
|
|
|
|
# for sibling in parent.find_all('input'):
|
|
|
|
|
# if id(sibling) not in processed_inputs:
|
|
|
|
|
# input_name = self._get_input_identifier(sibling, unnamed_counter)
|
|
|
|
|
# if input_name.startswith('unnamed_'):
|
|
|
|
|
# unnamed_counter += 1
|
|
|
|
|
# self.fields_mapping[label_text] = input_name
|
|
|
|
|
# processed_inputs.add(id(sibling))
|
|
|
|
|
# break
|
|
|
|
|
#
|
|
|
|
|
# # Priority 5: Inputs without labels
|
|
|
|
|
# for input_field in all_inputs:
|
|
|
|
|
# if id(input_field) not in processed_inputs:
|
|
|
|
|
# input_name = self._get_input_identifier(input_field, unnamed_counter)
|
|
|
|
|
# if input_name.startswith('unnamed_'):
|
|
|
|
|
# unnamed_counter += 1
|
|
|
|
|
# self.fields_mapping[input_name] = input_name
|
|
|
|
|
#
|
|
|
|
|
# @staticmethod
|
|
|
|
|
# def _get_input_identifier(input_field, counter):
|
|
|
|
|
# """
|
|
|
|
|
# Get the identifier for an input field.
|
|
|
|
|
#
|
|
|
|
|
# Args:
|
|
|
|
|
# input_field: The BeautifulSoup Tag object representing the input.
|
|
|
|
|
# counter: Current counter for unnamed inputs.
|
|
|
|
|
#
|
|
|
|
|
# Returns:
|
|
|
|
|
# The input name, id, or a generated "unnamed_X" identifier.
|
|
|
|
|
# """
|
|
|
|
|
# if input_field.get('name'):
|
|
|
|
|
# return input_field['name']
|
|
|
|
|
# elif input_field.get('id'):
|
|
|
|
|
# return input_field['id']
|
|
|
|
|
# else:
|
|
|
|
|
# return f"unnamed_{counter}"
|
|
|
|
|
#
|
|
|
|
|
# @staticmethod
|
|
|
|
|
# def _convert_number(value):
|
|
|
|
|
# """
|
|
|
|
|
# Convert a string value to int or float.
|
|
|
|
|
#
|
|
|
|
|
# Args:
|
|
|
|
|
# value: String value to convert.
|
|
|
|
|
#
|
|
|
|
|
# Returns:
|
|
|
|
|
# int, float, or empty string if conversion fails.
|
|
|
|
|
# """
|
|
|
|
|
# if not value or value.strip() == '':
|
|
|
|
|
# return ''
|
|
|
|
|
#
|
|
|
|
|
# try:
|
|
|
|
|
# # Try float first to detect decimal numbers
|
|
|
|
|
# if '.' in value or 'e' in value.lower():
|
|
|
|
|
# return float(value)
|
|
|
|
|
# else:
|
|
|
|
|
# return int(value)
|
|
|
|
|
# except ValueError:
|
|
|
|
|
# return value
|
|
|
|
|
#
|
|
|
|
|
# @staticmethod
|
|
|
|
|
# def _convert_value(value):
|
|
|
|
|
# """
|
|
|
|
|
# Analyze and convert a value to its appropriate type.
|
|
|
|
|
#
|
|
|
|
|
# Conversion priority:
|
|
|
|
|
# 1. Boolean keywords (true/false)
|
|
|
|
|
# 2. Float (contains decimal point)
|
|
|
|
|
# 3. Int (numeric)
|
|
|
|
|
# 4. Empty string
|
|
|
|
|
# 5. String (default)
|
|
|
|
|
#
|
|
|
|
|
# Args:
|
|
|
|
|
# value: String value to convert.
|
|
|
|
|
#
|
|
|
|
|
# Returns:
|
|
|
|
|
# Converted value with appropriate type (bool, float, int, or str).
|
|
|
|
|
# """
|
|
|
|
|
# if not value or value.strip() == '':
|
|
|
|
|
# return ''
|
|
|
|
|
#
|
|
|
|
|
# value_lower = value.lower().strip()
|
|
|
|
|
#
|
|
|
|
|
# # Check for boolean
|
|
|
|
|
# if value_lower in ('true', 'false'):
|
|
|
|
|
# return value_lower == 'true'
|
|
|
|
|
#
|
|
|
|
|
# # Check for numeric values
|
|
|
|
|
# try:
|
|
|
|
|
# # Check for float (has decimal point or scientific notation)
|
|
|
|
|
# if '.' in value or 'e' in value_lower:
|
|
|
|
|
# return float(value)
|
|
|
|
|
# # Try int
|
|
|
|
|
# else:
|
|
|
|
|
# return int(value)
|
|
|
|
|
# except ValueError:
|
|
|
|
|
# pass
|
|
|
|
|
#
|
|
|
|
|
# # Default to string
|
|
|
|
|
# return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestableInput(TestableElement):
|
|
|
|
|
def __init__(self, client, source):
|
|
|
|
|
super().__init__(client, source, "input")
|
|
|
|
|
assert len(self.fields) <= 1
|
|
|
|
|
self._input_name = next(iter(self.fields))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def name(self):
|
|
|
|
|
return self._input_name
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def value(self):
|
|
|
|
|
return self.fields[self._input_name]
|
|
|
|
|
|
|
|
|
|
def send(self, value):
|
|
|
|
|
self.fields[self.name] = value
|
|
|
|
|
if self.name and self._support_htmx():
|
|
|
|
|
return self._send_htmx_request(data={self.name: self.value})
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def get_value(tag):
|
|
|
|
|
# """Return the current user-facing value of an HTML input-like element."""
|
|
|
|
|
# if tag.name == 'input':
|
|
|
|
|
# t = tag.get('type', 'text').lower()
|
|
|
|
|
# if t in ('checkbox', 'radio'):
|
|
|
|
|
# # For checkbox/radio: return True/False if checked, else value if defined
|
|
|
|
|
# return tag.has_attr('checked')
|
|
|
|
|
# return tag.get('value', '')
|
|
|
|
|
#
|
|
|
|
|
# elif tag.name == 'textarea':
|
|
|
|
|
# # Textarea content is its text, not an attribute
|
|
|
|
|
# return tag.text or ''
|
|
|
|
|
#
|
|
|
|
|
# elif tag.name == 'select':
|
|
|
|
|
# # For <select>, get selected option value (or text if no value attr)
|
|
|
|
|
# selected = tag.find('option', selected=True)
|
|
|
|
|
# if selected:
|
|
|
|
|
# return selected.get('value', selected.text)
|
|
|
|
|
# first = tag.find('option')
|
|
|
|
|
# return first.get('value', first.text) if first else ''
|
|
|
|
|
#
|
|
|
|
|
# else:
|
|
|
|
|
# raise TypeError(f"Unsupported tag: <{tag.name}>")
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# def _update_value(tag, new_value):
|
|
|
|
|
# """Simulate user input by updating the value of <input>, <textarea>, or <select>."""
|
|
|
|
|
# if tag.name == 'input':
|
|
|
|
|
# t = tag.get('type', 'text').lower()
|
|
|
|
|
# if t in ('checkbox', 'radio'):
|
|
|
|
|
# # For checkbox/radio: treat True/False as checked/unchecked
|
|
|
|
|
# if isinstance(new_value, bool):
|
|
|
|
|
# if new_value:
|
|
|
|
|
# tag['checked'] = ''
|
|
|
|
|
# elif 'checked' in tag.attrs:
|
|
|
|
|
# del tag.attrs['checked']
|
|
|
|
|
# else:
|
|
|
|
|
# tag['value'] = str(new_value)
|
|
|
|
|
# else:
|
|
|
|
|
# tag['value'] = str(new_value)
|
|
|
|
|
#
|
|
|
|
|
# elif tag.name == 'textarea':
|
|
|
|
|
# tag.string = str(new_value)
|
|
|
|
|
#
|
|
|
|
|
# elif tag.name == 'select':
|
|
|
|
|
# # Deselect all options
|
|
|
|
|
# for option in tag.find_all('option'):
|
|
|
|
|
# option.attrs.pop('selected', None)
|
|
|
|
|
# # Select matching one by value or text
|
|
|
|
|
# matched = tag.find('option', value=str(new_value))
|
|
|
|
|
# if matched:
|
|
|
|
|
# matched['selected'] = ''
|
|
|
|
|
# else:
|
|
|
|
|
# matched = tag.find('option', string=str(new_value))
|
|
|
|
|
# if matched:
|
|
|
|
|
# matched['selected'] = ''
|
|
|
|
|
#
|
|
|
|
|
# else:
|
|
|
|
|
# raise TypeError(f"Unsupported tag: <{tag.name}>")
|
|
|
|
|
|
|
|
|
|
class MyTestClient:
|
|
|
|
|
"""
|
|
|
|
|
A test client helper for FastHTML applications that provides
|
|
|
|
|
@@ -565,7 +973,6 @@ class MyTestClient:
|
|
|
|
|
|
|
|
|
|
def clean_text(txt):
|
|
|
|
|
return "\n".join(line for line in txt.splitlines() if line.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self._content is None:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
@@ -578,9 +985,9 @@ class MyTestClient:
|
|
|
|
|
# Provide a snippet of the actual content for debugging
|
|
|
|
|
snippet_length = 200
|
|
|
|
|
content_snippet = clean_text(
|
|
|
|
|
visible_text[:snippet_length] + "..."
|
|
|
|
|
if len(visible_text) > snippet_length
|
|
|
|
|
else visible_text
|
|
|
|
|
visible_text[:snippet_length] + "..."
|
|
|
|
|
if len(visible_text) > snippet_length
|
|
|
|
|
else visible_text
|
|
|
|
|
)
|
|
|
|
|
raise AssertionError(
|
|
|
|
|
f"Expected to see '{text}' in page content but it was not found.\n"
|
|
|
|
|
@@ -665,7 +1072,7 @@ class MyTestClient:
|
|
|
|
|
f"No element found matching selector '{selector}'."
|
|
|
|
|
)
|
|
|
|
|
elif len(results) == 1:
|
|
|
|
|
return TestableElement(self, results[0])
|
|
|
|
|
return TestableElement(self, results[0], results[0].name)
|
|
|
|
|
else:
|
|
|
|
|
raise AssertionError(
|
|
|
|
|
f"Found {len(results)} elements matching selector '{selector}'. Expected exactly 1."
|
|
|
|
|
@@ -695,7 +1102,7 @@ class MyTestClient:
|
|
|
|
|
remaining = []
|
|
|
|
|
for form in results:
|
|
|
|
|
testable_form = TestableForm(self, form)
|
|
|
|
|
if all(testable_form.translate(field) in testable_form.fields for field in fields):
|
|
|
|
|
if all(testable_form._translate(field) in testable_form.fields for field in fields):
|
|
|
|
|
remaining.append(testable_form)
|
|
|
|
|
|
|
|
|
|
if len(remaining) == 1:
|