Mobile Automation using Appium and Java (Page Objects)


********************************Main Test File:-*************************************************************
package com.example;
 import io.appium.java_client.android.AndroidDriver;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.remote.DesiredCapabilities;
 import org.openqa.selenium.support.ui.ExpectedConditions;
 import org.openqa.selenium.support.ui.WebDriverWait;
 import org.testng.Assert;
 import org.testng.annotations.AfterTest;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;

import com.example.pages.HannaPagesTest;
 public class HannaTest 
 {
  AndroidDriver driver;
  @BeforeTest
  public void setUp() throws MalformedURLException 
  {
      
      DesiredCapabilities capabilities = new DesiredCapabilities();
      capabilities.setCapability("deviceName", "ZX1B32FFXF");
      capabilities.setCapability("appPackage", "com.hannainst.hannalab");
      capabilities.setCapability("appActivity","com.hannainst.hannalab.ManagerActivity");
      driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      WebDriverWait wait = new WebDriverWait(driver, 300);
      wait.until(ExpectedConditions.elementToBeClickable(By.className("android.widget.RelativeLayout")));
      
}
  @Test(priority = 0)
  public void checkAppElementPresent() 
  {
   HannaPagesTest hanna=new HannaPagesTest(driver); 
   if(hanna.verifyResult1("No Probe Connected"))
    System.out.println("App Launch TC Passed");
   else
    System.out.println("App Launch TC Failed");
      
  }
  
  @Test(priority = 1)
  public void CheckAppVersion() 
  {
   HannaPagesTest hanna=new HannaPagesTest(driver); 
   if(hanna.verifyResult2("Version 2.0.0 Beta"))
    System.out.println("App Version TC Passed");
   else
    System.out.println("App Version TC Failed");
      
  }
  
  
  @AfterTest
  public void End() throws IOException 
  {
   
   //driver.removeApp("com.hannainst.hannalab");
   driver.resetApp();
   driver.quit();  
   //driver.close();
  }
 }


************************Page Object Test File************************************
package com.example.pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class HannaPagesTest 
{
 
 public HannaPagesTest(AndroidDriver driver)
 {
  PageFactory.initElements( new AppiumFieldDecorator(driver), this);
 }
 @AndroidFindBy(name="No Probe Connected")
 public static WebElement text1;
 
 @AndroidFindBy(xpath="//*[@class='android.widget.ImageButton' and @content-desc='Navigate up']")
 public static WebElement text2;
 
 @AndroidFindBy(id="com.hannainst.hannalab:id/versionName")
 public static WebElement text3;
 
 public boolean verifyResult1(String result)
 {
  if(text1.getText().equals(result))
   return true;
   else
   return false;
 } 
 
 public boolean verifyResult2(String result)
 {
  text2.click();
  if(text3.getText().equals(result))
   return true;
   else
   return false;
 } 
   
 }

***************************************************************************************************************************