How To: Invoking Methods From Xamarin Test Cloud UITests

As a Xamarin Certified Premier Partner and a Xamarin Test Cloud Partner RedBit has been working with customers implementing iOS and Android apps and automating their test processes using Test Cloud. In that time we have come up with some issues but workarounds were always implemented to accomplish what needed to be done and IApp.Invoke is one of those great workarounds. We used this recently on a project where we needed to test the tapping of a map pin on Android but it just wasn’t available.

In this article, we’ll go through how to use IApp.Invoke to invoke some code in both Android and iOS Xamarin based app. If you are interested specifically in the mapping example leave a comment and we’ll do a write up on that, but for this article, it will go through adding items to a list on both Android and iOS and testing it using UITest.

The App Details

The app functionality is very simple and essentially adds an item to a list view on both iOS and Android and looks as follows

The only difference between the two apps are on Android, when an item is clicked it shows the item text at the bottom of the screen. On iOS, when an item is clicked, it shows a full screen of the page as follows

All source code is available on GitHub and you can follow along in the next sections from the code.

Setting Up The Methods – Android

Now that we know about the functionality we can get into the methods to invoke within our Android app. There are two methods we want to invoke

Method to invoke adding a new item called InvokeAddNewItem

Method to invoke tapping on an item in the ListView depending on the index passed as a parameter called InvokeTapItem

These are the key attributes you need to add to the methods to be able to invoke them from a Xamarin UITest and essentially makes these invokable from external processes.

Setting Up The Methods – iOS

On iOS, it takes a bit more work to get the methods to run. Essentially, we can only ‘expose’ methods from AppDelegate and from there we have to call the specific functionality to run. In this example Actions are used to run the functionality required from AppDelegate .

Notice how these are calling static Actions within MasterViewController called InvokeAddNewItem and InvokeTapItem. Also notice to expose these methods to UITests we use the Foundation.ExportAttribute passing in the method name with a colon on the end.

This is essentially the same logic the Android app does except it’s using iOS specific classes.

Invoking from UITest

Now that the methods are ready the UITest can be created to invoke these methods.

NOTE: this is a sample only to show how to accomplish invoking methods. The source code provided on GitHub does have how a real test for this example app should be done.

The UITests provided have been setup to work on both Android and iOS and will leave it up to you to explore that source code. For one of the tests we would want to test the following user scenario

When a users adds a new item to the list, they should be able to tap on that item and the app should show the details of that item. On iOS it should navigate to a details page and on Android it should show a ‘widget’ at the bottom.

This is easily implementable by the following standard test using no IApp.Invoke as follows

Now imagine a scenario where you don’t have access to the ‘add’ button or you could not tap on a listItem, this is where IApp.Invoke comes in handy. Here is the same test as above, but using the ‘invoke’ methods instead

What’s Next?

As I said, it’s a sample to show how to invoke some hidden methods within your iOS and Android app so using in this scenario doesn’t make a lot of sense. But imagine a scenario where you need to simulate some location code, you don’t have access to some elements on the screen which is the case with Android Maps and pins or you want to run some internal ‘test harness’ code, IApp.Invoke will be helpful.