ExpectedAttribute¶
The ExpectedAttribute
objects
serve as a means of compartmentalizing the logic for both how to check for
something specific against a PageComponent
, and
how to summarize any problems found. You can pass any number of
ExpectedAttribute
objects to a
State
object when you create it.
PyPCOM offers plenty of
ExpectedAttribute
classes out of
the box, which can be found below. But the system is designed to be customizable
so you can inherit from the
ExpectedAttribute
class and define
your own checks along with how to report them.
Defining Your Own¶
In order to define your own
ExpectedAttribute
, all you need to
do is make a class that inherits from
ExpectedAttribute
, and then give it
an __init__
and compare
method.
The __init__
method can be used to accept any expected values you want the
object to check for.
The compare
method will be passed a reference to the
PageComponent
when it gets called by the
State
object during the actual comparison. In
that method, there’s three ways you can track problems you find, all of which
are equally recommended (so use whichever you find most appealing):
- You can manually add problems you find using
add_problem()
- Use a standard
assert
statement, with an failure message attached (e.g.assert True is False, "True is not False"
) - Raise an
AssertionError
manually with a provided failure message (e.g.raise AssertionError("some failure message")
)
If you choose the first option, it’s recommended that you stick with
AssertionError``s, or at least provide an object with a ``.message
attribute
so that ExpectedAttribute
can find
the problem’s message in the way it normally does.
Here’s a quick example of a custom
ExpectedAttribute
, which is
provided already in PyPCOM (with the docstrings removed, however):
class IsDisplayed(ExpectedAttribute):
_msg = {
True: "Element is not displayed when it should be",
False: "Element is displayed when it shouldn't be",
}
def __init__(self, expected=True):
self._expected = bool(expected)
def compare(self, other):
assert other.is_displayed() is self._expected, self._msg[self._expected]
Provided ExpectedAttribute Classes¶
-
class
pypcom.state.expected_attribute.
Href
(expected=True)[source]¶ Checks if the component has a certain href value or not.
- Args:
- expected (bool): What href value the element should have.
-
class
pypcom.state.expected_attribute.
IsDisplayed
(expected=True)[source]¶ Checks if the component is currently displayed on the page or not.
- Args:
- expected (bool): Whether or not the element should be displayed.
-
class
pypcom.state.expected_attribute.
IsEnabled
(expected=True)[source]¶ Checks if the component is currently enabled on the page or not.
- Args:
- expected (bool): Whether or not the element should be enabled.
-
class
pypcom.state.expected_attribute.
IsPresent
(expected=True)[source]¶ Checks if the component is currently present on the page or not.
- Args:
- expected (bool): Whether or not the element should be present.
-
class
pypcom.state.expected_attribute.
Placeholder
(expected=True)[source]¶ Checks if the component has a certain placeholder value or not.
- Args:
- expected (bool): What placeholder the element should have.
-
class
pypcom.state.expected_attribute.
TagName
(expected=True)[source]¶ Checks if the component has a certain tag name or not.
- Args:
- expected (bool): What tag name the element should have.