Splinter Fields

The following Fields are available with the default Splinter implementation. Each implements a specific performer method.

  • Button: Clickable object.
  • Checkbox: Object with a set and unset state.
  • Dropdown: Object with multiple options to choose from.
  • Input: Object that accepts keyboard input.
  • Link: Clickable text.

All Fields that use Splinter also inherit the following convenience methods:

SplinterBase.is_present()

Checks if an element is present in the DOM.

Takes the same arguments as Splinter’s is_element_present_by_xpath

SplinterBase.is_not_present()

Checks if an element is not present in the DOM.

Takes the same arguments as Splinter’s is_element_not_present_by_xpath

SplinterBase.is_visible()

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

Parameters:wait_time (int) – The number of seconds to wait
SplinterBase.is_not_visible()

Checks if an element is present in the DOM but not visible.

Parameters:wait_time (int) – The number of seconds to wait

Example:

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


assert Inventory().price.is_present(wait_time=6)
class stere.fields.Button

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

Button.click()

Uses Splinter’s click method.

Example

>>> purchase = Button('id', 'buy_button')
>>> purchase.click()
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()
class stere.fields.Dropdown

By default, the Dropdown field works against HTML Dropdowns. However, it’s possible to extend Dropdown to work with whatever implementation of a CSS Dropdown you need.

It implements select() as its performer.

The option argument can be provided to override the default implementation. This argument expects a Field. The Field should be the individual options in the dropdown you wish to target.

self.languages = Dropdown('id', 'langDrop', option=Button('xpath', '/h4/a/strong'))
Dropdown.options()

Searches for all the elements that are an option in the dropdown.

Returns:list
Dropdown.select()

Searches for an option by its html content, then clicks the one that matches.

Parameters:value (str) – The option value to select.
Raises:ValueError – The provided value could not be found in the dropdown.
class stere.fields.Input

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

Input.fill()

Uses Splinter’s fill method.

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

Example

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

Fills the element with value.

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

Link.click()

Uses Splinter’s click method.

Example

>>> login = Link('id', 'loginLink')
>>> login.click()

Clicks the element.

Location 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.