Monday, 10 August 2015

SELENDROID automation code for execting the Android application (.Apk)


//ApkName ====spsbranch.apk
//Version   =====com.youtility.attendance.ui:16.0.1


package nativeui;

import java.sql.Timestamp;
import io.selendroid.SelendroidDriver;
import io.selendroid.common.SelendroidCapabilities;
import io.selendroid.standalone.SelendroidConfiguration;
import io.selendroid.standalone.SelendroidLauncher;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import xlsx.work.Excel_ReadAndWrite;

public class SIteAuditSyncChechk {
    SendingEmail emailObj = new SendingEmail();
    Excel_ReadAndWrite excelrwObj = new Excel_ReadAndWrite();
    SelendroidLauncher selendroidServer = null;
    WebDriver driver = null;
    WebDriverWait wait;
   
    String syncSuccessMsg = "Synchronization successful!", actualSyncMsg;
    String errorMsg = "";
    int excelRowCount;
    String syncStatus = "";
   
    public void excelConfig(){
        String path = "InputData/AppInfo.xlsx";
        excelrwObj.fis_setXlsFilePath(path);
        excelrwObj.fis_setFileConfig();
        excelrwObj.fis_setWorkBookConfig();
        excelrwObj.fis_setWorkSheetConfig("SiteAuditInfo");
        excelRowCount = excelrwObj.fis_getRowCount();
        System.out.println("Row count : "+excelRowCount);
    }
    SelendroidCapabilities caps;
    public void appConfigaration(int i) throws Exception{
        try {
            SelendroidConfiguration config = new SelendroidConfiguration();
            config.addSupportedApp("src/resources/"+excelrwObj.fis_getCellValueByColumnName(i, "ApkName"));
            selendroidServer = new SelendroidLauncher(config);
            selendroidServer.launchSelendroid();
            caps = new SelendroidCapabilities (excelrwObj.fis_getCellValueByColumnName(i, "Version"));
            try {
                driver = new SelendroidDriver(caps);
                wait = new WebDriverWait(driver, 60);
            } catch (Exception e) {
                e.printStackTrace();
                errorMsg = errorMsg + e;
            }
        } catch (Exception e) {
            System.out.println("Error :- Unable to configure and launch the Application.");
            errorMsg = errorMsg + e;
            e.printStackTrace();
        }
        if(!driver.findElement(By.xpath("//EditText[@id='server_url']")).isDisplayed()){
            driver = new SelendroidDriver(caps);
            System.out.println("Second attempt.");
        }
    }
   
    public void appServerDetailsEnter(int i){
        try {
            driver.findElement(By.xpath("//EditText[@id='server_url']")).clear();
            driver.findElement(By.xpath("//EditText[@id='server_url']")).sendKeys(excelrwObj.fis_getCellValueByColumnName(i, "serverURL"));
            driver.findElement(By.xpath("//*[@id='server_db']")).clear();
            driver.findElement(By.xpath("//*[@id='server_db']")).sendKeys(excelrwObj.fis_getCellValueByColumnName(i, "DatabaseName"));
            driver.navigate().back();
            driver.findElement(By.xpath("//Button[@value='Save']")).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//EditText[@id='username']")));
        } catch (Exception e) {
            System.out.println("Error :- Unable to set the server configaration to Application.");
            errorMsg = errorMsg + e;
            e.printStackTrace();
        }
    }
    public void appLogin(int i){
        try {
            driver.findElement(By.xpath("//EditText[@id='username']")).clear();
            driver.findElement(By.xpath("//EditText[@id='username']")).sendKeys(excelrwObj.fis_getCellValueByColumnName(i, "userid"));
            driver.findElement(By.xpath("//EditText[@id='password']")).clear();
            driver.findElement(By.xpath("//EditText[@id='password']")).sendKeys(excelrwObj.fis_getCellValueByColumnName(i, "password"));
            driver.navigate().back();
            driver.findElement(By.xpath("//Button[@id='btnLogin']")).click();
        } catch (Exception e) {
            System.out.println("Error :- Unable to login into Application.");
            errorMsg = errorMsg + e;
            e.printStackTrace();
        }
    }
    long estimatedTime = 0;
    public void syncStatus(){
        long startTime = System.nanoTime();
        System.out.println("StartTime in nano seconds : "+startTime);
        try {
            wait = new WebDriverWait(driver, 10000);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//Button[@id='button1']")));
            estimatedTime = System.nanoTime() - startTime;
            actualSyncMsg = driver.findElement(By.xpath("//TextView[@id='message']")).getText().trim();
            System.out.println("Message : "+actualSyncMsg);
            if(actualSyncMsg.equalsIgnoreCase(syncSuccessMsg)) syncStatus = "True";
            driver.findElement(By.xpath("//Button[@id='button1']")).click();
        } catch (Exception e) {
            System.out.println("Error :- Unable to validate the synchronization.");
            errorMsg = errorMsg + e;
            e.printStackTrace();
        }
        System.out.println("EstimatedTime in nano seconds : "+estimatedTime);
    }
    public void appDataClear(){
         try {
            driver.quit();
            selendroidServer.stopSelendroid();
        } catch (Exception e) {
            System.out.println("Error :-  Unable to clear the application data");
            errorMsg = errorMsg + e;
            e.printStackTrace();
        }
    }
    public void statusUpdateInExcel(int i){
        if(syncStatus.equalsIgnoreCase("True")) excelrwObj.fis_setCellValueAndColorByColumnName(i, "LastExeStatus", "Success", IndexedColors.GREEN, IndexedColors.WHITE);
        else excelrwObj.fis_setCellValueAndColorByColumnName(i, "LastExeStatus", "Fail", IndexedColors.RED, IndexedColors.WHITE);
        java.util.Date date= new java.util.Date();
        excelrwObj.fis_setCellValueByColumnName(i, "SyncConsumedTime", ""+estimatedTime);
        excelrwObj.fis_setCellValueByColumnName(i, "ActualMessage", actualSyncMsg);
        excelrwObj.fis_setCellValueByColumnName(i, "ErrorLog", errorMsg);
        excelrwObj.fis_fileSaveAndClose();
        emailObj.mailSending(excelrwObj, i);
    }
   
   
   
    @Test
    public void siteAuditAppSyncCheck(){
        excelConfig();
        for (int i = 2; i <= excelRowCount; i++) {
            actualSyncMsg = "Error"; syncStatus = "False";
            try {
                if(!excelrwObj.fis_getCellValueByColumnName(i, "ExeStatus").equalsIgnoreCase("Yes")) continue;
                appConfigaration(i);
                appServerDetailsEnter(i);
                appLogin(i);
                syncStatus();
                appDataClear();
                statusUpdateInExcel(i);
            } catch (Exception e) {
                appDataClear();
                errorMsg = errorMsg + e;
                statusUpdateInExcel(i);
                e.printStackTrace();
                System.out.println("Error :- Unable to validate the Application sync");
            }
            /*driver.quit();
            selendroidServer.stopSelendroid();*/
        }
    }//Test closed
}//Class closed








No comments:

Post a Comment