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 elements with name starting with specific value. First, you need to create a new ‘FindStrategy’ class.

Example

public class NameStartingWithFindStrategy : FindStrategy<IOSDriver<IOSElement>, IOSElement>
{
    private readonly string _locatorValue;

    public NameStartingWithFindStrategy(string name)
        : base(name)
    {
        _locatorValue = $"//*[starts-with(@name, '{Value}')]";
    }

    public override IOSElement FindElement(IOSDriver<IOSElement> searchContext)
    {
        return searchContext.FindElementByXPath(_locatorValue);
    }

    public override IEnumerable<IOSElement> FindAllElements(IOSDriver<IOSElement> searchContext)
    {
        return searchContext.FindElementsByXPath(_locatorValue);
    }

    public override AppiumWebElement FindElement(IOSElement element)
    {
        return component.FindElementByXPath(_locatorValue);
    }

    public override IEnumerable<AppiumWebElement> FindAllElements(IOSElement element)
    {
        return component.FindElementsByXPath(_locatorValue);
    }

    public override string ToString()
    {
        return $"Name starting with = {Value}";
    }
}

We override all available methods and use XPath expression for finding an element with name starting with.

To ease the usage of the locator, we need to create an extension methods for ComponentCreateService and Element classes.

public static class ComponentRepositoryExtensions
{
    public static TComponent CreateByNameStartingWith<TComponent>(this ComponentCreateService repo, string id)
        where TComponent : Component<IOSDriver<IOSElement>, IOSElement> => repo.Create<TComponent, NameStartingWithFindStrategy, IOSDriver<IOSElement>, IOSElement>(new NameStartingWithFindStrategy(id));

    public static ComponentsList<TComponent, NameStartingWithFindStrategy, IOSDriver<IOSElement>, IOSElement> CreateAllByNameStartingWith<TComponent>(this ComponentCreateService repo, string id)
        where TComponent : Component<IOSDriver<IOSElement>, IOSElement> => new ComponentsList<TComponent, NameStartingWithFindStrategy, IOSDriver<IOSElement>, IOSElement>(new NameStartingWithFindStrategy(id), null);
}
public static class ComponentCreateExtensions
{
    public static TComponent CreateByNameStartingWith<TComponent>(this Element<IOSDriver<IOSElement>, IOSElement> element, string id)
        where TComponent : Component<IOSDriver<IOSElement>, IOSElement> => App.Components.Create<TComponent, NameStartingWithFindStrategy>(new NameStartingWithFindStrategy(id));

    public static ComponentsList<TComponent, NameStartingWithFindStrategy, IOSDriver<IOSElement>, IOSElement> CreateAllByNameStartingWith<TComponent>(this Element<IOSDriver<IOSElement>, IOSElement> element, string id)
        where TComponent : Component<IOSDriver<IOSElement>, IOSElement> => new ComponentsList<TComponent, NameStartingWithFindStrategy, IOSDriver<IOSElement>, IOSElement>(new NameStartingWithFindStrategy(id), component.WrappedElement);
}

Usage

using Bellatrix.Mobile.IOS.GettingStarted.ExtensionMethodsLocators;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bellatrix.Mobile.IOS.GettingStarted
{
    [TestFixture]
    public class AddNewFindLocatorsTests : IOSTest
    {
        [Test]
        public void ButtonClicked_When_CallClickMethod()
        {
            var button = App.Components.CreateByNameStartingWith<Button>("Compute");

            button.Click();
        }
    }
}

You need to add a using statement to the namespace where the extension methods for new locator are situated.

using Bellatrix.Mobile.IOS.GettingStarted.ExtensionMethodsLocators;

After that, you can use the new locator as it was originally part of BELLATRIX.

var button = App.Components.CreateByNameStartingWith<Button>("Compute");