Targets

Targets are a way to encapsulate a human-readable string along with a CSS selector or xpath locator.

To instantiate a target, you might do something like this:

from screenpy import Target

EXAMPLE_ELEMENT1 = Target.the("first example element").located_by("//div")
EXAMPLE_ELEMENT2 = Target.the("second example element").located_by("span.example")

Let’s break that down a little bit.

The class method the() expects a human-readable string to give the element a log-friendly name. That same class method returns the newly instantiated Target object, ready to have its located_by() method called.

The located_by() method takes in the actual locator, which can either be XPath or CSS Selector.

Targets are expected to be defined in your user_interface files, and can then be used in your Actions, your Questions, and your Tasks.

Target Class

class screenpy.target.Target(desc: str)

A class to contain information about an element. This class stores a nice human-readable string describing an element along with either an XPath or a CSS selector string. It is intended to be instantiated by calling its static the() method. A typical invocation might look like:

Target.the(“header search bar”).located_by(“div.searchbar”)

It can then be used in Questions, Actions or Tasks to access that element.

all_found_by(the_actor: screenpy.actor.Actor) → List[selenium.webdriver.remote.webelement.WebElement]

Gets a list of WebElement objects described by the stored locator.

Parameters:the_actor (Actor) – The Actor who should look for these elements.
Returns:list(WebElement)
found_by(the_actor: screenpy.actor.Actor) → selenium.webdriver.remote.webelement.WebElement

Gets the WebElement object representing the targeted element.

Parameters:the_actor (Actor) – The Actor who should look for this element.
Returns:WebElement
get_locator() → Tuple[selenium.webdriver.common.by.By, str]

Returns the stored locator as a (By, str) tuple.

Returns:Tuple(By, str)
Raises:|TargetingError| – if no locator was supplied to the target.
located(locator: Tuple[selenium.webdriver.common.by.By, str]) → screenpy.target.Target

Supplies an instantiated target with a locator. This locator is a tuple of the By strategy to use and the identifying string, e.g.

Target.the(“signout link”).located((By.LINK_TEXT, “Sign Out”))
Parameters:locator – the (By, str) tuple to use to find the element.
Returns:Target
located_by(locator: str) → screenpy.target.Target

Supplies an instantiated Target with a locator string, which is either a CSS selector or an XPATH string. The strategy will be determined before it is stored.

Parameters:locator – the string to use as a locator for the element. Can be a CSS selector or an xpath string.
Returns:Target
static the(desc: str) → screenpy.target.Target

Creates a Target with a description. This method call should be followed up with a call to located_by().

Parameters:desc (str) – The human-readable description for the targeted element. Beginning with a lower-case letter makes the allure test logs look the nicest.
Returns:Target