Xamarin Test Cloud Tips&Tricks 2#: Deeplinking test

Standard

In my previous post I introduced Xamarin UI Test (XUT) and Xamarin Test Cloud (XTC).

This article we will know how easy it is to test deeplinking capabilities in XTC !

XTC Sample: Deeplink

Deeplinks

Pragmatically, in the mobile app context deeplinks are external provided links that your app accepts to receive and knows how to handle. They are usually used to get the user directly in a specific place in an app. Some common usecases:

  • Open a YouTube video link in any app, if you have the official YouTube app it will open with the video being loaded
  • Open Apple / Google / Microsoft store app link in a mobile device, you’ll be redirected to the app inside the Store app

Deeplinks can then be sumarrized as URLs that specific apps know how to handle. Thus, functionality is usually triggered by web pages in your browser, the OS then knows what apps answer to that specific url schemes.

Given this premise, we can automate deeplinking funcionality test by providing a way for the app to open the browser with your deeplink, which will then redirect back to your app. Pretty slick right?

Backdoors

Of course, you can add UI elements to do exactly this but, tests shouldn’t affect your app base funcionality / structure. In order to achieve this with minimal impact, we can leverage what Xamarin calls a Backdoor.

Backdoors are externally exposed methods that can be triggered by our tests. Your app will then need to be compiled with this code for it to be executed.

In our UITest we only need to execute IApp.Invoke(“MethodName”). You also have a overload to send parameters.

There are some platform specific details, please refer to the official documentation for more info!

Xamarin UI Test Sample: Deeplinking

Test Run Results !

Test Run Results !

XTC results: link | GitHub: link

Setting this up is fairly easy and you can test it by starting fresh:

  1. Create a new Android app
  2. In the MainActivity, add an IntentFilter as a class level attribute, bellow the Activity one. This is the setup you need to support deeplinks.
  3. [Activity(Label = "DeeplinkBackdoorTest", MainLauncher = true, Icon = "@drawable/icon")]
    //IntentFilter used to register for "<strong>dementedVice://DementedViceDotCom</strong>" deeplinks
    [IntentFilter(new[] { Intent.ActionView },
        Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable},
        DataScheme = "dementedVice",
        DataHost = "DementedViceDotCom")]
    public class MainActivity : Activity
    { .... }
    
  4. In the same Activity, add as a class method what Xamarin calls a backdoor. This will run by our UI test script, all the magic is done by setting the method as public and adding the Export attribute.
  5. [Export]
    public void InvokeDeepLink()
    {
        OpenLink("dementedVice://DementedViceDotCom");
    }
     
    private void OpenLink(string url)
    {
        var uri = Android.Net.Uri.Parse(url);
        var intent = new Intent(Intent.ActionView, uri);
        StartActivity(intent);
    }
    
  6. In your Ui Test script, call the InvokeDeepLink() method
  7. IApp.Invoke("InvokeDeepLink");
    

    Conclusion

    Easy right? It’s a simple neat trick but really useful! This implementation can be improved:

    • Have the backdoor receive the url string you want to open and create tests with multiple url cases
    • Only compile the backdoor code in debug by using #if DEBUG preprocessor define

    Do you know more XUT tricks like this? Share them in the comment section!

    Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s