image

Step-by-step guide for Automated Testing Framework Xamarin

Mobile apps have already become part of our daily lives including when we make purchases, when socializing and so on. Various frameworks and tools exist for developing mobile apps with great interfaces. In this article, the focus will be on the Xamarin platform, which helps designers and developers build native iOS, Android and Windows applications. We are going review how to check your app and find defects by using the Xamarin.UI tests before releasing the app to real customers.

What is the Xamarin.UITest?

The Xamarin.UITest is an automated testing framework which allows developers to write acceptance (e2e) tests written in NUnit.

Why automated tests are so important?

Automated tests allow one to check the behavior of a system from the client side. Additionally, it helps in catching errors in the codebase with developers being able to better collaborate in building user stories. Below are some of the benefits you get by of using automated tests:

  • Assist is an actively developed system
  • Improve and maintain testing quality
  • Remove routine operations from testers
  • Save time on regression testing

Xamarin.UI tests in details

Before we start, let’s see what kind of tools you need:

AppInitializer

First of all, we need to initialize a Xamarin.UITest on Android and iOS by using the static ConfigureApp class:

public static IApp StartApp(Platform platform)

{

            string currentFile = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;

            FileInfo fi = new FileInfo(currentFile);

            string dir = fi.Directory.Parent.Parent.Parent.FullName;

            String apkPath = Path.Combine(dir, "MyShop.Android", "bin", "Debug", "com.refractored.myshoppe.apk");

            String appPath = Path.Combine(dir, "MyShop.iOS", "bin", "iPhoneSimulator", "Debug", "MyShopiOS.app");

            if (platform == Platform.Android)

            {

                return ConfigureApp

                    .Android

                    .ApkFile (apkPath)

                    .StartApp();

            }

            return ConfigureApp

                .iOS

                .DeviceIdentifier("C9655270-CF4B-4E61-B9B6-BD66487C1639")

                .AppBundle (appPath)

                .StartApp();

  }

Xamarin UITest Queries and Debugging

Xamarin UI tests allow you use the next Queries

  • ID
  • Class
  • Button
  • Text
  • Marked
  • XPath
  • CSS

How it should look when you write a test:

public Func<AppQuery, AppQuery> Xamarin_Spinner = (e => e.Id("content"));

public Func<AppQuery, AppQuery> Text_field = (e => e.Marked("specific class or id"));

public Func<AppQuery, AppQuery> Text_field = (e => e.Marked("text1").Marked(“text2”));

A more difficult test

public Func<AppQuery, AppQuery> Text_field = (e => e.Id("text1").Parent().Id(“text2”).index(4));

public Func<AppQuery, AppQuery> Text_field = (e => e.Property("text").StartsWith(“Hello”);

Now it’s time to use the debug mode

We are going to use the app.Repl() method which allows you to stop the execution of your tests and see the whole model tree. Just place app.Repl() in any part of your tests where you want your tests to stop executing.

[Test]

public void verifyElemensOnLoadPage()

{

load = new LoadPage(app);

app.Repl();

Assert.IsTrue(load.isExistXamarinSpinner());

Assert.AreEqual("Xamarin Store", app.Query(header.Text_field).FirstOrDefault().Text);

}

That is all, pretty simple right? Then, to identify elements you can use the app.Flash command

app.Flash(e => e.Marked("specific class or id"))

Page Object pattern

For the tests, we are going to use page object pattern (where the main idea like each page it’s external class)

class LoadPage

    {

        private AndroidApp app;

        public Func<AppQuery, AppQuery> Xamarin_Spinner = (e => e.Id("content"));

        public Func<AppQuery, AppQuery> Xamarin_Label = (e => e.Id("home"));

        public LoadPage(AndroidApp app)

        {

            this.app = app;

        }

        public Boolean isExistXamarinSpinner()

        {

            app.WaitForElement(Xamarin_Spinner, "Timed out waiting for data to load", TimeSpan.FromSeconds(10));

            return app.Query(Xamarin_Spinner).Any();

        }

        public Boolean isExistXamarinLabel()

        {

            app.WaitForElement(Xamarin_Label, "Timed out waiting for data to load", TimeSpan.FromSeconds(10));

            return app.Query(Xamarin_Label).Any();

        }

}

Run tests in the cloud

Now, once we are done with simple tests, it’s time to run them somewhere in the cloud (i.e Microsoft App center)

To run tests on Android you can use such command:

appcenter test run uitest --app "/Android" --devices f6f85 --app-path MyShop.Android/bin/Debug/com.refractored.myshoppe.apk --test-series "master" --locale "en_US" --build-dir MyShop.UITest/bin/Debug --uitest-tools-dir packages/Xamarin.UITest.3.0.7/tools

Integration in development lifecycle

This will configure the pipeline which increases the speed of software development. 

For each and every check-in into a version control system, this pipeline will be triggered and every piece of code will get tested automatically before it is merged into the master.

By Pavel Douhan, Senior Testing Engineer, Innovation-Digital Lab at Seavus

Over +5 years in automation testing with web(mobile web), native app, performance and api testing. Experienced in:

  • BDD, writing and implementation of Gherkin specifications on different languages java, javascript, c#. Mobile automation for Android and iOS with running tests in the clouds like Sauce labs, Browser Stack, AppCenter( Xamarin Test Cloud).
  • Presentation and demo preparing for different clients from USA, Germany, UK Load/ Perfomance testing by jmeter with Grahana visualization.
  • Continuous integration (Jenkins, TeamCity, Circle Ci, VSTS) , creating pipelines with integration in development process.
  • Creating own Docker images and publishing into Docker hub Easy going, sociable, excellent communication skills, opened to help colleagues