Appium : Getting started with Appium
|Appium Tool is basically used for automation testing of mobile applications that is not limited to only web applications or native applications , it has support for hybrid app also. It has support for android,iOS as well as for the latest addition to mobile OS Firefox.
Requirements Analysis :
For Android:
- Mac OSX 10.7+ or Windows 7+ or Linux.
- Android SDK >= 16
For iOS:
- Mac OSX 10.7+ (Recommended 10.8+)
- Xcode 4.5+ with Command Line Tools
Getting Started:
- Download android SDK and set necessary environment variables.
- Download Appium according to OS from here.
Start the server by running appium and figure out the IP on which the server has been started.
Or
Alternatively Appium server can also be started using Node.js for earlier versions of Appium , that is also available to download.Install Node.js, Run reset.bat in the directory ‘Appiumnode_modulesappium’ from cmd and then run ‘node server.js’ . It will start the server and note down the IP. - Pick up your favorite Java IDE to start with the project . Lets assume we are using Eclipse.
- Writing Code:
For Android:
Example:
public class TestScript { public static WebDriver driver; public static Runtime rt = Runtime.getRuntime(); public static Process proc; @Before public void setUp() throws Exception { File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "\app"); File app = new File(appDir, "installer file e.ginstaller.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); //capabilities.setCapability("device","devicename/emulator_name"); capabilities.setCapability("device","appiumtest"); //capabilities.setCapability("device","selendroid"); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability(CapabilityType.VERSION, "4.3"); capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("app-package", "com.package.name"); capabilities.setCapability("app-activity", ".screen_name"); driver = new SwipeableWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);-------> Mention the IP on which server started here. // Implicitly wait for 30 seconds for application to open //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void apiDemo() throws InterruptedException{ /* Login process */ /* // Input login WebElement enterLogin = driver.findElements(By.tagName("textfield")).get(0); enterLogin.clear(); enterLogin.clear(); enterLogin.sendKeys("abcd"); } @SuppressWarnings("deprecation") public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen { private RemoteTouchScreen touch; public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) { super(remoteAddress, desiredCapabilities); touch = new RemoteTouchScreen(getExecuteMethod()); } public TouchScreen getTouch() { return touch; } }
For iOS:
Example:
public class class_name {
private WebDriver driver;
@Before
public void setUp() throws Exception {
//Appium needs the path of app build
//Set up the desired capabilities and pass the iOS SDK version and app path to Appium
File app = new File("/Users/user_name/Desktop/../build/Release-iphonesimulator/appname.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());
//Create an instance of RemoteWebDriver and connect to the Appium server.
//Appium will launch the App in iPhone Simulator using the configurations specified in Desired Capabilities
driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
}
@Test
public void testapplication() throws Exception {
//iOS controls are accessed through WebElement class
//Locate the Height & Weight textField by their accessibility labels using By.name()
WebElement heightTextField = driver.findElement(By.name("Height"));
heightTextField.sendKeys("181");
WebElement weightTextField = driver.findElement(By.name("Weight"));
heightTextField.sendKeys("80");
//Locate and tap on Calculate button using the click() method
WebElement calculateButton = driver.findElement(By.name("Calculate"));
calculateButton.click();
//Locate all the label elements using By.tagName()
List<WebElement> labels = driver.findElements(By.tagName("staticText"));
//Check the calculated Bmi and Category displayed on labels
//Label with index 8 has value of the Bmi and index 9 has the value for category
assertEquals("24.42",labels.get(8).getText());
assertEquals("Normal",labels.get(9).getText());
}
@After
public void tearDown() throws Exception {
//Close the app and simulator
driver.quit();
}
}
Desired Capabilities can be configured to locate the app otherwise we need to give the app path while starting Appium server, that can be figured out with the UI.
For iOS applications if we want to run test scripts with iOS Simulator then we need the .app build of the xcode project that can be found inside the ios simulator application.
If its a real iPhone then we have to mention Bundle id and package details either with desired capabilities or with the GUI provided with applium app. And one more important fact is iPhone device should be authenticated/paired with Xcode installed.
For more details on how to target various elements on the screen please follow this link.
The most robust nature of Appium is that if even the platform is different (android/ios/firefox)and the UI of applications is same then we can use the same script to run with all the applications. Only change we need is setting up desired capabilities in the project.
Current Limitations:
- Can’t run iOS test scripts on Windows or any other platforms except Mac.
- Can’t use older versions of Windows or Mac.
- Bring the auto Sync magic of Protractor to Selenium with Java - October 23, 2015
- Restarting Appium Server could improve various server freezing issues and may improve execution time - January 20, 2015
- Appium with iOS 8 and XCode 6 : What’s new? - November 5, 2014
- REST API automation testing using Apache HttpClient – The Approach - October 3, 2014
- An Overview of mobile application : Moving forward to automation - October 1, 2014
- An introduction to REST - September 29, 2014
- Run ChromeDriver with Chrome Driver Service to reduce script execution time significantly - September 26, 2014
- Selenium WebDriver – Get Cookies from an existing session and add those to a newly instantiated WebDriver browser instance. - September 26, 2014
- Simulate Copy Paste action using Java Robot and Clipboard class - September 26, 2014
- Android : How to test if Android Application has memory leaks - August 11, 2014