DEV Community

Simon Foster
Simon Foster

Posted on • Originally published at funkysi1701.com on

Mobile App UI Testing

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.

Xamarin UI testsIn 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 you 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
Enter fullscreen mode Exit fullscreen mode

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_
Enter fullscreen mode Exit fullscreen mode

where is your appcenter username, is the name of your app in appcenter and is the group of apps you have created in app center to test against.

Look at device sets in the test section of the appcenter 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.

app.Screenshot("First screen.");
Enter fullscreen mode Exit fullscreen mode

Lets look at what else is included in app

app.Repl();
Enter fullscreen mode Exit fullscreen mode

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"));
Enter fullscreen mode Exit fullscreen mode

This taps an element on 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"));
Enter fullscreen mode Exit fullscreen mode

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 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.

The post Mobile App UI Testing appeared first on Funky Si's Tech Talk.

Top comments (0)