Monday 20 May 2013

Sample script using Appium for native iOS application

iOS automation using Appium:


Appium is an open source automation tool for native and hybrid apps. It supports for both Android and iOS platforms. Appium drives the device/emulator/simulator using the Apple's UIAutomation library or Android's UiAutomator framework using Selenium's Webdriver JSON wire protocol. So we can use the languages for scripting, which all supported by Selenium.

Requirements:

  • MAC OS X 10.7 or higher
  • Xcode and command line tools(xcode version 4.6.2 would be fine)
  • Node.js
  • Appium.app
  • Eclipse
  • TestNG

1. Prepare the application to be automated:


We need the .app file for appium automation. So download any iOS application and build the .app file.

I downloaded the iOS app named iPhoneCoreDataRecipes from the apple website. The app looks like as below,

                                                            Sample Recipe iOS application

Steps:


  • Download the application from  iPhoneCoreDataRecipes
  • Extract it and import it into the xcode.
  • Run the project, Ensure that the simulator is opened up with the application.
  • Open the terminal and go to the project folder
  • Run the following command to build the .app file

                     xcodebuild -sdk iphonesimulator6.1

It will build the app and generate the Recipe.app file in yourProjectfolder/Build/Release-iphonesimulator, which is the only one we need to give in our automation script.

Now the application is ready to be automated :)

2. Prepare the machine for iOS automation:


We need node.js to work with appium if we didn't use appium.app. Just follow the below steps,

  • Install node.js from the site http://nodejs.org/
  • Install webdriver API to the node with the below command,

                       sudo npm install wd
           here npm represents that Node.js Package Manager

  • Install Appium to the machine with the below command

                      sudo npm install appium -g

  • Now start the appium server with the below command,

                      appium &

Now appium server is started in the default port 4723 and the IP Address 0.0.0.0 (http://0.0.0.0:4723) as below,

                                                              Appium server is started


3. Prepare the automation script:


Actually I do all the scripts using java language. So I use Eclipse IDE and TestNG for scripting.

The goal of the following script is add the recipe named 'Pizza' in the recipe list and verify its presence.

package com.qa.test;

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestRecipe {

public WebDriver driver = null;

@BeforeMethod
public void setUp() throws Exception {
// set up appium
File appDir = new File("/Users/Elang/Documents/iPhoneApps/iPhoneCoreDataRecipes/Build/Release-iphonesimulator");
File app = new File(appDir, "Recipes.app");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
capabilities.setCapability(CapabilityType.VERSION, "6.1");
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
}

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


@Test
public void testAddRecipes() throws InterruptedException {

String recipeName = "Pizza";
String recipeDescription = "My lovable dish";
String preparationTime = "1 hour";

driver.findElement(By.name("Add")).click();
driver.findElement(By.xpath("//textfield[1]")).sendKeys(recipeName);
driver.findElement(By.name("Save")).click();

driver.findElement(By.name("Edit")).click();

driver.findElement(By.xpath("//tableview[1]/textfield[2]")).sendKeys(recipeDescription);
driver.findElement(By.xpath("//tableview[1]/textfield[3]")).sendKeys(preparationTime);

driver.findElement(By.name("choosePhoto")).click();
driver.findElement(By.xpath("//tableview[1]/cell[1]/text[1]")).click();
driver.findElement(By.xpath("//tableview[1]/cell[1]/image[1]")).click();
driver.findElement(By.name("Done")).click();

driver.findElement(By.xpath("//navigationBar[1]/button[1]")).click();

                // Verify the presence of added recipe.
List<WebElement> recipesDetails = driver.findElements(By.xpath("//tableview[1]/cell"));
//System.out.println("No. of Recipes: "+recipesDetails.size());
boolean result = false;
for(WebElement recipeDetails : recipesDetails) {
//System.out.println("details: "+recipeDetails.getAttribute("name"));
if(recipeDetails.getAttribute("name").equals(recipeDescription+", "+preparationTime+", "+recipeName)) {
result = true;
}
}
Assert.assertTrue(result, "Recipe is not present");
}

}

I used Appium inspector to identify the element locators. See here about how to use the appium inspector.

For the example of Android native app automation in WINDOWS platform, please look at here.


References:





19 comments:

  1. HI Elangovan,

    Say if i want to connect to my real divice and wanted to automate the app which is already installed on my device, then what is the procedure.
    Can you please help me on this.

    Thanks,
    Mahesh M

    ReplyDelete
    Replies
    1. Hi Mahesh,

      If you have the app installed already appium will invoke the installed .app file. It will not uninstall and install until you start the appium server with the flag --full-reset = true.

      Thanks,
      Elango.

      Delete
  2. Hi Elangovan

    How do you run the script? Did you create a new TestNG project? Could you make a more detailed torial about how you run the script?

    Thank you

    ReplyDelete
    Replies
    1. Hi,

      I just created a java project and used TestNG annotations in java class file. You can run the above example without testNG concepts by having the java main() method.

      Thanks,
      Elango.

      Delete
  3. what if i want to test the preinstalled apps like contacts messages etc

    ReplyDelete
    Replies
    1. Please join Apple, if you want to do that :P...

      Delete
  4. Our iOS App Developers believe in precision and excellence when it comes to developing iOS Applications, thus making every effort in creating the best of the best apps in accordance with the client’s requirements.

    iOS App Developers

    ReplyDelete
  5. Hello Sir,

    Can you Please share script to click inside a Collection view cell.

    Thanks and Regards,

    Anitha

    ReplyDelete
  6. can you please guide through the steps for android app automation using appium in mac machine

    ReplyDelete
  7. Can you please help me how to run this script in real devices
    I tried in simulator and it works fine, the same I need to run on my real device.
    Is it possible? If so please help me!

    ReplyDelete
  8. Please help me enable wifi using appium in ios when a application is running...

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. I’m eager to find the valuable information and for me this is the right place to get the good stuff.
    ios training institute in hyderabad

    ReplyDelete
  11. I’m eager to find the valuable information and for me this is the right place to get the good stuff.
    Value Softtech Solutions

    ReplyDelete
  12. Sample script using Appium for native iOS application really great one to read and to know i also suggest
    Selenium Training
    Selenium Training in Chennai

    ReplyDelete
  13. Appium Training in chennai
    We provides Best Appium Training in Chennai with real time project assistance by our leading Appium mobile App Automation testing architect
    For Free Live Demo @ Call to 8122241286.
    Appium ,Selenium,Mobile Automation Testiing using Android and IOS
    www.thecreatingexperts.com
    Appium training in chennai

    ReplyDelete
  14. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    PMP Classes in pune
    PMP Workshop in pune

    ReplyDelete