Using PyPCOM in Automated Tests¶
PyPCOM was built around writing end-to-end/UI tests for websites, and, as a result, comes with several features to make that process easier. While it will work within any Python testing framework, it was built with pytest in mind, so examples and code snippets will be written assuming your test framework is pytest.
The Tests¶
PyPCOM comes with two handy classes to assist with writing tests:
State
and
ExpectedAttribute
. Using them, you
can check the state of a component against several things at once with only a
single assert statement. If you’re using pytest, it will also automatically
provide an organized message of all the problems it found in the failure
message.
A State
is used to compare against the
component, while multiple
ExpectedAttribute
objects are
passed to the State
when it’s instantiated. The
ExpectedAttribute
objects are
responsible for knowing how to check the values off the component and reporting
any problems. The State
is just there to
manage the comparison of each
ExpectedAttribute
against the
component and generate a failure message for pytest to show in the failure
output.
Quick Example¶
Assuming you have all the page objects and fixtures defined, writing a test can be as easy as this (assume this is a method inside a test class):
def test_some_component(self, page):
assert page.some_component == State(
IsDisplayed(),
Text("My Text"),
TagName("p"),
)
If the element was found to be displayed, but had different text and wasn’t a <p> tag, you would see a failure that looks something like this:
Comparing SomeComponent State:
Text: "Something else" != "My Text"
TagName: "div" != "p"
The IsDisplayed
,
Text
, and
TagName
classes you see all inherit
from ExpectedAttribute
. PyPCOM
comes with several baked in (see Provided ExpectedAttribute Classes for a full
list), but the system is designed to easily extended so you can add your own.
For more detail on that, check out State or ExpectedAttribute.