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

 

Vishal Garg

Vishal Garg

Senior Quality Lead Manager

Vishal Garg is working as a Sr. Quality Lead Manager with 3Pillar Global. He brings with him rich experience in the field of Quality Assurance with various domain’s knowledge e.g. Digital Media, Retail Banking, Finance and e-Governance. He has hands on experience in web automation, Mobile Automation, API automation, and Load Testing. Prior to joining 3Pillar Global, he has been associated with reputed organizations like Birlasoft Ltd. and OSI (Open Solutions Inc.).

18 Responses to “iOS Mobile App Automation Testing Using Appium and C#”
  1. Deacon Liu on

    Very good sharing, and I wonder if it’s possible to run a c# testcase on windows, the purpose of this testcase is doing some verification on safari of a real MAC book, would you give me this favors?

    Reply
  2. Roopesh Tripathi on

    Hi Vishal ,

    How to log the success and failure results of script and generate the UIAutomation report ?
    your reply will be highly Appreciated.

    Reply
  3. divyadheen on

    Thanks for sharing this Information, Got to learn new things from your Blog on Appium.

    Reply
  4. Tirumala on

    Can you please send detailed flow Appium Automation for IOS and Android devices.

    Reply
  5. Minh Nhut on

    This information help me so much, thanks for sharing!

    Reply
  6. gargi on

    I want to automate below IOS specific function using APPIUM. Anyone can please help me is it possible or not ? I want solution only on real devices.

    IOS Specific Device Function like
    Location Enable/Disable
    Kill App
    Put Application in background
    Restart Device
    Shut Down
    Device Enable/disable Mobile Data

    Reply
  7. Prashant on

    How can we install Visual Studio on MAC? If we install VMware on MAC to run the Appium client (i.e. C# scripts using Visual Studio) then how VMware’s Visual Studio interact with the ios devices?

    Reply
  8. Amit on

    Very good article, does the Iphone need to be connected to run the tests? I was thinking of continuous integration tests.

    Reply
  9. QA Guru on

    Well, here are sharing such a great and helpful information regarding Automation Testing. Thanks for the publish great ideas in this blog!

    Reply
  10. QA Guru on

    Well, here are sharing such a great and helpful information regarding Mobile Testing. Thanks for the publish great ideas in this blog!

    Reply
  11. S.Guru on

    Very nice…

    Is it possible to automate pre-installed application like Contacts or other apps like Facebook using the above setup?

    Reply
  12. Anand on

    Hi Vishal,

    I am using “Visual Studio for Mac” in my Mac. Without installing Eclipse, can i use VS for MAC to run UI tests on Appium? Right now i am running Xamarin tests (on Android, iOS app’s) from Visual Studio. Can i use Visual Studio MAC to run Appium tests? I already have android SDK installed in my mac. I am trying to download and install appium server (https://bitbucket.org/appium/appium.app/downloads/appium.dmg).
    I am using Nunit framework, C# for scripting. Does Appium supports Nunit framework?

    Reply
  13. Suman Kumar Sahu on

    I need xpath for windows application control automation.Using Appium.

    Can any one please help on it.
    Thanks and Regards,
    Suman kumar Shau

    Reply
  14. Rajeev on

    Hi Vishal,

    Such a nice post.

    How can i write same code which will work with both OS(Android, IOS).

    Thanks,
    Rajeev

    Reply
  15. Satyakirti on

    Hi,
    Thank you for sharing this information. Can you please advice that the .app path mentioned in capabilities is path on MAC or Windows System?

    Reply
  16. Rajeev on

    Hi Vishal,

    Thanks for posting such a nice article. I am interested in where is IP address of IOS machine. Is this one is 172.xx.xx.169

    Thanks,
    Rajeev

    Reply
Leave a Reply

Related Posts

4 Reasons Everyone is Wrong About Blockchain: Your Guide to ... You know a technology has officially jumped the shark when iced tea companies decide they want in on the action. In case you missed that one, Long Isl...
3Pillar Recognized as a Leading Experience Designer by Forre... Fairfax-based product development company named to its second Forrester report in 2018 FAIRFAX, VA (June 18) - Today, 3Pillar Global, a global cust...
3 Cloud Optimization Projects That Will Pay for Themselves i... AWS introduced 1,430 new features and tools in 2017, including 497 in the 4th quarter alone. This means that it can be a challenge for even the mos...
The Connection Between Innovation & Story On this episode of The Innovation Engine, we'll be looking at the connection between story and innovation. Among the topics we'll cover are why story ...
Go Native (App) or Go Home, and Other Key Takeaways from App... I just returned from my first WWDC. I feel like I learned more in a week at Apple’s annual developer’s conference than I have in years of actually dev...