C# Xamarin Automation (Win)

1.Project Overview

This guide will cover creating a C# Xamarin automation test suite. Its purpose is to test an Android app.

The code generated in this guide is hosted at https://github.com/kirbycope/c-sharp-xamarin

2.Installing Visual Studio 2017 Community

  1. Navigate to https://www.visualstudio.com/downloads/
  2. Select the “Free Download” button
  3. In the prompt, select “Save”
  4. After the download has finished, select “Run” from the prompt
  5. If the User Account Control prompt pops up, select “Yes”
  6. Select “Continue”
  7. Scroll down to “Mobile & Gaming” and then select “Mobile development with .Net”
  8. Select “Install”
  9. After the install finishes, select “Launch”
  10. Select “Not now, maybe later.”
  11. Select “Start Visual Studio” (I recommend selecting the “Dark” theme, too)
  12. Visual Studio is now installed and ready to use

3.Creating the Framework

The framework will contain the code that is common to all of our test projects. In this way we limit the amount of copy+paste and redundancy.

  1. In Visual Studio, select “File” → “New” → “Project”
  2. Select “Test” in the templates tree
  3. Select “UI Test App (Xamarin.UITest | Android)”
  4. Name the project AutomationFramework and select “OK”
  5. Right-click on the auto-generated Tests.cs file and then select “Delete”
  6. Select “Tools” → “NuGet Package Manager” → “Manage NuGet Packages for Solution…”
  7. Select “NUnit”
  8. Select the checkbox next to “AutomationFramework” and then select “Uninstall”

    • Repeat Steps 7 & 8 for “NUnitTestAdapter”
  9. Select the “Updates” tab
  10. Select the checkbox next to “Xamarin.UITest” and then select “Update”
  11. Select “Tools” → “NuGet Package Manager” → “Manage NuGet Packages for Solution…”
  12. Type Install-Package NUnit and then press [Enter]
  13. Type Install-Package NUnit3TestAdapter and then press [Enter]
  14. Close the “NuGet – Solution” tab

3.1.Creating a Base Class for Tests

In this framework, we will create a class that our tests will inherit from. Inheritance will allow all of out test classes to share some base functionality or properties without a lot of copy paste. Its purpose in this framework is to define the [Setup] for every test in a [TestFixture].

  1. In the Solution Explorer, right-click on “AutomationFramework” and select “Add” → “Class…”
  2. Name the class TestBase.cs and then select “Add”
  3. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]

  4. Change the class signature to public

  5. In the class, paste in the following code:
  6. In the Solution Explorer, right-click on “References” and then select “Add Reference…”
  7. In the search field, type “Config”
  8. Select the checkbox next to “System.Configuration” and then select “OK”
  9. Place the cursor in the ConfigurationManager text and then fix the missing using directive
  10. Place the cursor in the IEnumerable text and then fix the missing using directive
  11. Place the cursor in the App text and then generate the type in a new class file

3.2.Creating an App Wrapper

  1. Right-click on the “AutomationFramework” project and then select “Add” → “Class”
  2. Name the class App.cs and then select “Add”
  3. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
  4. Change the class signature to public
  5. In the class paste in the following code
  6. Fix the missing using directive for IApp
  7. Fix the missing using directive for FileInfo
  8. Open the “TestBase.cs” tab
  9. Fix the missing using directive
  10. Save and close all tabs
    • We will fix the errors related to AppControl in the next section

3.3.Creating a Native Element Wrapper

  1. In Solution Explorer, right click on “AutomationFramework” and then select “Add” → “Class”
  2. Name the class AppControl.cs and then select “OK”
  3. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
  4. Change the class signature to public
  5. In the class add the following code:

    • Fix all missing using directives
  6. Save and close all tabs

3.4.Creating a WebView Element Wrapper

  1. In Solution Explorer, right click on “AutomationFramework” and then select “Add” → “Class”
  2. Name the class AppWebControl.cs and then select “OK”
  3. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
  4. Change the class signature to public
  5. In the class add the following code:

    • Fix all missing using directives
  6. Save and close all tabs

4.Creating a Test Suite

A Test Suite is a collection of tests cases intended to test an application. In our paradigm, we will create a new Visual Studio solution that references the AutomationFramework. This allows us to test multiple web applications using the same framework. The Test Suite will contain the tests and necessary page object models (POMs).

  1. Select “File” → “New” → “Project”
  2. Name the project TimothyCopeAndroid and then select “OK”

    • Unsure the following:
      • Template: Test
      • Type: UI Test App (Xamarin.UITest | Android)
      • Solution: Create new solution
  3. Delete the auto-generated .cs file

4.1.Referencing the Framework

  1. In the Solution Explorer, right-click the “TimothyCopeAndroid” solution and then select “Add” → “Existing Project…”
  2. Browse to the “AutomationFramework” project and then select “Open”
  3. Right-click on “References” for “TimothyCopeAndroid” and then select “Add Reference…”
  4. Select “Projects”
  5. Select the checkbox next to “AutomationFramework” and then select “OK”

4.2.Creating a Page Object Model

A Page Object Model (POM) is used in the Page Object Design Pattern. The POM is composed of the following components:

  1. Right-click “AutomationFramework” and select “Add” → “New Folder”
  2. Type POMs and then press [Enter]
  3. Right-click on the “POMs” folder and then select “Add” → “Class…”
  4. Name the class HomePageObject.cs and then select “Add”
  5. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
  6. Change the class signature to public
  7. In the class, type ctor and then press [Tab] twice to have Visual Studio template your constructor
  8. In the constructor type /* Empty Constructor */
  9. Below that, add a couple regions for “Page Elements” and “Page Methods”
  10. In the “Page Elements” region add the following code:
  11. Save and close all tabs

4.3.Add POM Reference to App

  1. In the “AutomationFramework”, open “App.cs”
  2. In the “Page Object Models” region, add the following code:
  3. At the top of the using directives, add using AutomationFramework.POMs;
  4. Save and close all tabs

4.4.Creating a Test

  1. Right-click on the “TimothyCopeAndroid” project and then select “Add” → “Class…”
  2. Name the class HomePageTests.cs and then select “Add”
  3. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]

4.4.1.Create the Test Fixture

  1. Above the class, add [TestFixture]

    • Fix the missing using directive (NUnit.Framework)
  2. Change the class signature to public
  3. After the class name, add : TestBase

    • Fix the missing using directive (AutomationFramework)

4.4.2.Create a Test Method

  1. In the class, add the following:

    • When we want to interact with the app we call “App”, then the page (“HomePage”), and then the element (“imgLogo”).
  2. Save and close all tabs

5.Setting Up the Config

  1. Open Notepad and add the following:
  2. Save as App.config and then drag and drop into your project
  3. Select “Build” → “Rebuild Solution”
  4. Select “Test” → “Windows” → “Test Explorer”
  5. The test should have the Device ID from the App.config

6.Running the Tests

You can use Visual Studio’s Test Explorer to execute tests.

  1. Select “Tools” > “Android” > “Android Emulator Manager…”
  2. Select the device you’d like to run on and then select “Start”
  3. Select “Launch”

    • If you have any errors reported, fix them and try again. I had to disable HyperVisor and reboot.
  4. Wait until the emulated device is fully booted up
  5. In “Test Explorer”, right-click your test and then select “Run Selected Tests”
  6. The result(s) will be updated as each test finishes
Suggest Edit