Since I started creating an Android app, I have been writing simple UI tests. I have been taking advantage of the Visual Studio App Center, which allows you to test against hundreds of different devices in the Test Cloud.
In order to write a UI test, create a UI Test App, this makes use of the nuget package Xamarin UI Test. By default, you will now have a test called AppLaunches which will take a screenshot of your app after it starts.
You can now run this test against any device from Visual Studio assuming you have it physically plugged into your machine. However, how do you run against the Test Cloud?
To run against the Test Cloud you need to first install node.js. Now you can install the appcenter cli with the following command.
npm install -g appcenter-cli
To run the tests in the Test Cloud run the following command
appcenter test run uitest --app "<username>/<appname>" --devices "<username>/<deviceset>" --app-path pathToFile.apk --test-series "master" --locale "en_US" --build-dir pathToUITestBuildDir
where <username> is your app center username, <appname> is the name of your app in the app center, and <deviceset> is the group of apps you have created in the app center to test against.
Look at device sets in the test section of the app center and click the new device set button. You can then search for any device you like and add it to a set, as I write this there are 244 devices you can test against.
It is currently not possible within app center to run tests against a new build, however, if you build your app in VSTS as well, you can create build or release step that runs it. As the Visual Studio app center is still under development I wouldn’t be surprised if it is added at some point.
In VSTS look for Mobile Center Test in the definitions and you can specify the same variables as specified in the command line above.
Now how do you actually write a useful UI test? I mentioned above you get a default test which contains the following code, this takes a screenshot of your app. However you don’t have to include this when using the Test Cloud as screenshots are included for free.
Let’s look at what else is included in the app:
This starts an interactive REPL (Read-Eval-Print-Loop) which lets you explore what is on screen in your app and pauses execution. I don’t include this in my tests, however, I do make use of it to explore what is on screen and what tests I might make use of.
app.Tap(c => c.Marked("Button"));
This taps an element on the screen called Button. There is also a method called TapCoordinates which would allow you to click anywhere you like.
app.WaitForElement(c => c.Marked("View"));
After clicking a button you are probably going to want to wait for the app to load extra data or a new screen. The WaitForElement waits for an element to appear on the screen. There are also methods that wait a period of time or wait until an element no longer exists.
These are the main methods I have used so far, however, there is an extensive list including methods for scrolling, swiping, pinching and adjusting the volume buttons. So you should be able to test all manner of app functionality and if you make use of the Test Clouds will know which devices are causing problems.