Creating backdoors in Xamarin UITest for both iOS and Android

Backdoor methods:

Backdoor methods in Xamarin UI Test are a powerful way of amending settins within your application to setup for a test. An example of this could be where a feature is toggled off by default but you would like to toggle it on for the purpose of the test.

The iOS and Android are remarkably similar with a few subtle differences.



The backdoor method must be defined in AppDelegate.cs

[Export ("togglefeaturevalue:withfeatureenabled:")]
        public NSString togglefeaturevalue (string configurationKey, bool featureenabled)
            var _countryConfigurationService = Mvx.Resolve<ICountryConfigurationService> ();
            if (_countryConfigurationService != null) {
                _countryConfigurationService.SetFeature (configurationKey, featureenabled);

            return new NSString ("OK");



There are various places you can define the backdoor method for Xamarin Android, however, we have had inconsistent results placing it in the Activity where the method is sometimes not found. We suspect this may have to do with Activity lifecycle and solved the problem by subclassing Application and placing the method in there.

        public void togglefeaturevalue(string configurationKey, bool featureEnabled)
            var _countryConfigurationService = Mvx.Resolve<ICountryConfigurationService>();
            if (_countryConfigurationService != null)
                _countryConfigurationService.SetFeature(configurationKey, featureEnabled);


Calling your backdoor method(s):

public class FeatureToggleServiceCommand
        readonly IApp _app;
        readonly Platform _platform;

        public FeatureToggleServiceCommand()
            _app = FeatureContext.Current.Get<IApp>("App");
            _platform = FeatureContext.Current.Get<Platform>("Platform");

        public virtual void Execute(string configurationKey, bool featureEnabled)
            if (_platform == Platform.Android)
                _app.Invoke ("togglefeaturevalue", new object [] { configurationKey, featureEnabled });

            if (_platform == Platform.iOS)
                _app.Invoke ("togglefeaturevalue:withfeatureenabled:", new object [] { configurationKey, featureEnabled });