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):

  1. You can manually add problems you find using add_problem()
  2. Use a standard assert statement, with an failure message attached (e.g. assert True is False, "True is not False")
  3. 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.
compare(other)[source]

Check the element’s href.

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.
compare(other)[source]

Check if the element is displayed or not.

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.
compare(other)[source]

Check if the element is enabled or not.

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.
compare(other)[source]

Check if the element is present or not.

The output will be one of the following:

IsPresent: Element is not present when it should be
IsPresent: Element is present when it shouldn't be
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.
compare(other)[source]

Check the element’s placeholder.

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.
compare(other)[source]

Check the element’s tag name.

class pypcom.state.expected_attribute.Text(expected=True)[source]

Checks if the component has certain text or not.

Args:
expected (bool): What text the element should have.
compare(other)[source]

Check the element’s text.

class pypcom.state.expected_attribute.Type(expected=True)[source]

Checks if the component is of a certain type or not.

Args:
expected (bool): What type the element should be.
compare(other)[source]

Check the element’s type.