Delpin Susai Raj Wednesday, 9 June 2021

Xamarin.Forms - Working with UI Tests

 In this blog, you will learn how to test Xamairn UI controls in Xamarin.Forms.

Introduction

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Xamarin.UITest

Xamarin.UITest is a C# testing framework using NUnit for UI Acceptance Tests on iOS and Android apps. It integrates tightly with Xamarin.iOS and Xamarin.Android projects, Xamarin.UITest is the Automation Library that allows the NUnit tests to execute on Android and iOS devices. The tests interact with the user interface as a user would: entering text, tapping buttons, and gestures - such as swipes.

Like Appium and Robot Framework, Xamarin.UITest is among the best open-source, cross-platform UI testing frameworks. It’s a more straightforward choice when it comes to automating Android and iOS apps built with Xamarin.Forms. 

Follow the Arrange-Act-Assert pattern:

  • Arrange: The test will set up conditions and initialize things so that the test can be actioned.
  • Act: The test will interact with the application, enter text, pushing buttons, and so on.
  • Assert: The test examines the results of the actions run in the Act step to determine correctness. For example, the application may verify that a particular error message is displayed.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.

 Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform. 

Add UI Test Project

Now, Add the UI Test project to your existing Xamarin.forms project. Go to your solution add a new project select Xamarin UI Project refer to the following screenshot.

Name your UI Test project the click to add.

Use AutomationId

  1. AutomationId should be added to all UI controls that are required for UI testing.
  2. AutomationId is a Dependency Property which can also be set with a binding expression.
  3. InvalidOperationException will be thrown if an attempt is made to set the AutomationId property of an Element more than once.
  4. AutomationId and X:Name are different. You must set AutomationId for all controls.

Ex: AutomationId="EntryPhoneNumber"

Create a UI

Here, I'm going to create a simple UI for UI Testing.

MainPage.XAML

Configure App

Here, you need to configure both iOS and Android App paths see below code.

AppInitializer.cs

 Android

You can specify your APK path. Go to your Android project debug/bin folder you can find the com.companyname.xamarinapp.apk file you copy the file path and set the ApkFile.

The device can be specified using the DeviceSerial method:

iOS

Specify the iOS IPA path. Go to your iOS debug/bin folder you can find the AppName.iOS file you copy the file path and set the AppBundle.


iOS Enable Test Cloud

To run tests on iOS, the Xamarin Test Cloud Agent NuGet package must be added to the project. Once it's been added, Add the following code into the AppDelegate.FinishedLaunching method:

Xamarin Test Cloud Agent NuGet package

AppDelegate

Set Compiler Variable

Set compiler variable to your iOS app. Go to the project option in the Compiler section set define symbols. "ENABLE_TEST_CLOUD" refer below screenshot.

Write UI Test

Now, I'm going to write the Automation UI Test for PhoneNumber validation. See below code

Run

The test method has been passed.

Download full source from Github

References

https://docs.microsoft.com/en-us/appcenter/test-cloud/frameworks/uitest/

I hope you have understood you will learn how to test Xamarin UI Elements Xamarin.Forms.

Thanks for reading. Please share your comments and feedback. 

Happy Coding :)

4 comments:

  1. Good stuff, thanks for sharing it with us.

    And for extensive write-up and sample repo on how to do UI tests in Azure DevOps pipeline: https://github.com/jmegner/XamarinPipelineDemo

    ReplyDelete
  2. Thanks for sharing.
    I can't seem to get push notifications while running my tests on the iOS platform. is there some way to achieve this?

    ReplyDelete
  3. Nice blog! I really loved reading through this Blog... Thanks for sharing such a very interesting post with us and keep blogging. Visit our website-
    Xamarin App Developers
    ABP .io Development company
    Web and App development company

    ReplyDelete
  4. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site. For more info:- Xamarin App Development

    ReplyDelete