December 1, 2015

iOS Mobile App Automation Testing Using Appium and C#

Appium is a very well-known and useful tool when it comes to mobile app automation testing. Most of the applications in today’s world are being tested using Appium, so we have also decided to use Appium to automate our mobile applications for iOS devices.

Normally, for scripting purposes, the Java language is preferred because it has very good documentation and support over the internet. However, in our case, our client was using both the .NET framework and C# themselves, so they suggested that we use C# for scripting.

The challenge that we faced using C# as a scripting language is that there is not much outside support to resolve issues, so I prepared this blog to provide a basic understanding to a novice user of using C# for mobile app automation testing. This blog will help you set up an Appium server and an Appium client, as well as help you prepare and execute test scripts and analyze the test results.

Appium Client-Servier Architecture

Here is the architecture through which Appium communicates with both the mobile device and the client.

mobile_automation_1

Approach

There are two ways to do the entire setup:

  1. Use a Mac machine, set up an Appium server on it and install VMware on it to run the Appium client (i.e. C# scripts using Visual Studio)
  2. Set up an Appium server on a Mac machine and an Appium Client (i.e. C# scripts using Visual Studio) on a Windows machine

We have used the second approach to perform our testing. Below are the requirements to follow this approach:

Requirements

On an Appium Server machine (iOS):

  • Mac OS X (10.7 or higher recommended)
  • XCode (4.6.3 and above recommended)
  • Apple Developer Tools (iPhone simulator SDK)
  • Appium Server (we used Appium-1.4.8.dmg)
  • Platform: iOS 8.1

On an Appium Client Machine (Windows):

  • Visual Studio 2013
  • Selenium WebDriver
  • Appium Dot Net Driver
  • NUnit Testing Framework

mobile_automation_2

Steps to Set up a Server Machine

Set up an Appium Server by downloading the Appium App on a Mac Machine from here.

mobile_automation_3

Appium Server Setting for Simulator

Verify that no checkbox is checked in iOS settings and General settings on the Appium server:

mobile_automation_4

mobile_automation_5

Appium Server Setting for Real Device

Verify that both the "UDID" and "Bundle ID" checkboxes are checked in iOS settings (correct details are provided in corresponding text boxes) and no checkbox is checked in General settings on the Appium server:

mobile_automation_6

mobile_automation_7

Steps to Set up the Client Machine

  1. Download and install Visual Studio on a Windows Machine
  2. Create an Automation Project in C# in Visual Studio
  3. Download the WebDriver .NET API Binaries from here for C# as shown below (download the latest version available)
    mobile_automation_8
  4. Download the NuGet client for Visual Studio from here and install it. When you use NuGet to install a package, it copies the library files to your solution and automatically updates your project (add references, change config files, etc.).
  5. Download NUnit from here and install it or install it using the NuGet Package manager as shown below:
    mobile_automation_9
    mobile_automation_10
  6. Using the "NUnit" Testing framework structure to set up the capabilities

Preparing the Automation Script

Prepare the test scripts in Visual Studio

Sample Test Script for Simulator

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SeleniumHelloWorld
{
    class Simulator
    {
        public IWebDriver driver;
        
        [TestFixtureSetUp]
        public void Class1()
        {
            //Setting Capabilities
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.SetCapability("platformName", "iOS");
            capabilities.SetCapability("platformVersion", "8.1");
            capabilities.SetCapability("platform", "Mac");
            capabilities.SetCapability("deviceName", "iPhone 6");
            capabilities.SetCapability("app", "/Users/ADMIN/Desktop/CAMobileApp.app");
            //Connecting to Appium Server
            driver = new RemoteWebDriver(new Uri("http://172.xx.xx.169:4723/wd/hub"), capabilities);
        }

        [Test()]
        public void VerifyMenuSimulator()
        {
            //Test to login into app
            driver.FindElement(By.XPath("//UIATextField[1]")).SendKeys("username");
            driver.FindElement(By.XPath("///UIASecureTextField[1]")).SendKeys("password");
            driver.FindElement(By.XPath("///UIAButton[1]")).Click();
        }

        [TestFixtureTearDown]
        public void End()
        {
            driver.Quit();
        }
    }
}

Sample Test Script for Real iOS Device

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SeleniumHelloWorld
{
    class IOSDevice
    {
        public IWebDriver driver;

        [TestFixtureSetUp]
        public void Class1()
        {
            //Setting Capabilities
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.SetCapability("platformName", "iOS");
            capabilities.SetCapability("platformVersion", "8.1");
            capabilities.SetCapability("platform", "Mac");
            capabilities.SetCapability("deviceName", "Company’s iPhone");
            capabilities.SetCapability("udid", "512a44173579311b509c115b3b7651e04c75698c");
            capabilities.SetCapability("bundleId", "com.ca.projectpocket");
            capabilities.SetCapability("app", "/Users/ADMIN/Desktop/CAMobileApp.app");
            //Connecting to Appium Server
            driver = new RemoteWebDriver(new Uri("http://172.xx.xx.169:4723/wd/hub"), capabilities);
         }


        [Test()]
        public void VerifyMenuDevice()
        {
            //Test to enter loginname & Password on login page
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
            wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//UIAButton[1]")));
            driver.FindElement(By.XPath("//UIATextField[1]")).SendKeys("username");
            driver.FindElement(By.XPath("//UIASecureTextField[1]")).SendKeys("password");
            driver.FindElement(By.XPath("//UIAButton[1]")).Click();
         }

        [TestFixtureTearDown]
        public void End()
        {
            driver.Quit();
        }
    }
}

Running the Test

  1. Launch the Appium server on a Mac machine
    mobile_automation_11
  2. Run the automation scripts on a Windows machine with the help of the NUnit Testing Framework. To run a set of tests, go to Tests > Windows > Test Explorer and you will see all of the tests that you prepared in your scripts listed. Select a set of tests and run it.
  3. Verify that the logs started appearing on Appium, that the app is launched on a Simulator/Device, & tests as written in scripts are being executed.
  4. Verify the execution result in Visual Studio after all scripts are executed.
    mobile_automation_12