Extensibility- Extend Existing Elements- Extension Methods
Learn how to extend BELLATRIX Android elements using extension methods.
Example
using Bellatrix.Mobile.Android.GettingStarted.Custom;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bellatrix.Mobile.Android.GettingStarted
{
[TestFixture]
public class ExtendExistingElementWithExtensionMethodsTests : AndroidTest
{
[Test]
[Ignore]
public void ButtonClicked_When_CallClickMethod()
{
var button = App.Components.CreateByIdContaining<Button>("button");
button.SubmitButtonWithScroll();
}
}
}
Explanations
namespace Bellatrix.Mobile.Android.GettingStarted.Custom
{
public static class ButtonExtensions
{
public static void SubmitButtonWithScroll(this Button button)
{
button.ToExists().ToBeClickable().WaitToBe();
button.ScrollToVisible();
button.Click();
}
}
}
One way to extend an existing element is to create an extension method for the additional action.
- Place it in a static class like this one.
- Create a static method for the action.
- Pass the extended element as a parameter with the keyword ‘this’.
- 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.Android.GettingStarted.Custom;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bellatrix.Mobile.Android.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.