Forum General


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin.UITest how do i change state in my app in between tests?

The problem:
I am attempting to write some useful Ui-tests with Xamarin.UITest and have lately been trying to figure out a way to change the application’s state in between tests. This is in order to test app behavior in different scenarios where certain elements, object or components are either present, empty or missing.

One of the solutions that I have been trying to implement is the use of backdoor methods, that lets you invoke actions and change of state on the device during a test run. However, I seem to have a great deal of trouble getting the backdoor to react with the binding variables of the app.

Furthermore, is that I have not been able to find any practical examples or guides on how to change state whilst running tests on the app. At times it almost seems as if it is not a possible to do at all. The Microsoft documentation explain that state change during tests is one of the main purposes of these backdoor methods.

The documentation gives an overall understanding of how backdoors can be set up but does not go in depth about the contents of the backdoors. It explains how to access the backdoor method, but not what kind of work I need to do in order to interact with the state of my app.

What I need help with:
I need to figure out what to write in the backdoor method in order to modify/change with the state of my app during testing.

I want to interact with binding variables like these from the Xamarin.Forms template app:

I have not been able to find any examples on how to go about doing this whatsoever, or if it’s even possible this way. Any type of help is most appreciated.


  • pelinalpppelinalpp Member ✭✭✭

    You want to change binding value while uitest is running, isn't it? If i understand true, you can do this with following steps:

    1-Create a viewmodel for bind value
    2-Define this view model on app.xaml.cs as public static. For example:
    public static ItemDetailViewModel ItemDetailVM { get; set; }
    3-Create new object when open the ItemDetail page with this static view model. For example:
    public ItemDetailPage()
    App.ItemDetailVM = new ItemDetailViewModel();
    BindingContext = App.ItemDetailVM;
    4-Create a method to change binding value on viewmodel. For example:
    public class ItemDetailViewModel

    public void ChangeBindingValue(string value)
    Text = value;

    5-Trigger this view model method on main activity backdoor method. For example:
    6-Call this export method with app.invoke() on uitest

    I didn't try but i think it will work. But it is not good solution. Because goal of uitest is not changing to binding value with uitest on runtime. Goal of uitest is learning app works or doesn't work as true on runtime.
    Why do you want to do this? It is not true way.

  • Thanks for the answer :)

    The reason we want to change the app state is to test multiple scenarios. For instance:
    The app will display something different on a screen if there is no data to display. We need to mock it so that one test will have data, and another without

    We are looking for the right way to go about this, so any suggestions you may have would be very welcome.

  • pelinalpppelinalpp Member ✭✭✭
    edited September 2020

    If you want to see different screen when there is no data, you must prepare this situation before uitest starts.

    For example I assume you have two type item that are type x and type y. And I also assume when you click type x, you want to see Label of binding Text. Or when you click type y, you don't want to see Label of binding Text. If this is what you want, you must prepare this status with preprocess before uitest starts. You must do this for example with API or another way.

    After that you must only click item and check you see or don't see Label with uitest.

  • Thank you very much for the assistance pelinalpp. Your help has been much appreciated.

Sign In or Register to comment.