Wednesday 25 July 2018

Native App Automation using Appium

In the Mobile world, we see multiple types of apps namely Native App, Hybrid App & the browser based application, Web App. Also, we see multiple types of operating systems like Android, iOS, Windows, Firefox OS etc.

Automated testing of these many variation of apps, is very tricky & difficult task. Not all of them are supported by single automation tool. Having said that, Appium which is built over Selenium webdriver supports most of the OS & apps with different configurations.

In this article, you would know a way to automate the Native App using AndroidDriver.

Prerequisites:

  1. Android SDK is downloaded and installed. (ANDROID_HOME env. variable should be set)
  2. Either Emulator or  Real device (USB debugging enabled) is available.
  3. Appium desktop app or non GUI package is installed. 

Steps:

  1. Create a java project in eclipse 'AppiumMobileTesting'.
  2. Create a class 'TestNativeApp' to add the automation code.
  3. Connect your device(USB debugging enabled), to system's USB port & accept if any confirmation pop ups.
  4. Start Appium Server. Once the device is connected, open the 'Appium Inspector Session' providing the suitable capabilities.
  5. Open the Native app in the device and you see the same screen on inspector session.
  6. Click on the required control in inspector session & then the related locator details are shown on the right hand side.

                      
                
  7. Add the below code snippet to the 'TestNativeApp' class which perform the addition of 2 digits and captures the result to print on the console.

public static void main(String[] args) {

 //Set the Desired Capabilities
 DesiredCapabilities caps = new DesiredCapabilities();
 caps.setCapability("deviceName", "MyDevice");
 caps.setCapability("udid", "G3WGLMA6217835"); //Give DeviceID of your mobile
 caps.setCapability("platformName", "Android");
 caps.setCapability("platformVersion", "6.0");
 caps.setCapability("noReset", true);
 caps.setCapability("appPackage", "com.android.calculator2");
 caps.setCapability("appActivity", "com.android.calculator2.Calculator");

 AndroidDriver<WebElement> driver = null;
 try {
 driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);

//Perform the actions on the required locators
driver.findElement(By.id("com.android.calculator2:id/clear")).click();
driver.findElement(By.id("com.android.calculator2:id/digit7")).click();
driver.findElement(By.id("com.android.calculator2:id/plus")).click();
driver.findElement(By.id("com.android.calculator2:id/digit9")).click();
driver.findElement(By.id("com.android.calculator2:id/equal")).click();
String txt = driver.findElement(By.className("android.widget.EditText")).getText();
System.out.println("Calculation Value:" + txt);
Thread.sleep(5000);
driver.quit();
 }catch (Exception e) {
   System.out.println(e.getMessage());
 }
}

   8. Execute the above script using eclipse.

That's it! You are done.
Now you can watch the Native App execution on the device.


Execution Video Below:





No comments:

Post a Comment