Extensibility- Extend Existing Elements- Extension Methods

Learn how to extend BELLATRIX iOS elements using extension methods.

Example

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

namespace Bellatrix.Mobile.IOS.GettingStarted
{
    [TestClass]
    [IOS(Constants.IOSNativeAppPath,
        Constants.IOSDefaultVersion,
        Constants.IOSDefaultDeviceName,
        AppBehavior.RestartEveryTime)]
    public class ExtendExistingElementWithExtensionMethodsTests : IOSTest
    {
        [TestMethod]
        public void ButtonClicked_When_CallClickMethod()
        {
            var button = App.ElementCreateService.CreateByName<Button>("ComputeSumButton");

            button.SubmitButtonWithScroll();
        }
    }
}

Explanations

namespace Bellatrix.Mobile.IOS.GettingStarted.Custom
{
    public static class ButtonExtensions
    {
        public static void SubmitButtonWithScroll(this Button button)
        {
            button.ToExists().ToBeClickable().WaitToBe();
            button.ScrollToVisible(ScrollDirection.Down);
            button.Click();
        }
    }
}

One way to extend an existing element is to create an extension method for the additional action.

  1. Place it in a static class like this one.
  2. Create a static method for the action.
  3. Pass the extended element as a parameter with the keyword ‘this’.
  4. Access the native element through the WrappedElement property and the driver via WrappedDriver.

Later to use the method in your tests, add a using statement containing this class’s namespace.

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

namespace Bellatrix.Mobile.IOS.GettingStarted

To use the additional method you created, add a using statement to the extension methods’ namespace.

button.SubmitButtonWithScroll();

Use the custom added submit button with scroll-to-visible behaviour.