Automated Xamarin UI Tests using Test Recorder in a local Android device
Nowadays we can find posts about tools, components, controls or whatever, but there are not much people talking about testing and the only solution we can see that has a main role on this topic is Xamarin Test Cloud. For the opposite, I think we could give more value to our Apps if we create Automated Locally UI Tests which can be created on a fast and easy way and also ran for free on physical devices.
Those kind of tests include functionality testing for the UI controls and they will help you to verify the whole app. It gives you more code coverage and lets you know if your app has some bug on an early stage. The great thing is that you would have to record the tests using Xamarin Test Recorder and you can find it for:
- Visual Studio 2013/2015 – Windows
- Visual Sutdio Community – Mac
In this post I will use Xamarin Test Recorder for Windows, however on the next post I will run an automated test using Test Recorder for Mac.
Create new UI Test App (Cross-Platform) project into your solution
If you have already debugged any application on your device, skip these requirements:
Requirements for connecting device and local machine
- Debugging is usually enabled on the Android device. If it is not enabled, go to Settings > Developer options and Check off the Usb Debugging option.
- If you could not find the Developer options, go to Settings > About phone and tap seven times the Build number item, soon the Developer options will be activated. Now you are able to turn on the debugging option.
- Then download and install the Google USB Driver package from: Tools > Android > Android SDK Manager.
- The API number your device has also should be installed on the computer (Android SDK). For example, my phone works with version number 23, so I have installed it by the Android SDK Manager (screenshot below).
Compile and pack your application before run the tests
Check if the compiled application targets a correct CPU architecture for the device where you will run the test.
Don’t use Shared Runtime – be careful! In debug mode it is checked under Android Poject > Properties/Options > Android Options.
You could deploy the app into your device and the .apk file will be created. Or click right on the Android project > Deploy (on Release mode).
Connect your device and check the serial identifier of it:
Open ADB Command prompt and run: adb devices
Copy the id from your device.
-Comment TestFixture iOS for now, I will create another post for testing apps in iOS devices.
-Setup the app using the apk file created previously:
Possible exception you have to deal with:
Go to the ADB Command prompt and run: adb kill-server
It will remove all the emulators and connected devices.
Then you should run: adb start-server
Now you should only see your device on the list, if it is the case you’re ok!
Go to Test Explorer and Run the AppLunches test. It should open the application in your phone and it will save a screenshot on your machine itself.
After that, the test will happen 🙂
On that part we could have several tests, created by hand or by the recorder as we will see in this post. For more information I recommend to check UI Test documentation and learn to create complex UI using Gestures, Backdoors, etc.
I would also suggest using AutomatitionId when we want to interact with controls into our Xamarin forms apps.
Recording a test
Download the preview version for Windows using the following links:
Xamarin Test Recorder – Visual Studio 2015: https://marketplace.visualstudio.com/items?itemName=XamarinInc.XamarinTestRecorder2015
Xamarin Test Recorder – Visual Studio 2013: https://marketplace.visualstudio.com/items?itemName=XamarinInc.XamarinTestRecorder2013
Also I have added the link for Mac, but I will emphasize about this tool in the next post.
Xamarin Test Recorder – Visual Studio Community: Download link
Requirements for Windows:
- Visual Studio 2013 or 2015 Enterprise edition.
- Android 4.3 or higher (API 19)
- INTERNET permissions on Android manifest.
- Remember to not use Shared Mono Runtime.
If you checked the previous image on this post, maybe you have seen an highlighted icon in blue.
With your project in Debug mode, select the same apk you have referenced on SetUp method.
Visual Studio will launch the app on your device and it will be listening each move you do and recording them in Test.cs as a new method, Wooala!!!
In that case I tapped the button in the MainPage and it was automatically added to the test method called: NewTest1.
During the recording you will see an icon on the left of the new method which you can:
- Stop recording
- Take a screenshot, it will create a step on your method which will take a screenshot while the test is running.
- Start assertion mode, it is used to lock the app and wait until you select an element on the UI.
Doing those steps, we could create all the tests we want.
To sum up
I have tried to create a brief first post about automated tests in local environments so all of us could add them in our projects and really check that it could help us to create more robust apps.
If you would have some question, suggestion or simply want to talk about that…contact me 🙂