Targets - Galad/tranquire GitHub Wiki

Targets are a mean to represent an element on the user interface without needing to know how this element is resolved. In Tranquire.Selenium, they are represent with the ITarget interface.

Targets can be used in one of the built-in action, such as Click.

Creating a target

You can create a target with the Target static class:

var addButton = Target.The("Add button").LocatedBy(By.Id("add"));

The LocatedBy method takes a Selenium By object, so you can use any locator supported by Selenium.

Targets with parameters

Some targets needs a selector that can only be known during the test execution. For instance, if after adding an item in a list we want to click on it, we might give the item a name to be able to identify it properly.

Tranquire.Selenium provides the ITargetWithParameter interface that allows to create a target by giving additional parameters, such as the item name. To create a ITargetWithParameter instance, you can just use the LocatedBy overload:

var itemTarget = Target.The("Item").LocatedBy("//div[text() = '{0}']", locator => By.XPath(locator));

The string value passed in the first argument is a format for the locator, where you can use placeholders to represent the values known at runtime (see string.Format for more information about how the placeholders format). The function to pass as an argument takes the locator where the placeholders have been replaced by the value known at runtime, and returns a By selector built from it.

Finally, to create the ITarget instance, use the method Of where the argument are values that should replace the placeholders:

var buyBreadItemTarget = itemTarget.Of("Buy bread");

Creating a target from a IWebElement

It is also possible to create a target from a Selenium IWebElement instance, the method LocatedByWebElement. Those targets will always resolve the same element, which is the one provided to the method.