Skip to main content

Xamarin UI Tests – Deep dive Part 2

Following up to the previous blog i would like to get my hands a bit more dirty with the code and actually start using calabash to do some UI tests, knowing that the whole idea of testing can be maximized as we go through, from a sample UI test using the normal calabash syntax to the a more complex behavioral driven development approach where we can employ the power of  Cucumber and Gherkin using SpecFlow Syntax to create readable test cases that can be ready by non technical guys, of course every step we take towards a more technical advancement the more work we are required to do, anyhow lets start simple

First thing is the tools we gonna need and i will mention them all even if we are going to start simple but i like to get everything ready first.

The Tools

Depending on your preference you can choose to use Xamarin studio or Visual studio to write your tests
The Test Runner:
You should be able to run the test using Microsoft test runner or the Xamarin studio test running or you could just use the one that comes with other providers like resharper, it should be all up to you
Speckflow is all .net implementation for the Cucumber and Gherkin syntax so in order for Visual studio to understand the natural language that the User Stories is written by, you will need to install it on visual studio which can be found in the extensions
or you could use the xamarin studio addin from the link Specflow for Xamarin studio

That cuts it for the tools, now lets write a UI Test, but we will need an application so that we can test it on, and that is entirely up to you, i have already been working on an app and i am gonna use it to do the UI tests.

Our First UI Tests

First we will create a Xamarin UI tests you can find it in the visual studio templates as below

now that we have the ui test project layout we can view the anatomy of the solution on how it should work
before we begin it is good to know that the resources need for an initial xamarin UI test is two basic libraries
Xamarin.UITest: which contains the basic UI querying mechanisms
Xamarin.UITest.Queries; which contains the querying assisting functions like comparing and assertion helpers.
first there should be a file called appintializer which by its name should define how the apps should get initialized and initiated as below

1:  public static IApp StartApp(Platform platform)  
2:      {  
3:        if (platform == Platform.Android)  
4:        {  
5:          return ConfigureApp  
6:            .Android  
7:            .StartApp();  
8:        }  
9:        return ConfigureApp  
10:          .iOS  
11:          .StartApp();  
12:      }  

but this project does not know which or what APK/IPA files he should run so obviously we will need to modify this file and here is a sample of how you can do that

 public static IApp StartApp (Platform platform, string iOSSimulator, bool resetDevice)  
                // TODO: If the iOS or Android app being tested is included in the solution   
                // then open the Unit Tests window, right click Test Apps, select Add App Project  
                // and select the app projects that should be tested.  
                if (platform == Platform.Android) {  
                     if (resetDevice) {  
                          ResetEmulator ();  
                     return ConfigureApp  
                          .ApkFile ("../../binaries/com.xamarin.samples.taskydroidnew.exampleapp.apk")  
                          .EnableLocalScreenshots ()  
                          .StartApp ();  
                } else if (platform == Platform.iOS) {  
                     if (resetDevice) {  
                          ResetSimulator (iOSSimulator);  
                     return ConfigureApp  
                          .AppBundle ("../../binaries/")  
                          .EnableLocalScreenshots ()  
                          .StartApp ();  
                throw new ArgumentException ("Unsupported platform");  

As you can  see i have added the path to all the packages for android and iOS and some additional code regarding the unsupported exception and so on.

now lets check the test class that is added by default in the project

and it should be as below

   public class Tests  
     IApp app;  
     Platform platform;  
     public Tests(Platform platform)  
       this.platform = platform;  
     public void BeforeEachTest()  
       app = AppInitializer.StartApp(platform);  
     public void AppLaunches()  
       app.Screenshot("First screen.");  

the setup decorated method is the method which contains the code that should be ran before the beginning of each test

while the test decorated method is the method running the test and in this case it is just taking a screenshot so no actual test

is the app that calls the Android/iOS APIs to check the UI

so if i run now should that UI test run ?
the answer is no, because as i said before Xamarin UI test relies on calabash, and in order to run it needs the calabash to be started on the APK/IPA file so how should this be done ?
on Android in the main activity
adding this line of code


Same as for the iOS App delegate, this will allow the Xamarin UI tests to talk to the android/iOS APIs at runtime.

now lets run and check the runing of the app, and accordingly you should see your app runing in the simulator as per the Appinitializer.cs
now lets right a sample test and figure out how we can do a small test, say a test that checks if a button with a certain name exist, we are going to see how we can query the ui of the app.

 public void ButtonNamedLoginShouldExist()  
       app.WaitForElement(c => c.Marked("Login"));  
       app.Query(c => c.Marked("Login")).Length.ShouldBeGreaterThan(0);  
       app.Screenshot("Button named Login exists");  

in the previous code we wait for any element marked with Login  which will get any element named login or has the text property set to login and then we query the results to make sure that the button exist
If we run app should run and we should see the test passing,  for me i am using resharper test runner,

Now that we are done with sample unit test, this concludes this part, in the next part we will take things another step further, we will talk and do things like BDD , Gherkin, Specflow.


Popular posts from this blog

(AsyncWebClient) Async Webclient for windows phone 8

Using windows phone web client is pretty common but the thing is you can not use it using Async -> Await mechanism so i used threading to create an async functionality for The Download string and upload string methods here is the code below // Comment public class AsyncWebClient { public Task DownloadString(Uri uri) { var task = new TaskCompletionSource(); try { var client = new WebClient(); client.DownloadStringCompleted += (s, e) => { task.SetResult(e.Result); }; client.DownloadStringAsync(uri); } catch (Exception ex) { task.SetException(ex); } return task.Task; } public Task UploadString(Uri uri, string content) { var task = new TaskCompletionSource(); try { var client…

RuntimeBinderException cannot resolver property in Unit Test in case of dynamic return.. Solved

Xamarin UI Tests – Deep dive Part 1

IntroAs the title may describe this is the first of the series of articles that will cover the UI Test of Xamarin in a deep dive, we will start simple and then dig deeper as we go. Since this is very well the first of the series, it will mostly cover up the architecture and the testing technique that Xamarin UI Tests uses.

The Technology
Xamarin UI Test is an automation testing framework similar to selenium, Watir, Watin (.net), Robot and Sikuli, if you have used BDD (Behavioral Driven Development) or a more advanced TDD (Test Driven Development) approach at least you must have come across one of these frameworks, and in that perspective comes Xamarin UI Testing as an automation framework designed specifically for Xamarin Automation testing, For starter Xamarin UI Tests was not build from scratch, instead it was built on top of another UI Automation testing framework that is targeting android and iOS sepecifically which is Calabash, I have to say that this choice was made wisely as of n…