Extensability- Extend Common Services
Learn how to extend BELLATRIX common services.
Example
using Bellatrix.Mobile.Android.GettingStarted.CommonServicesExtensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bellatrix.Mobile.Android.GettingStarted
{
[TestFixture]
public class ExtendExistingCommonServicesTests : AndroidTest
{
[Test]
public void ButtonClicked_When_CallClickMethod()
{
App.AppService.LoginToApp("bellatrix", "topSecret");
var button = App.Components.CreateByIdContaining<Button>("button");
button.Click();
}
}
}
Explanations
public static class AppServiceExtensions
{
public static void LoginToApp(this AndroidAppService appService, string userName, string password)
{
var ComponentCreateService = new ComponentCreateService();
var userNameField = ComponentCreateService.CreateByIdContaining<TextField>("textBox");
var passwordField = ComponentCreateService.CreateByIdContaining<Password>("passwordBox");
var loginButton = ComponentCreateService.CreateByIdContaining<Button>("loginButton");
userNameField.SetText(userName);
passwordField.SetPassword(password);
loginButton.Click();
}
}
One way to extend the BELLATRIX common services 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 common service as a parameter with the keyword ‘this’.
- Access the native 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.CommonServicesExtensions;
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.
App.AppService.LoginToApp("bellatrix", "topSecret");
Use newly added login method which is not part of the original implementation of the common service.