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 MobileBy convert() {
        return MobileBy.ByAndroidUIAutomator(String.format("new UiSelector().resourceIdMatches(\"%s.*\");", getValue()));
    }

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

In the convert method, we use a standard WebDriver MobileBy locator, and in this case we implement our requirements through Android UI Automator.

Usage

public class AddNewFindLocatorsTests extends AndroidTest {
    @Test
    public void buttonClicked_When_CallClickMethod() {
        var button = app().create().by(Button.class, new IdStartingWithFindStrategy("button"));

        button.click();
    }
}

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

var button = app().create().by(Button.class, new IdStartingWithFindStrategy("button"));