iOS Mobile Automation using Appium Automation tool for Native and Hybrid Application
iOS Mobile Automation using Appium
This time I started with a new tool which I had heard about
earlier but didn’t start working on it. Earlier I had worked for some time on
UI Automation Library using Instruments tool for iOS automation.
Appium – an
open source, cross-platform test automation tool which can be used for native,
hybrid and mobile web apps. It requires :
• Mac OS X
10.7 or higher, 10.8.4 recommended
• XCode >=
4.6.3
It drives various native automation frameworks and provides an API
based on Selenium’s WebDriver JSON wire protocol. It drives Apple’s
UIAutomation library for iOS support.
Appium can be useful for automation as :
• We can
write tests with Selenium WebDriver API and language specific client libraries
with any of the WebDriver compatible language say Java , Objective-C ,
Javascript , PHP , Python , C# , Ruby , Clojure or Perl.
• Any testing
framework can be used.
So I thought of trying this tool out with Java language in Eclipse
as I have been using the same for my Android automation as well and I chose
TestNG framework. Testing framework will be required if you want any way to log
your results for your purpose otherwise you can even skip this.
To start with , you will be need :
• Selenium
Server in order to run either Selenium RC style scripts or Remote Selenium
Webdriver ones.
• Language
specific client drivers
• TestNG
framework (optional)
• Appium app
to launch appium server
• Eclipse
How to use Appium -
• Appium uses
‘Appium Inspector’ to view the elements similar to what we have as
‘uiautomatorviewer’ tool to view UI components in Android in UI Automator. You
can access elements using name , xpath or tagName as visible through appium
inspector.
• Appium
Inspector can be launched by clicking on the blue ‘i’ button besides ‘Launch’
button from appium app.
• Through
appium inspector – you can view name , value , label , xpath of different
elements on the screen.
Some examples how to use elements from the screen using xpath ,
tagName, name, attribute is here –
//webdriver declaration
public WebDriver driver ;
//using xpath
driver.findElement(By.xpath("//window[1]/scrollview[1]/button[1]"));
//using name
driver.findElement(By.name("Go"));
//using attribute –
WebElement x =
driver.findElement(By.xpath("//window[1]/tableview[1]/cell[2]/text[1]"));
x.getAttribute("name"); //get name of the element
//using tagName
driver.findElement(By.tagName("button"));
//sendkeys can be used for giving some input in any field
WebElement login_email =
driver.findElement(By.xpath("//window[1]/scrollview[1]/textfield[1]"));
login_email.click();
login_email.sendKeys("kalyan@abc.com");
//provide delay between two event clicks
Thread.sleep(time in milliseconds);
NOTE - To run automation scripts for iOS , you need a Mac machine.
It can’t be done on Windows as Xcode needs
to be present for Appium to work.
You will not be able to test iOS apps on a locally hosted server,
because Apple’s instruments binary, which Appium uses to launch the iOS
simulator by default uses the currently-selected Xcode, and the highest iOS SDK
installed with that version of Xcode.
Executing Basic Script –
FIRST STEP : Creating a new project
[1] Create a new java project in Eclipse :
• Go to
‘File’ -> ‘New’ -> ‘Java Project’
[2] Add project name and click ‘Next’ –
[3] Click ‘Finish’ –
[4] Expand your project. Right click on src folder -> ‘New’
-> ‘Class’ –
[5] Enter ‘Name’ for class. Also mention ‘Package’ and then click
‘Finish’ –
[6] You will see something like –
SECOND STEP : Adding required jar
files
[1] You will need to export required selenium jar files and
install your testing framework (if required) in your project.
For that , download :
i) Selenium Server (formerly the Selenium RC Server) version and
ii) Required client driver (according to your language choice)
under – Selenium Client & WebDriver Language Bindings.
Selenium server and client drivers can be downloaded from here
– http://docs.seleniumhq.org/download/ .
[2] Import all these jar files in your project.
Right click on your project -> Properties -> Libraries ->
Add External JARs. Add all three selenium jar files here.
Optional – If you want to use TestNG : Go to Help
-> install new software -> use http://beust.com/eclipse/ :
THIRD STEP -
Download Appium app depending on your platform either
Mac/Win https://bitbucket.org/appium/appium.app/downloads/
You can use ‘IP Address’ as ‘127.0.0.1’ and port ‘4723’.
FOURTH STEP -
To run scripts on real device , you will need “UDID” (Device ID)
and “Bundle ID” (Application Bundle ID) of your app :
To run scripts on simulator , you will need “.app” and
“Bundle ID” of your app (Force device also can be left
unchecked) :
NOTE : You
can run your test scripts either in simulator or on real device but I would
recommend using iOS simulator as it responds faster than real device. Desired
capabilties need to be updated depending on whether you run on simulator or
real device.
(Desired
capabilities are a set of keys to communicate with Appium server to
tell the server what kind of automation session you are interested in).
To Run on Simulator -
public WebDriver driver = null;
DesiredCapabilities capabilities = new
DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,
"iOS");
capabilities.setCapability(CapabilityType.VERSION,
"6.1");
capabilities.setCapability(CapabilityType.PLATFORM,
"Mac");
capabilities.setCapability("device",
"iPhone");
capabilities.setCapability("app","/Users/abc/Desktop/MyiOSApp.app");
driver = new RemoteWebDriver(new
URL("http://127.0.0.1:4723/wd/hub"), capabilities);
To Run on Real Device -
public WebDriver driver = null;
DesiredCapabilities capabilities = new
DesiredCapabilities();
capabilities.setCapability("device",
"iPhone");
capabilities.setCapability("udid",
"1234567890abcdef");
capabilities.setCapability("bundleid",
"com.example.appiumiphonetest");
capabilities.setCapability("ipa",
"MyiOSApp.ipa");
driver = new RemoteWebDriver( new
URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
There are two ways to get the “.app” of your application :
[1] You can build your app using xcode. Not down the build path of
your application – that would be the location where your ‘.app’ file will be
created.
Ex – I ran my build on iPhone simulator 4-inch , 7.0.3. My
‘.app’ was generated at the following path :
‘/Users/Library/Application Support/iPhone Simulator/7.0.3/Applications/’.
[2] If your app is in store :
- Download app from itunes store. Then open that file in finder.
- There will be one plist file -> you can get bundle id from
there.
BASIC SCRIPT -
package com.smriti.firstest;
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 FirstTest {
public WebDriver driver = null;
@BeforeMethod
public void setUp() throws Exception {
// set up appium
File appDir = new
File("/Users/abc/Desktop/smriti/iPhoneSimulator");
File app = new File(appDir, "XYZ.app");
DesiredCapabilities capabilities = new
DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,
"");
capabilities.setCapability(CapabilityType.VERSION,
"7.1");
capabilities.setCapability(CapabilityType.PLATFORM,
"Mac");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new RemoteWebDriver(new
URL("http://127.0.0.1:4723/wd/hub"), capabilities);
System.out.println("App launched");
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void testCases() throws InterruptedException {
String myname = "Smriti";
driver.findElement(By.xpath("//textfield[1]")).sendKeys(myname);
driver.findElement(By.name("Save")).click();
Thread.sleep(5000);
// write all your tests here
}
}
• I copied my
‘.app’ file from that location to a folder in my local here –
‘/Users/abc/Desktop/smriti/iPhoneSimulator. That is what it shows in
code :
File appDir = new
File("/Users/abc/Desktop/smriti/iPhoneSimulator");
File app = new File(appDir, "XYZ.app");
@BeforeMethod is for appium setup and app launch.
@Test is for writing all test cases after app launch .
@AfterMethod is after test cases are complete.
I have started using appium for native mobile iOS apps automation.
Let’s see how it goes
Comments
Post a Comment