How not to write Xamarin UI Test !

Here in this blog, i will discuss about the experience while using Xamarin UI Test for iOS/Android apps.

What is Xamarin UI Test?

Xamarin UI Test is an automation framework based on Calabash, C# and NUnit. This can be used to automate mobile applications like iOS/Android.

Why?

For an engineer who is comfortable with C#, this is the best framework that can be used to automate the mobile apps developed on hybrid/native apps.

How?

You can go through this link to know more about Xamarin UI Test.

Tips & Tricks!

  1. What if your app contains multiple pages!  You can definitely use a page object pattern framework which comes along with the nuget Package. You can find the sample here.
  2. Thread.Sleep! Undoubtedly Polling is bad. Xamarin UI Test gives us many methods to stop polling. Here is a sample where the app waits until the button is enabled:
 app.WaitFor(() => app.Query(e => e.Class(  "UIButton"  ).Index( 1 )).First().Enabled,   "Waiting for Element"  , timeout:  TimeSpan .FromMinutes( 3.0 )); 

      3. Code without Assertions ?? Never! You can use the same assertions that you use for Nunit. Like Assert, StringAssert,FileAssert, CollectionAssert.

      4. How to Identify Elements like datepicker? You can use invoke method on the element

 app.Query (x => x.Class ("UIPickerView").Invoke ("selectRow", date.Month - 1 , "inComponent", 0, "animated", true)); 

      5. How to add test case category ?? Yes, you can! Why would someone add a test category? It not only helps in identifying the test case no. but also when you have a CI pipeline, you can categorize the test methods to run specific tests.

  [Category("Test Category Name")] 

     6. How to start writing the test? Always open the repl mode and use app.flash to identify the element. After you play with the repl mode, Just type "copy" and paste it in your test method. Remove the unwanted code. "Tree" keyword is used to identify the structure of elements in the UI. And the repl mode is really interactive and supports intellisense.

    7. I am not able to find elements in repl ! If you are trying to use class, sometimes it hardly gives any result.. so try classful ! And there are many implicit methods which works efficiently to capture the element.

8. Can i check the app's memory leak issues? Yes ! you can use backdoor methods in the app to trace the managed and unmanaged memory. For a long running test, this is much helpful. Alternatively you can use Xamarin Profiler too.

9. Automate a webview inside a native app? Some apps use web based login to authenticate the user. We still have the provision to use Webview() to automate those functionalities. It supports Xpath too !

10. I need to create more data and use assertion. Can i use data driven test ? Yes definitely, CSV or Xml or JSon.

11. I want to run my UI Test in my device along with my regular builds. Is it possible ? Yes we can use cake addins to perform CI integration. Alternatively you can use app center to have e2e mobile app dev/test/deploy.

Common Troubleshooting with iOS apps

  1. Unable to contact test backend running in app. A common cause is that the app is not properly linked - You need to check the Xamarin.TestCloud.Agent (to be latest based on the Xcode) or Xamarin.Calabash component is missing from the appdelegate.cs.
  2. I upgraded all the packages but my test is not running ! You should use NUnit Version below 3.0.0, ideally to use 2.6.4 unless there is any update. Sometimes Xamarin UI Tests doesn't work well with iOS upgrades. So wait for sometime until the Xcode/Xamarin support is provided.
  3. I am not able to deploy the device agent. Check the version of Xamarin.TestCloud.Agent, Xamarin.UITest. If there was an iOS update, the Xcode needs to be updated (if not available use the preview version).
  4. System.Net.Http.HttpRequestException - Sometimes you get this error in repl mode. Try reconnecting the device and run the repl again.
  5.  Not able to view the tests. This is a common issue which we observe in visual studio for mac. You can rebuild the solution, Quit Visual Studio (Command+Q) and then re-open to view the tests.

Please feel free to post any queries below !