Extensibility – Add New Find Locators

Learn how to extend BELLATRIX adding new custom find locators.

Introduction

Imagine that you want to create a new locator for finding all components with ID starting with specific value. First, you need to create a new ‘FindStrategy’ class.

Example

public class IdStartingWithFindStrategy extends FindStrategy {
    public IdStartingWithFindStrategy(String value) {
        super(value);
    }

    @Override
    public By convert() {
        return By.cssSelector(String.format("[id^='%s']", getValue()));
    }

    @Override
    public String toString() {
        return String.format("id starting with %s", getValue());
    }
}

In the convert method, we use a standard WebDriver By locator, and in this case we implement our requirements through a little CSS.

Usage

public class NewFindLocatorsTests extends WebTest {
    @Test
    public void promotionsPageOpened_When_PromotionsButtonClicked() {
        app().navigate().to("http://demos.bellatrix.solutions/");

        var promotionsLink = app().create().by(Anchor.class, new IdStartingWithFindStrategy("promo"));

        promotionsLink.click();
    }
}

You can use the new FindStrategy in the default create().by and create().allBy methods like in this example:

var promotionsLink = app().create().by(Anchor.class, new IdStartingWithFindStrategy("promo"));