Splinter Integration

Stere contains Fields designed specifically for when Splinter is connected. Each implements a specific performer method.

All Fields designed for Splinter also inherit the following convenience methods:

SplinterBase.is_clickable()

Check if an element is present in the DOM and clickable.

Parameters:wait_time (int) – The number of seconds to wait. If not specified, Stere.retry_time will be used.
SplinterBase.is_not_clickable()

Check if an element is not clickable in the DOM.

Parameters:wait_time (int) – The number of seconds to wait. If not specified, Stere.retry_time will be used.
SplinterBase.is_present()

Check if an element is present in the DOM.

Parameters:wait_time (int) – The number of seconds to wait. If not specified, Stere.retry_time will be used.
SplinterBase.is_not_present()

Check if an element is not present in the DOM.

Parameters:wait_time (int) – The number of seconds to wait. If not specified, Stere.retry_time will be used.
SplinterBase.is_visible()

Check if an element is present in the DOM and visible.

Parameters:wait_time (int) – The number of seconds to wait. If not specified, Stere.retry_time will be used.
SplinterBase.is_not_visible()

Check if an element is not visible in the DOM.

Parameters:wait_time (int) – The number of seconds to wait. If not specified, Stere.retry_time will be used.

Example:

class Inventory(Page):
    def __init__(self):
        self.price = Link('css', '.priceLink')


assert Inventory().price.is_present(wait_time=6)

Fields

Button

class stere.fields.Button

Convenience Class on top of Field, it implements click() as its performer.

Button.click()

Use Splinter’s click method.

Example

>>> purchase = Clickable('id', 'buy_button')
>>> purchase.click()

Checkbox

class stere.fields.Checkbox

By default, the Checkbox field works against HTML inputs with type=”checkbox”.

Can be initialized with the default_checked argument. If True, the Field assumes the checkbox’s default state is checked.

It implements opposite() as its performer.

Checkbox.set_to()

Set a checkbox to the desired state.

Parameters:state (bool) – True for check, False for uncheck

Example

>>> confirm = Checkbox('id', 'selectme')
>>> confirm.set_to(True)
Checkbox.toggle()

If the checkbox is checked, uncheck it. If the checkbox is unchecked, check it.

>>> confirm = Checkbox('id', 'selectme')
>>> confirm.toggle()
Checkbox.opposite()

Switches the checkbox to the opposite of its default state. Uses the default_checked attribute to decide this.

>>> confirm = Checkbox('id', 'selectme')
>>> confirm.opposite()

Input

class stere.fields.Input

A simple wrapper over Field, it implements fill() as its performer.

The default_value argument can be provided, which will be used if fill() is called with no arguments.

self.quantity = Input('id', 'qty', default_value='555')
Input.fill()

Use Splinter’s fill method.

Parameters:value (str) – The text to enter into the input.

Example

>>> first_name = Input('id', 'fillme')
>>> first_name.fill('Joseph')
Input.highlight()

Highlight the text content in an input element.

Money

class stere.fields.Money

Money has methods for handling Fields where the text is a form of currency.

Money.money()

Create a Money object from the Field’s text.

The returned object is an instance of moneyed.Money. See: py-moneyed

Parameters:currency (str) – Name of the currency to use
Returns:moneyed.Money
Money.number

The Field’s text, normalized to look like a number.

Locator Strategies

These represent the way a locator can be searched for.

By default, the strategies available with Splinter are:

  • css
  • xpath
  • tag
  • name
  • text
  • id
  • value

These strategies can be overridden with a custom strategy (ie: You can create a custom css strategy with different behaviour).

Custom Locator Strategies

Custom strategies can be defined using the @strategy decorator on top of a Class.

Any class can be decorated with @strategy, as long as the _find_all and _find_all_in_parent methods are implemented.

In the following example, the ‘data-test-id’ strategy is defined. It wraps Splinter’s find_by_xpath method to simplify the locator required on the Page Object.

from stere.strategy import strategy


@strategy('data-test-id')
class FindByDataTestId():
    def _find_all(self):
        """Find from page root."""
        return self.browser.find_by_xpath(f'.//*[@data-test-id="{self.locator}"]')

    def _find_all_in_parent(self):
        """Find from inside parent element."""
        return self.parent_locator.find_by_xpath(f'.//*[@data-test-id="{self.locator}"]')

With this implemented, Fields can now be defined like so:

my_button = Button('data-test-id', 'MyButton')

Support for data-* attributes is also available via the add_data_star_strategy function:

from stere.strategy import add_data_star_strategy


add_data_star_strategy('data-test-id')

This will automatically add the desired data-* attribute to the valid Splinter strategies.