change test helper function to be more flexible on attributes and manage content vs non content based elements. Also adds area tests

This commit is contained in:
Fabio Pliger
2024-02-27 12:36:09 -06:00
parent 51dc0909ac
commit 97fdfaeeb6
2 changed files with 38 additions and 9 deletions

View File

@@ -1,8 +1,9 @@
from textwrap import dedent
from pyscript import document, when, window
from pyweb import JSProperty, js_property, pydom
from pyscript import document, when, window
# Global attributes that all elements have (this list is a subset of the official one)
# and tries to capture the most used ones
GLOBAL_ATTRIBUTES = [
@@ -83,7 +84,9 @@ class ElementBase(pydom.Element):
elif style is None:
pass
else:
raise ValueError(f"Style should be a dictionary, received {style} (type {type(style)}) instead.")
raise ValueError(
f"Style should be a dictionary, received {style} (type {type(style)}) instead."
)
# IMPORTANT!!! This is used to auto-harvest all input arguments and set them as properties
self._init_properties(**kwargs)
@@ -151,6 +154,7 @@ def _add_js_properties(cls, *attrs):
class a(TextElementBase):
tag = "a"
# # Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes
_add_js_properties(a, "download", "href", "referrerpolicy", "rel", "target", "type")
@@ -163,7 +167,7 @@ class abbr(TextElementBase):
_add_js_properties(abbr)
class address(ElementBase):
class address(TextElementBase):
tag = "address"

View File

@@ -27,7 +27,9 @@ DEFAULT_ELEMENT_ATTRIBUTES = {
class TestElements(PyScriptTest):
def _create_el_and_basic_asserts(self, el_type, el_text, properties=None):
def _create_el_and_basic_asserts(
self, el_type, el_text=None, properties=None, check_click=True
):
if not properties:
properties = {}
@@ -37,7 +39,15 @@ class TestElements(PyScriptTest):
return f"'{v}'"
attributes = ", ".join([f"{k}={parse_value(v)}" for k, v in properties.items()])
attributes = ""
if el_text:
attributes += f'"{el_text}",'
if properties:
attributes += ", ".join(
[f"{k}={parse_value(v)}" for k, v in properties.items()]
)
body = self.page.locator("body")
assert body.inner_html() == ""
element = self.page.locator(el_type)
@@ -47,7 +57,7 @@ class TestElements(PyScriptTest):
from pyscript import when
from pyweb import pydom
from pyweb.ui.elements import {el_type}
el = {el_type}("{el_text}", {attributes})
el = {el_type}({attributes})
when("click", el)(lambda e: pydom.body.append("{el_type} clicked"))
pydom.body.append(el)
</script>
@@ -58,13 +68,15 @@ class TestElements(PyScriptTest):
el = self.page.locator(el_type)
tag = el.evaluate("node => node.tagName")
assert tag == el_type.upper()
assert el.inner_html() == el_text
if el_text:
assert el.inner_html() == el_text
assert self.console.error.lines == []
assert expected_log not in self.console.log.lines == []
# Click the link
el.click()
assert expected_log not in self.console.log.lines == []
if check_click:
el.click()
assert expected_log not in self.console.log.lines == []
if properties:
for k, v in properties.items():
@@ -107,6 +119,19 @@ class TestElements(PyScriptTest):
address = self._create_el_and_basic_asserts("address", "some text")
assert address.text_content() == "some text"
def test_area(self):
properties = {
"shape": "poly",
"coords": "129,0,260,95,129,138",
"href": "https://developer.mozilla.org/docs/Web/HTTP",
"target": "_blank",
"alt": "HTTP",
}
# TODO: Check why click times out
area = self._create_el_and_basic_asserts(
"area", properties=properties, check_click=False
)
def test_element_button(self):
button = self._create_el_and_basic_asserts("button", "click me")
assert button.inner_html() == "click me"