C# Xamarin Automation (OSX)

1.Project Overview

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

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

2.Installing Visual Studio for Mac

  1. Navigate to https://www.visualstudio.com/vs/visual-studio-mac/
  2. Select the “Download Visual Studio for Mac Preview” button
  3. From the dock, select “Downloads” and then select “VisualStudioforMacPreviewInstaller.dmg”
  4. Double-click”Install Visual Studio for Mac Preview”
  5. Select “Open”
  6. Select “I accept the License Terms and the Privacy Statement” and then select “Accept”
  7. Select “Continue”
  8. Select “Continue”
  9. Select “Install”
  10. Select “I agree to the license terms” and then select “Continue”
  11. (If prompted) Enter your credentials and then select “OK”

    • You may be prompted multiple times throughout the installation
  12. Select “Continue”
  13. Select “Quit”
  14. Close the installer launcher

    1. You can now eject and delete the .dmg

3.Installing XCode

  1. Open the App Store
  2. Search for XCode
  3. Select “Get”
  4. Select “INSTALL APP”
  5. Select “OPEN”
  6. Select “Agree”
  7. (If prompted) Enter your credentials
  8. Select “Xcode” → “Open Developer Tool” → “Simulator”

    • Tip: Change the scaling to 50%
      • Select “Window” → “Scale” → “50%”
  9. Right-click on the Simulator icon in the dock and then select “Options” → “Keep in Dock”
  10. Close all windows/applications

4.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. Open Visual Studio
  2. Select “New Project…”
  3. Select “Tests” from the template pane
  4. Select “UI Test App” and then “Next”
  5. Enter AutomationFramework for both the Project and Solution name and then select “Create”
  6. Select “Project” → “Update NuGet Packages”
  7. Expand the Project in the Solution Explorer, right-click on Tests.cs, and then select “Remove”
  8. In the prompt, select “Delete”

4.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. Right-click the “AutomationFramework” project and then select “Add” → “New File…”
  2. Select “General” → ” Empty Class”
  3. Name the class TestBase and then select “New”
  4. Delete the TestBase() method in the TestBase class
  5. In the class, paste in the following code:
  6. Right-click on the text IEnumerable and then select “Quick Fix”
  7. Select “Using System.Collections.Generic;”
  8. In the Solution Explorer, right-click on “References” and then select “Edit References…”
  9. In the search field, type Config
  10. Select the checkbox next to “System.Configuration” and then select “OK”
  11. Right-click on the text “ConfigurationManager” and then select “Quick Fix”
  12. Select “using System.Configuration;”
  13. Remove the unused reference “using System;”

  14. Fix the missing using directive:
    1. ConfigureApp – using Xamarin.UITest;

4.2.Creating an App Wrapper

  1. In the Solution Explorer, right-click on the “AutomationFramework” project and then select “Add” → “New File…”
  2. Select “General” → “Empty Class”
  3. Name the class App and then select “New”
  4. Delete the App() method in the App class
  5. In the class, paste in the following code
  6. Right-click the text “IApp” and then select “Quick Fix”
  7. Select “using Xamarin.UITest;”
  8. Right-click on the text “FileInfo” and then select “Quick Fix”
  9. Select “using System.IO;”

4.3.Creating a Native Element Wrapper

  1. In the Solution Explorer, right-click on the “AutomationFramework” project and then select “Add” → Add File…”
  2. Select “General” → “Empty Class”
  3. Name the class AppControl and then select “New”
  4. Delete the AppControl() method in the AppControl class
  5. In the class, paste the following code:
  6. Right-click the text “AppQuery” and then select “Quick Fix”
  7. Select “using Xamarin.UITest.Queries”
  8. Right-click on the text “StringBuilder” and  then select “Quick Fix”
  9. Select “using System.Text;”

4.4.Creating a WebView Element Wrapper

  1. In the Solution Explorer, right-click on the “AutomationFramework” project and then select “Add” → “Add File…”
  2. Select “General” → “Empty Class”
  3. Name the class AppWebControl and the select “New”
  4. Remove the AppWebControl() method from the AppWebControl class
  5. In the class, paste the following code:
  6. Fix the missing using directives:
    1. AppQuery/AppWebQuery – using Xamarin.UITest.Queries;
    2. StringBuilder – using System.Text;
  7. Save and close all tabs

5.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 Solution”
  2. Select “iOS” → “Tests” → “UI Test App” and then select “Next”
  3. Enter “TimothyCopeIos” for the Project and Solution name and then select “Create”
  4. Right-click on Tests.cs and select “Remove”
  5. In the prompt, select “Delete”
  6. Update your NuGet pacakages like you did with the AutomationFramework.

5.1.Referencing the Framework

  1. Right-click on the Solution and then select “Add” → “Add Existing Project…”
  2. Navigate to and select the .csproj file for the AutomationFramework and then select “Open”
  3. Right-click on the “References” for the test project and then select “Edit References…”
  4. Select the “Projects” tab
  5. Select the checkbox next to “AutomationFramework” and then select “OK”

5.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 select “Add” → “New File”
  4. Create a new Empty Class named HomePageObject
  5. In the HomePageObject() method add /* Empty Constructor */
  6. Below that, add a couple regions for “Page Elements” and “Page Methods”
  7. In the Page Elements region add the following code:
  8. Save and close the HomePageObject.cs tab

5.3.Add POM Reference to App

  1. In the AutomationFramework project, open “App.cs”
  2. In the “Page Object Models” region, add the following code:
  3. Right-click the text “HomePageObject” and select “Quick Fix”
  4. Select “using AutomationFramework.POMs;”
  5. Save and close the “App.cs” tab

5.4.Creating a Test

  1. Right-click on the “TimothyCopeIos” project and select “Add” → “New File…”
  2. Create an empty class named HomePageTests and then select “New”
  3. Delete the HomePageTests() method in the HomePageTests class

5.4.1.Create the Test Fixture

  1. Above the class add [TestFixture]

    • Fix the missing using directive: “using NUnit.Framework;”
  2. After the class name add : TestBase

    • Fix the missing using directive: “using AutomationFramework;”

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

6.Setting Up the Config

  1. Right-click on the “TimothyCopeIos” project and then select “Add” → “New File…”
  2. Select “Misc” → “Application Configuration File”
  3. Name the file App and then select “New”
  4. Replace the contents of the App.config file with:
  5. Select “Build” → “Build All”
  6. Select “View” → “Pads” → “Unit Tests”

    • Alternatively you can switch to the “Test” view
  7. Find the test, “ExampleTest”

    • It should now read ExampleTest("deviceID")

7.Running the Tests

The Setup() in TestBase.cs relies on the app being already installed on the target system as well as being instrumented.

  1. Replace “deviceID” in the App.config with the ID of your device (either physical or simulated, or both (separated by a comma)
  2. Select “Build” → “Rebuild All”
  3. Right-click on your test in the “Unit Tests” pane and then select “Run”
  4. You will see the Test turn red (fail), yellow (skip/ignore) or green (pass) in the “Unit Tests” pane. You can get more detail by looking at the “Test Results” pane (“View” → “Pads” → “Test Results”)
Suggest Edit