How to set-up Automation in C# for iOS Hybrid app using Appium?

How to set-up Automation in C# for iOS Hybrid app using Appium?

Image result for appium ios
The below capabilities are to be set in case of the following conditions:
  1. When the iOS app is hybrid in nature - consist of Native and WebView contexts 
  2. When webView context is not visible/detected by Visual Studio 
Pre-conditions:
  1. 'iOS-webkit-debug-proxy' should be downloaded & installed in Mac machine 
  2. 'iOS-webkit-debug-proxy' should be always remain connected in Mac Machine prior running the below set-up code of Appium. 
  3. Appium Server in Mac should be already configured with the below capabilities:
          In Custom Server tab of Appium Server, add the below details:
             Remote Host: IP address of the local machine (Example: 10.2.100.120)
             Remote Port: Port for appium server (Example: 4723)
             Check "Allow Session Override" in 'Advanced' tab of Appium
             Pass WebDriverAgent Port: 8100 in iOS section of 'Advanced' tab in Appium
             Click on Start Server button
           

Set-up Configurations to connect, start Appium server and automate:

DesiredCapabilities capabilities;
System.Diagnostics.Process process;
System.Diagnostics.ProcessStartInfo startInfo;

AppiumLocalService service;
public static IWebDriver driver;
public static AppiumDriver<IOSElement> iOSappDriver;

public void iOSAppInitialise()
        {
            capabilities = new DesiredCapabilities();
            process = new System.Diagnostics.Process();
            startInfo = new System.Diagnostics.ProcessStartInfo();

//To connect to the already open iOS simulator in Mac
            capabilities.SetCapability("app", "");
            capabilities.SetCapability("platformName", "iOS");
            capabilities.SetCapability("platformVersion", "11.2");
            capabilities.SetCapability("deviceName", "iPhone 7 Plus"); 

//To launch the installed app in the iOS simulator in Mac to automate test scripts
            capabilities.SetCapability("bundleId", "com.package.appName.test");
            capabilities.SetCapability("automationName", "XCUITest"); 
            capabilities.SetCapability("WebDriverAgent", "http://localhost:8100");

//To enable typing via iOS keypad 
            capabilities.SetCapability("connectHardwareKeyboard", "false");
           
//To connect and detect the WebView Context elements in the iOS hybrid app 
//(Thus, Selenium Web code gets used in Appium Hybrid for all applicable sections)
            capabilities.SetCapability("autoWebView", true);
            capabilities.SetCapability("startIWDP", true);
            capabilities.SetCapability("instrumentApp", true);
            capabilities.SetCapability("udid","auto");
            capabilities.SetCapability("webviewConnectRetries", 3);
            capabilities.SetCapability("webkitDebugProxyPort", 27753);
            capabilities.SetCapability("absoluteWebLocations", true);

//To Set App Launch Timeout & default Timeout settings 
            capabilities.SetCapability("newCommandTimeout",3000);
            capabilities.SetCapability("launchTimeout", 70000);

//To start Appium Server on the pre-defined IP address & port set-up on Appium Server installed in Mac
            AppiumLocalService service = new AppiumServiceBuilder()
                                      .WithIPAddress("10.2.162.120")
                                      .UsingPort(1234)
                                      .Build();
            service.Start();

//To launch the hybrid app in iOS Simulator
            driver = new IOSDriver<IOSElement>(new Uri("http://10.2.162.120:1234/wd/hub"),  capabilities, TimeSpan.FromSeconds(150));
            iOSappDriver = (IOSDriver<IOSElement>)driver;
        }

How to Inspect iOS app elements using Appium Inspector?

Once the above set-up is up and running, User can inspect the app elements using Appium Inspector by following the below steps: 

Click on "Start Inspector Session" icon in Appium 
Verify the displayed Remote Host, Port & Path (pre-populated) 
Desired Capabilities in Json Representation should be displayed with the below: 

//The below capabilities will allow user to debug/inpect webView elements in Appium Inspector
         {
            "platformName": "iOS",
            "platformVersion": "11.2",
            "deviceName": "iPhone 7 Plus",
            "bundleId": "com.package.appName.test",
            "startIWDP": true,
            "autoWebView": true,
            "automationName": "XCUITest"
          }

Go/Click on "Attach to Session" : Attach to an existing session if one already is active - Ideally pre-populates with connected device details based on the already active session in respective emulators. 
          
User should be able to detect all elements of iOS app in Appium Inspector by now. 

Note:
Above solution works fine for Appium v1.8.1



About the author of this post:


I am a Software Test Engineer, who is quite passionate about testing, automating and finding bugs. I am from Bangalore, India. 

Feel free to reach me out on suggestions and improvements at My Linkedin Profile

Copyright © 2017 qatoolsguide.blogspot.com || ALL RIGHTS RESERVED