Monday, August 29, 2016

Driverscript

 /**  
  * @author:Soumya.D  
  *   
  * Contains Code for driving the testcase execution  
  */  
 package com.hr.generic;  
 import java.io.File;  
 import java.lang.reflect.Method;  
 import java.text.SimpleDateFormat;  
 import java.util.ArrayList;  
 import java.util.Date;  
 import java.util.HashMap;  
 import java.util.List;  
 import java.util.Map;  
 import net.sf.sahi.client.Browser;  
 import org.testng.annotations.Test;  
 import config.Constants;  
 import com.hr.automation.utilities.Property;  
 import com.hr.automation.utilities.ReadExcelFile;  
 public class DriverScript {  
 public static GenericActionKeywords genericActionKeywords;  
 public static DmsActionKeywords dmsactionKeywords;  
 public static EippActionKeywords eippActionKeywords;  
 public static DisActionKeywords disActionKeywords;  
      public static boolean bResult;  
      public static String sTestCaseID;  
      public static String sRunMode;  
      public static String sTestCaseName;  
      public static int iTestStep;  
      public static int iTestLastStep;  
      public static String sActionKeyword;  
      public static String sData;  
      public static String sObjIdentifier;  
      public static String sEnvironment;  
      public static Method method;  
      public static HashMap<String,String> dynamicValue = new HashMap<String, String>();  
      public static String comments ="";   
      static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
      static Date startAt=null,endAt=null;  
      static String startTime="",endTime="";  
      public static String baseProjectPath=System.getProperty("user.dir");  
       public static Property configProps=new Property("config.properties");  
       public static RecoveryScenarios recoveryScenario=new RecoveryScenarios();  
       public static Browser browser;  
       public static String sdynamicValueName;  
      @Test  
      public void driverScript() throws Throwable  
      {  
           try  
           {   
                genericActionKeywords = new GenericActionKeywords();  
                dmsactionKeywords = new DmsActionKeywords();  
                disActionKeywords= new DisActionKeywords();  
                eippActionKeywords = new EippActionKeywords();  
                //******Code to read list of excels that need to be executed*******  
                ReadExcelFile readTestCase=new ReadExcelFile();  
                List<Map<String, String>> sheetsToRunList = new ArrayList<Map<String, String>>();  
                HashMap<String,String> sheetToRun = new HashMap<String, String>();  
                sheetsToRunList=readTestCase.getExcelRecords(Constants.Path_TestData+"SheetsToRun.xls",Constants.Sheet_SheetsToRun);  
                if(sheetsToRunList.equals(null))//TODO EOF  
                {  
                     Log.error("Class Utils | Method driverScript | Exception desc : getExcelRecords returns null");  
                      return ;  
                }  
                String sDestPath="";  
                for(int iSheetsToRunList=0;iSheetsToRunList<sheetsToRunList.size();iSheetsToRunList++)  
                {  
                     sheetToRun=(HashMap<String, String>) sheetsToRunList.get(iSheetsToRunList);  
                     sDestPath=Constants.Path_Results+sheetToRun.get("SheetToRun")+"_"+CommonKeywords.getStartTime()+".xls";  
                     ExcelUtils.fileCopy(Constants.Path_TestData+sheetToRun.get("SheetToRun")+".xls",sDestPath);  
                     System.out.println("SheetToRun "+sheetToRun.get("SheetToRun"));  
                     configProps.setProperty("DestPath", sDestPath);  
                     ExcelUtils.setExcelFile(sDestPath);  
                     //to get envi from SheetToRun sheet  
                     sEnvironment=sheetToRun.get("Environment");  
                     configProps.setProperty("ENVIRONMENT",sEnvironment);  
                     System.out.println("env"+sEnvironment);  
                     execute_TestCase();  
                }  
                configProps.setProperty("URL", "Null");  
                dynamicValue.clear();  
           }  
                catch(Exception e)  
                {  
                     e.printStackTrace();  
                     Log.error("Class Utils | Method driverScript | Exception desc : "+e.getMessage());  
                     return;  
                }  
      }  
      private void execute_TestCase() throws Throwable  
      {  
           int iTotalTestCases = ExcelUtils.getRowCount(Constants.Sheet_TestCases);  
           System.out.println("iTotalTestCases "+iTotalTestCases);  
           for(int iTestcase=1;iTestcase<iTotalTestCases;iTestcase++)  
           {  
                bResult = true;  
                sTestCaseID = ExcelUtils.getCellData(iTestcase, Constants.Col_TestCaseID, Constants.Sheet_TestCases);  
                sRunMode = ExcelUtils.getCellData(iTestcase, Constants.Col_RunMode,Constants.Sheet_TestCases);  
                sTestCaseName = ExcelUtils.getCellData(iTestcase, Constants.Col_TestCaseName,Constants.Sheet_TestCases);  
                //sEnvironment=ExcelUtils.getCellData(iTestcase, Constants.Col_ENVIRONMENT,Constants.Sheet_TestCases);  
                //configProps.setProperty("ENVIRONMENT",sEnvironment);  
                if(sRunMode.equalsIgnoreCase("YES"))  
                {  
                     Log.startTestCase(sTestCaseID);  
                     startAt=new Date();  
                     startTime = dateformat.format(startAt);  
                     configProps.setProperty("TestCaseName", sTestCaseName);  
                     iTestStep = ExcelUtils.getRowContains(sTestCaseID, Constants.Col_TestCaseID, Constants.Sheet_TestSteps);  
                     iTestLastStep = ExcelUtils.getTestStepsCount(Constants.Sheet_TestSteps, sTestCaseID, iTestStep);  
                     bResult=true;  
                     for (;iTestStep<iTestLastStep;iTestStep++)  
                     {  
                          sActionKeyword = ExcelUtils.getCellData(iTestStep, Constants.Col_ActionKeyword,Constants.Sheet_TestSteps);  
                          sData = ExcelUtils.getCellData(iTestStep, Constants.Col_TestData, Constants.Sheet_TestSteps);  
                          sObjIdentifier= ExcelUtils.getCellData(iTestStep, Constants.Col_ObjIdentifier, Constants.Sheet_TestSteps);  
                          sdynamicValueName=ExcelUtils.getCellData(iTestStep, Constants.Col_DynamicValueName, Constants.Sheet_TestSteps);  
                          execute_Actions();  
                          if(bResult==false)  
                          {  
                               endAt=new Date();  
                               endTime = dateformat.format(endAt);  
                               ExcelUtils.setCellData(Constants.KEYWORD_FAIL,iTestcase,Constants.Col_Result,Constants.Sheet_TestCases);  
                               ExcelUtils.setCellData(startTime,iTestcase,Constants.Col_StartTime,Constants.Sheet_TestCases);  
                               ExcelUtils.setCellData(endTime,iTestcase,Constants.Col_EndTime,Constants.Sheet_TestCases);  
                               Log.endTestCase(sTestCaseID);            
                               genericActionKeywords.reLogin();  
                               break;  
                          }                                
                     }  
                     if(bResult==true)  
                     {  
                          endAt=new Date();  
                          endTime = dateformat.format(endAt);  
                          ExcelUtils.setCellData(Constants.KEYWORD_PASS,iTestcase,Constants.Col_Result,Constants.Sheet_TestCases);  
                          ExcelUtils.setCellData(startTime,iTestcase,Constants.Col_StartTime,Constants.Sheet_TestCases);  
                          ExcelUtils.setCellData(endTime,iTestcase,Constants.Col_EndTime,Constants.Sheet_TestCases);  
                          Log.endTestCase(sTestCaseID);       
                     }                           
                }  
           }  
      }  
      private static void execute_Actions() throws Throwable   
       {  
           try  
           {  
                ArrayList<String> ObjectIdentifierList = new ArrayList<String>();  
                String []objectIdentifiers=null;  
                ArrayList<String> TestDataList = new ArrayList<String>();  
                String []testData=null;  
                Class<?>[] paramTypeArrayLsit=null;  
                Object[] paramArrayData ;  
                if(sObjIdentifier.contains(","))  
                {  
                     objectIdentifiers=sObjIdentifier.split(",");  
                     for(int i=0;i<objectIdentifiers.length;i++)  
                     {  
                          ObjectIdentifierList.add(i, objectIdentifiers[i]);  
                     }  
                }  
                else  
                {  
                     if(sObjIdentifier.equals(""))  
                     {}  
                     else  
                     {  
                     ObjectIdentifierList.add(0, sObjIdentifier);  
                     }  
                }  
                if(sData.contains(","))  
                {  
                     testData=sData.split(",");  
                     for(int i=0;i<testData.length;i++)  
                     {  
                          TestDataList.add(i, testData[i]);  
                     }  
                     //code to read the test data and replace dynamic value  
                     int index=0;  
                     if(sData.contains("dynamicValue") )  
                     {  
                          for(int lCount=0;lCount<TestDataList.size();lCount++)  
                          {  
                             if(TestDataList.get(lCount).contains("dynamicValue") )  
                             {  
                                 String dynamicSplitValue[]=TestDataList.get(lCount).split("~");  
                                 String data=dynamicSplitValue[1];  
                                 TestDataList.set(lCount, dynamicValue.get(data));  
                             }  
                         }  
                     }  
                }  
                else  
                {  
                     if(sData.equals(""))  
                     {}  
                     else  
                     {  
                          TestDataList.add(0, sData);  
                          if(TestDataList.get(0).contains("dynamicValue") )  
                             {  
                                 String dynamicSplitValue[]=TestDataList.get(0).split("~");  
                                 String data=dynamicSplitValue[1];  
                                 TestDataList.set(0, dynamicValue.get(data));  
                             }  
                     }  
                }  
                //Removed repeated steps for every product:By Sadguna  
                if(ObjectIdentifierList.size() ==0 && TestDataList.size()==0)  
                {  
                     paramTypeArrayLsit =new Class[]{};  
                     paramArrayData= new ArrayList<?>[]{};  
                }  
                else if(ObjectIdentifierList.size() >0 && TestDataList.size()>0)  
                {  
                     paramTypeArrayLsit = new Class[]{ArrayList.class,ArrayList.class};       
                     paramArrayData= new ArrayList<?>[]{ObjectIdentifierList,TestDataList};  
                }  
                else if(ObjectIdentifierList.size() >0 && TestDataList.size()==0)  
                {  
                     paramTypeArrayLsit = new Class[]{ArrayList.class};       
                     paramArrayData=new ArrayList<?>[]{ObjectIdentifierList};  
                }  
                else  
                {  
                     paramTypeArrayLsit = new Class[]{ArrayList.class};       
                     paramArrayData=new ArrayList<?>[]{TestDataList};  
                }                 
                //Switch to call methods  
                String []numOfProducts = {"dms","eipp","dis"};  
                int products;  
                for(products=0;products<numOfProducts.length;products++)  
                {  
                     if(sActionKeyword.contains(numOfProducts[products]))  
                          break;  
                }  
                     switch(products)  
                     {  
                     case 0:  
                          Class<?> dmsActionKeyword_obj = dmsactionKeywords.getClass();  
                          method = dmsActionKeyword_obj.getMethod(sActionKeyword,paramTypeArrayLsit );  
                          method.invoke(dmsactionKeywords,paramArrayData);  
                          break;  
                     case 1:  
                          Class<?> eippActionKeyword_obj = eippActionKeywords.getClass();  
                          method = eippActionKeyword_obj.getMethod(sActionKeyword,paramTypeArrayLsit );  
                          method.invoke(eippActionKeywords,paramArrayData);  
                          break;  
                     case 2:  
                          Class<?> disActionKeyword_obj = disActionKeywords.getClass();  
                          method = disActionKeyword_obj.getMethod(sActionKeyword,paramTypeArrayLsit );  
                          method.invoke(disActionKeywords,paramArrayData);  
                          break;       
                     default:  
                          Class<?> actionKeyword_obj = genericActionKeywords.getClass();  
                               method = actionKeyword_obj.getMethod(sActionKeyword,paramTypeArrayLsit );  
                               method.invoke(genericActionKeywords,paramArrayData);  
                     }  
                          if(bResult==true){  
                               ExcelUtils.setCellData(Constants.KEYWORD_PASS, iTestStep, Constants.Col_TestStepResult, Constants.Sheet_TestSteps);  
                               if(!comments.isEmpty())  
                               {  
                                    ExcelUtils.setCellData(comments,iTestStep,Constants.Col_Comments,Constants.Sheet_TestSteps);  
                               }  
                          }  
                          else{  
                               ExcelUtils.setCellData(Constants.KEYWORD_FAIL, iTestStep, Constants.Col_TestStepResult, Constants.Sheet_TestSteps);  
                               if(!comments.isEmpty())  
                               {  
                                    ExcelUtils.setCellData(comments,iTestStep,Constants.Col_Comments,Constants.Sheet_TestSteps);  
                               }  
                               //CAll to take screen shot method  
                               takeScreenshot();  
                               //GenericActionKeywords.closeBrowser();  
                               }                           
                          comments="";  
           }  
           catch(Exception e)  
           {  
                e.printStackTrace();  
                DriverScript.bResult = false;  
                Log.error("Class Utils | Method execute_Actions | Exception desc : "+e.getMessage());  
                 return ;  
           }  
       }  
      /**By Divya on Jun 23   
       * To capture the screen shot of the test execution on failure*/  
      public static void takeScreenshot() throws Throwable {  
           try {  
                String filePath = baseProjectPath + "/Screenshots/"  
                          + DriverScript.sTestCaseName +CommonKeywords.getStartTime()+ ".jpg";  
                File file = new File(filePath);  
                if (file.exists())  
                     file.delete();  
                browser.focusWindow();  
                browser.takeScreenShot(filePath);  
                Thread.sleep(1000);  
                if (file.exists())  
                     ExcelUtils.setCellData(filePath, DriverScript.iTestStep,  
                               Constants.Col_ScreenShotonFAILURE,  
                               Constants.Sheet_TestSteps);  
           } catch (Exception e) {  
                e.printStackTrace();  
                DriverScript.bResult = false;  
           }  
      }  
 }  

2 comments: