Thursday, 24 July 2014

Automate (test) iOS Application with Appium

Hello folks,

In my previous 2 blogs I have explained basics of Appium and setting up of platform for Android. Here we will talk about the iOS dev environment set up.

As you know, we have to set up both client and server. Test client remains the same except a few things.

Test Client

Client set up is almost similar to that of Android. You can find the details here.


1. Get the location of your .app file. Carefully select the simulator / device version.
2. Change platformName, version, device name etc.

That means, your test case will look like this.


package org.winster.appium;

import io.appium.java_client.AppiumDriver;

import java.io.File;
import java.net.URL;
import java.util.Set;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class IOSWebViewSimulatorTest {
  private AppiumDriver driver;

  @Before
  public void setUp() throws Exception {
    new File(System.getProperty("user.dir"));
    final File app = new File(
        "/Users/winster/Library/Developer/Xcode/DerivedData/HybridSample-dboeetgqdtcsujguegflwiqhejkh/Build/Products/Debug-iphonesimulator/HybridSample.app");
    final DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
    capabilities.setCapability("platformVersion", "7.1");
    capabilities.setCapability("platformName", "iOS");
    capabilities.setCapability("deviceName", "iPhone Simulator");
    capabilities.setCapability("app", app.getAbsolutePath());
    driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),
        capabilities);
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
  }

  @Test
  public void webView() throws InterruptedException {
    final Set<String> contextNames = driver.getContextHandles();
    for (final String contextName : contextNames) {
      if (contextName.contains("WEBVIEW")) {
        driver.context(contextName);
      }
    }
    final WebElement registerButton = driver.findElement(By.id("register"));
    registerButton.click();
  }

}

As you might have noticed, you have to correctly choose the simulator version of .app file.
For device testing just modify the .app location and deviceName.


    final File app = new File(
        "/Users/winster/Library/Developer/Xcode/DerivedData/HybridSample-dboeetgqdtcsujguegflwiqhejkh/Build/Products/Debug-iphoneos/HybridSample.app");    final DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
    capabilities.setCapability("platformVersion", "7.1.1");
    capabilities.setCapability("platformName", "iOS");
    capabilities.setCapability("deviceName", "iPhone");
    capabilities.setCapability("app", app.getAbsolutePath());

Appium Server

Setting up of Appium server was straight forward for Android. But for iOS, there are some more things to do. Please note that only iOS6+ is supported for device testing.

1. Download and install XCode(4.5+) 
2. Install XCode Command Line Tools
3. Install Node in your Mac. Please note that you have to install Node using a method (Homebrew, for example) that doesn't require sudo to install global npm packages. If its already using sudo, you have to reinstall it.
brew install node
4. Install Appium
npm install -g appium
5. Install ios-webkit-debug-proxy for device testing.

brew install ios-webkit-debug-proxy
During the installation of ios-webkit-debug-proxy, you may getting several brew dependencies. Be patient and install each of them correctly.

6. Now start appium server.

appium &
7. For device testing, also start webkit proxy

ios_webkit_debug_proxy -c <device-udid>:27753 -d
If you can see a message as follows in your terminal, you are good to go. Simply run your test case!


Troubleshooting

For device testing make sure you have added your UDID and Bundle identifier while starting the server.
appium -U <device-udid> --app <app-bundle-id>
If you are getting a error message in your terminal as follows, use --session-override parameter while server is started.


02:16:39 error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress

Solution is
appium --session-override &
Another common error is due to your network's proxy server configuration. Your proxy server might have blocked several domains such as downloads.sourceforge.net which are required for installation of brew dependencies. I got similar error for pkg-config, xz, libusb etc. You may have to find an alternative to bypass this. Either get such URLs cleared from your network administrator or use a network without any proxy.

Another issue I came across while testing device is as follows.


Invalid idevice_connection struct? Unable to attach <UDID> inspector

You can find the solution for the same here. I am also copy pasting the solution here.


downgrade the libimobiledevice to v1.1.5
Steps:

Type this on Terminal

cd /usr/local
git checkout 7e209f0 Library/Formula/libimobiledevice.rb
brew unlink libimobiledevice
brew install libimobiledevice



Hope you could make it work. Cheers!


No comments:

Post a Comment