Tuesday, 9 December 2014

How to use an external Excel Sheet as an object repository in RFT?

In my previous post, have explained about how we can find an object iteratively with some max timeout in application. In continuation will be explaining how we can use an excel sheet as an object repository for storing object properties, instead of capturing the object in object map. The object properties can be read from excel sheet and can be used to find the object in application at runtime.

We need to maintain an excel sheet as an external object repository in below format:


 In External Object Sheet we have 5 Columns namely :

1. OBJECT_NAME : The Name of the object through which it will be identified  in test scripts.
2. PropertyName1 : The first locator property name for the object through which we will be identifying the object. Generally this property is “.class”
 3. PropertyValue1: The Value for property 1.
 4. PropertyName2: The socond locator property name for the object through which we will be identifying the object. This property usually uniquely identify the object in application but not necessarily always. Generally used are : .id , .name , .text , for etc.
 5. PropertyValue2: The Value for Property 2.

Suppose this external sheet reside in below path in the project:
 SheetPath = “C://…….//ExternalObjectSheet.xls” and sheet name is “ObjectProperties” in the workbook. This information should be defined as application environment variable in the project.

We will be using Apache Poi API for accessing the excel sheets. 

We will be first try to find the object in OBJECT_NAME column and the read the corresponding property names and values for the object in respective array.

Now find the root test object as “rootObject” in the screen with the help of getRootTestObject () method.

 Now pass this rootObject along with property name and property value array to function find_object_iteratively which will return the test object  array. Return the 1st element of test object array.


public TestObject LoadObjectFromExternalObjectSheet(String ObjectName) throws Exception
                {                             
                                String SheetPath = “”; // Retrieve from Application Environment Variable of project.
                                String sheetName = ""; // Retrieve from Application Environment Variable of project.
// Create
                                Workbook dataWB;
                                String ObjRow = "";
                                String [] PropName = new String [2];
                                String [] PropValue = new String [2];
                                int ObjectFoundInSheetFlag = 0;
                                try{
                                                if(SheetPath.contains(".xlsx")){
                                                                dataWB = new XSSFWorkbook(SheetPath);
                                                }
                                                else{                                                     
                                                                InputStream inp = new FileInputStream(SheetPath);
                                                                dataWB = (Workbook) new HSSFWorkbook(new POIFSFileSystem(inp));                                                                             
                                                }
                                                Sheet sheet = null;
                                                try{
                                                                sheet = dataWB.getSheet(sheetName.toString());
                                                }
                                                catch(Exception e1){
                                                                throw new Exception("Sheet not present in the excel");
                                                }
                                                int noOfRows = sheet.getLastRowNum(); // row count of dataSheet                                                     
                                                if(noOfRows == 0){
                                                                throw new Exception("no rows present");
                                                }
                                                Row row = null;
                                                for (int i=1;i<(noOfRows+1);i++)
                                                {
                                                                row=sheet.getRow(i);
                                                               
                                                                if(row == null){
                                                                                continue;
                                                                }

                                                                try{
                                                                                ObjRow = row.getCell(findCol(sheet, "OBJECT_NAME")).toString().trim();                                                                          
                                                                }
                                                                catch(Exception e1){
                                                                                ObjRow= "";
                                                                               
                                                                }
                                                                if(ObjRow.equalsIgnoreCase(ObjectName))
                                                                {
                                                                                ObjectFoundInSheetFlag=1;
                                                                                PropName[0]=GetColumnValue(sheet,"PropertyName1",row);
                                                                                PropName[1]=GetColumnValue(sheet,"PropertyName2",row);
                                                                                //PropName[2]=GetColumnValue(sheet,"PropertyName3",row);
                                                                                PropValue[0]=GetColumnValue(sheet,"PropertyValue1",row);
                                                                                PropValue[1]=GetColumnValue(sheet,"PropertyValue2",row);
                                                                                //PropValue[2]=GetColumnValue(sheet,"PropertyValue3",row);                                                           
                                                                }
                                                }
                                                if(ObjectFoundInSheetFlag == 0)
                                                {
                                                                logTestResult ("Object:"+ObjectName+" Not found in External object File",false);
                                                                return null;
                                                }
                                                if ((PropName[0].equalsIgnoreCase(""))&&(PropName[1].equalsIgnoreCase(""))||((PropValue[0].equalsIgnoreCase(""))&&(PropValue[1].equalsIgnoreCase(""))))
                                                {
                                                                logTestResult ("Object Properties not defined in External object File for object"+ObjectName,false);
                                                                return null;
                                                }
                                                else
                                                {
                                                                RootTestObject rootObject = getRootTestObject();
                                                                TestObject [] foundObjs = find_object_iteratively (rootObject,PropName,PropValue);
                                                                return foundObjs[0];    
                                                }                                                             
                                }
                                catch(NullPointerException e){
                                                return null;
                                }
                                catch (ObjectNotFoundException e2){
                                                logTestResult ("Object not found Exception:"+e2.getMessage(),false);
                                                return null;
                                }
                                catch (FileNotFoundException e) {
                                                logTestResult ("There is no data excel file in the specified path: " + "'"+SheetPath+"'",false);
                                                throw new Exception("File Not Found");
                                }
                                catch(Exception e) {
                                                logTestResult ("Exception :"+e.getMessage()+" in keyword GetObjectFromExternalObjectSheet using object ",false);
                                                return null;
                                }                             
                }

Please feel free to share your thoughts and suggestion on the approach.

Thanks and Regards,
Anupam
er.anupamkumar@gmail.com

No comments:

Post a Comment