Wednesday, June 29, 2016

Read Excel using Groovy

import groovy.xml.*
import jxl.*
import jxl.write.*

//def Temp_Array_Length = 0
//********************* Excel Sheet Path *******************************

def projectDir = context.expand('${projectDir}');
Path= projectDir +"\\TestData"+ "\\Update.xls" // Excel Sheet Location

log.info "Project Path" + Path
Workbook workbook1 = Workbook.getWorkbook(new File(Path)) // Read Excel Sheet
Sheet sheet1 = workbook1.getSheet('CreateUpdate') // Sheet Number

def GET_REQUEST_XML = testRunner.getTestCase()
int rows = 0;
int cols = 0;
rows = sheet1.getRows(); // Rows Count
cols = sheet1.getColumns();// Columns Count
//log.info "Row Count--------------" + rows
//log.info "Cols Count--------------" + cols
def data_array = new String[cols]



for(i=0;i<rows;i++)
{
                for(j=0;j<cols;j++)
                {
                                if(i==0)// Reading First Line from Excel Sheet
                                {
                                  Cell a1 = sheet1.getCell(j,0)
                            def data1 = a1.getContents()
                          //log.info data1
                               if(!GET_REQUEST_XML.hasProperty(data1)){
                                GET_REQUEST_XML.addProperty(data1)
                               }
                                data_array[j] = data1
                                }
                                else{
                        Cell a2 = sheet1.getCell(j,i)
                          def data2 = a2.getContents()
                          value1 = data_array[j]
                          testRunner.testCase.setPropertyValue( value1 , data2 )
                         
                                }
                }


           
                if(i!=0)
                {
                def RunStatus = testRunner.testCase.getPropertyValue('RunStatus')
                if(RunStatus=='true')
                {
               
testRunner.testCase.setPropertyValue( 'email' , testRunner.testCase.testSuite.project.getPropertyValue('email') )
testRunner.runTestStepByName('Authentication') // Running Auth Services
try{
Thread.currentThread().sleep(1000)
}
catch(Exception e){}
def str = testRunner.testCase.getPropertyValue('Parameter')// Reading Prameter from Excel Sheet
def values = str.split(',')
int charCount = str.replaceAll("[^,]", "").length(); // finding lenget of parameter
log.info (charCount);
for( x=0;x<=charCount;x++)
{
//${#TestCase#Name}
valueParameter= '${#TestCase#' + values[x] + '}'// find  Value
//log.info "Parameters :  " + values[x]
testRunner.testCase.getTestStepByName('Service').httpRequest.setPropertyValue(values[x],valueParameter) // Adding Value and Name for Services

}
if(testRunner.testCase.getPropertyValue('scope')=='groups')
{
testRunner.testCase.setPropertyValue( 'actorList' , testRunner.testCase.testSuite.project.getPropertyValue('groupId') )
}
if(testRunner.testCase.getPropertyValue('scope')=='private')
{
testRunner.testCase.setPropertyValue( 'actorList' , testRunner.testCase.testSuite.project.getPropertyValue('ActorId') )
}

// Fetching URL form Project Level for Different Environment
ServiceURL=testRunner.testCase.testSuite.project.getPropertyValue('url')+ testRunner.testCase.getPropertyValue('EndPoint')
testRunner.testCase.setPropertyValue( 'Service URL' , ServiceURL )

testRunner.runTestStepByName('Service') // Running Service

//*****************************************************************************************
testRunner.runTestStepByName('ActivityDetails')
testRunner.runTestStepByName('Database')
testRunner.runTestStepByName('GroovyAssertion')



//*****************************************************************************************

for( y=0;y<=charCount;y++)
{
testRunner.testCase.getTestStepByName('Service').httpRequest.removeProperty(values[y])
//log.info "Parameters :  " + values[y]
}
//*****************************************************************************************
                }

}
}

No comments:

Post a Comment