Extensibility- Add New Element Wait Methods

Learn how to extend BELLATRIX adding new element wait methods.

Introduction

Imagine that you want to wait for an element to have a specific content. First, you need to create a new ‘WaitStrategy’ class that inheriting the WaitStrategy class.

Example

public class WaitHaveSpecificContentStrategy<TDriver, TDriverElement> : WaitStrategy<TDriver, TDriverElement>
    where TDriver : IOSDriver<TDriverElement>
    where TDriverElement : AppiumWebElement
{
    private readonly string _elementContent;

    public WaitHaveSpecificContentStrategy(string elementContent, int? timeoutInterval = null, int? sleepInterval = null)
        : base(timeoutInterval, sleepInterval)
    {
        _elementContent = elementContent;
        TimeoutInterval = timeoutInterval ?? Bellatrix.ConfigurationService.GetSection<MobileSettings>().ElementToHaveContentTimeout;
    }

    public override void WaitUntil<TBy>(TBy by) => WaitUntil(ElementHasSpecificContent(WrappedWebDriver, by), TimeoutInterval, SleepInterval);

    private Func<TDriver, bool> ElementHasSpecificContent<TBy>(TDriver searchContext, TBy by)
        where TBy : FindStrategy<TDriver, TDriverElement>
    {
        return driver =>
        {
            try
            {
                var element = by.FindElement(searchContext);
                return component.Text == _elementContent;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
            catch (InvalidOperationException)
            {
                return false;
            }
        };
    }
}

After UntilHaveSpecificContent is created, it is important to be passed on to the element’s EnsureState method.

Usage

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

namespace Bellatrix.Mobile.IOS.GettingStarted
{
    [TestFixture]
    [IOS(Constants.IOSNativeAppPath,
        Constants.IOSDefaultVersion,
        Constants.IOSDefaultDeviceName,
        Lifecycle.RestartEveryTime)]
    public class AddNewElementWaitMethodsTests : IOSTest
    {
        [Test]
        [Ignore]
        public void MessageChanged_When_ButtonHovered_Wpf()
        {
            var button = 
				App.Components.CreateByName<Button>("ComputeSumButton").ToHaveSpecificContent("button");

            button.Click();
        }
    }
}

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

using Bellatrix.Mobile.IOS.GettingStarted.ExtensionMethodsWaitMethods;

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

var button = App.Components.CreateByName<Button>("ComputeSumButton").ToHaveSpecificContent("button");