Wednesday, 24 June 2015

Sending E-Mail

public void mailSending(int i){
        final String username = "User name";
        final String password = "Password";
       
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

        try {
             Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from mail@gmail.com"));//From mail-id
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(excelrwObj.fis_getCellValueByColumnName(i, "mailTo")));//To mail-id
            message.setSubject("Re: Apllication sync status");
            message.setText("Hi, \n\n"+
                    "Application Name : "+excelrwObj.fis_getCellValueByColumnName(i, "ApkName")+
                    ",\nVersion info : "+excelrwObj.fis_getCellValueByColumnName(i, "Version")+
                    ",\nUser Name : "+excelrwObj.fis_getCellValueByColumnName(i, "userid")+
                    ",\nPassword : "+excelrwObj.fis_getCellValueByColumnName(i, "password")+
                    ",\nSync status : "+excelrwObj.fis_getCellValueByColumnName(i, "LastExeStatus")+
                    ",\nRun At : "+excelrwObj.fis_getCellValueByColumnName(i, "LastRunAt")+
                    "\n\nRegards, \nYotility Team.");

            Transport.send(message);
             System.out.println("Done");
         } catch (Exception e) {
            e.printStackTrace();
        }
    }

Tuesday, 23 June 2015

Selendroid Sample program Config & Run

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 {
   
    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);
    }
   
    public void appConfigaration(int i){
        try {
            SelendroidConfiguration config = new SelendroidConfiguration();
            config.addSupportedApp("src/resources/"+excelrwObj.fis_getCellValueByColumnName(i, "ApkName"));
            selendroidServer = new SelendroidLauncher(config);
            selendroidServer.launchSelendroid();
            SelendroidCapabilities caps = new SelendroidCapabilities (excelrwObj.fis_getCellValueByColumnName(i, "Version"));
            try {
                driver = new SelendroidDriver(caps);
                wait = new WebDriverWait(driver, 100000);
            } 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();
        }
    }
   
    public void appServerConfig(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();
        }
    }
    public void syncStatus(){
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//Button[@id='button1']")));
            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();
        }
    }
    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, "LastRunAt", new Timestamp(date.getTime()).toString());
        excelrwObj.fis_setCellValueByColumnName(i, "ActualMessage", actualSyncMsg);
        excelrwObj.fis_setCellValueByColumnName(i, "ErrorLog", errorMsg);
        excelrwObj.fis_fileSaveAndClose();
    }
   
    @Test
    public void siteAuditAppSyncCheck(){
        excelConfig();
        for (int i = 2; i <= excelRowCount; i++) {
            actualSyncMsg = "Nill"; syncStatus = "False";
            try {
                if(!excelrwObj.fis_getCellValueByColumnName(i, "ExeStatus").equalsIgnoreCase("Yes")) continue;
                appConfigaration(i);
                appServerConfig(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");
            }
        }
       
    }//Test closed
}//Class closed