Fields

Field

class stere.fields.Field

Field objects represent individual pieces on a page. Conceptually, they’re modelled after general behaviours, not specific HTML elements.

Parameters:
  • strategy (str) – The type of strategy to use when locating an element.
  • locator (str) – The locator for the strategy.
  • workflows (list) – Any workflows the Field should be included with.

Example

>>> from stere.fields import Field
>>> my_field = Field('xpath', '//*[@id="js-link-box-pt"]/small/span')
includes()

Will search every element found by the Field for a value property that matches the given value. If an element with a matching value is found, it’s then returned.

Useful for when you have non-unique elements and know a value is in one of the elements, but don’t know which one.

Parameters:value (str) – A text string inside an element you want to find.
Returns:element

Example

>>> class PetStore(Page):
>>>     def __init__(self):
>>>         self.inventory = Link('xpath', '//li[@class="inv"]')
>>>
>>> pet_store = PetStore()
>>> pet_store.inventory_list.includes("Kittens").click()
before()

Called automatically before methods with the @use_before decorator are called.

By default it does nothing. Override this method if an action must be taken before the method has been called.

In this example, Dropdown has been subclassed to hover over the element before clicking.

from stere.fields import Dropdown

class CSSDropdown(Dropdown):
    """A Dropdown that's customized to hover over the element before attempting
    a select.
    """
    def before(self):
        self.element.mouse_over()
after()

Called automatically before methods with the @use_after decorator are called.

By default it does nothing. Override this method if an action must be taken after the method has been called.

Root

class stere.fields.Root

A simple wrapper over Field, it does not implement a performer method. Although Root has no specific behaviour, it can be useful when declaring a root for an Area or RepeatingArea.

Example

>>> from stere.areas import RepeatingArea
>>> from stere.fields import Root
>>>
>>>
>>> collections = RepeatingArea(
>>>     root=Root('xpath', '//table/tr'),
>>>     quantity=Text('css', '.collection_qty'),
>>> )

Text

class stere.fields.Text

A simple wrapper over Field, it does not implement a performer method. Although Root has no specific behaviour, it can be useful when declaring that a Field should just be static Text.

Example

>>> from stere.fields import Text
>>>
>>>
>>> self.price = Text('id', 'item_price')

Performer method

A Field can have a single method be designated as a performer. This causes the method to be called when the Field is inside an Area and that Area’s perform() method is called.

For example, Input’s performer is the fill() method, and Button’s performer is the click() method. Given the following Area:

search = Area(
    query=Input('id', 'xsearch'),
    submit=Button('id', 'xsubmit'),
)

and the following script:

search.perform()

When search.perform() is called, query.fill() is called, followed by submit.click().

See the documentation for Area for more details.

Subclassing Field

Field can be subclassed to suit your own requirements.

If the __init__() method is overwritten, make sure to call super() before your own code.

If your class needs specific behaviour when interacting with Areas, it must use the @stere_performer decorator to specify a performer method.

Assigning the performer method

When creating a new type of Field, the stere_performer class decorator can be used to assign a performer method.

from stere.fields.field import stere_performer

@stere_performer('philosophize', consumes_arg=False)
class DiogenesButton(Field):
    def philosophize(self):
        print("As a matter of self-preservation, a man needs good friends or ardent enemies, for the former instruct him and the latter take him to task.")

The consumes arg argument should be used to specify if the method should use an argument provided by Area.perform() or not.