Adding test for LoginPage

This commit is contained in:
2025-10-26 20:26:32 +01:00
parent f39205dba0
commit b98e52378e
14 changed files with 1003 additions and 8 deletions

View File

@@ -129,6 +129,307 @@ class TestableElement:
# 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 self.ft.has_attr('hx_get') or self.ft.has_attr('hx_post')
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(source, '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_form(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():
self.fields[name] = value
def submit(self):
"""
Submit the form.
Returns:
The response from the form submission.
"""
return self._send_htmx_request(data=self.fields)
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 MyTestClient:
@@ -320,6 +621,33 @@ class MyTestClient:
f"Found {len(results)} elements matching selector '{selector}'. Expected exactly 1."
)
def find_form(self, fields: list = None):
"""
Find a form element in the page content.
Can provide title of the fields to ease the search
:param fields:
:return:
"""
if self._content is None:
raise ValueError(
"No page content available. Call open() before find_element()."
)
results = self._soup.select("form")
if len(results) == 0:
raise AssertionError(
f"No element form found."
)
# result = _filter(results, fields)
if len(results) == 1:
return TestableForm(self, results[0])
else:
raise AssertionError(
f"Found {len(results)} forms (with the specified fields). Expected exactly 1."
)
def get_content(self) -> str:
"""
Get the raw HTML content of the last opened page.