Forum Discussion

dev16's avatar
dev16
Contributor
7 years ago
Solved

how to handle empty field in excel sheet in data driven testing using groovy script

I am using soapui 5.2.1 open source

where i am trying to read excel sheet data and send json post request through groovy script

if data/value is available in all fields in excel sheet then it works properly but some below issues i am facing when one of the field is empty

1) if in excel sheet 4 rows and columns available

and second row's first column is empty and run the groovy then second column data value is shown in first column

E.g.  in this case docotrassociateId value is shown in OnSetDate field

so want to handle empty field using groovy script

so request you please guide me.

caseIdonSetDateVisitDate
DocotorAssociateId
39  1
402017-05-052017-05-05

1

 

 

 

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;

import java.util.Iterator;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.eviware.soapui.impl.wsdl.panels.support.MockTestSuiteRunner;
import com.eviware.soapui.impl.wsdl.panels.support.MockTestSuiteRunContext;

project = testRunner.getTestCase().testSuite.getProject();
testSuite = project.getTestSuiteByName("DTOUser_TestSuite");
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
 
FileInputStream file = new FileInputStream(new File('D:\\SOAPUiTool\\testingsheet.xlsx'));
XSSFWorkbook workbook1 = new XSSFWorkbook(file)
XSSFSheet sheet1 = workbook1.getSheetAt(0)
Iterator ite = sheet1.rowIterator();

def myTestCase = context.testCase
 propTestStep = myTestCase.getTestStepByName("PropertiesClinicalInfo") // get the Property TestStep object
headers=[]

ite.next();
DecimalFormat decimalFormat = new DecimalFormat("#");
while (ite.hasNext())
{
    Row row = ite.next();
    Iterator<Cell> cite = row.cellIterator();
    
         project.setPropertyValue("caseId", cite.next().toString())
         log.info(" caseId:: " + project.getPropertyValue("caseId").toString())
     
         project.setPropertyValue("onsetDate",cite.next().toString())
         log.info("onsetDate :: " + project.getPropertyValue("onsetDate").toString())
       
         project.setPropertyValue("visitDate",cite.next().toString())
         log.info("visitDate :: " + project.getPropertyValue("visitDate").toString())
          
         project.setPropertyValue("doctorAssociationId", cite.next().toString())
        log.info("doctorAssociationId :: " + project.getPropertyValue("doctorAssociationId").toString())

def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName("IRSHA AUG17")
tCase = prj.testSuites['DTOUser_TestSuite'].testCases['TC3AddClinicalInfo']
tStep = tCase.getTestStepByName("AddClicnicalInfo")
tStep.setPropertyValue("caseId",project.getPropertyValue("caseId").toString());
tStep.setPropertyValue("onsetDate",project.getPropertyValue("onsetDate").toString());
tStep.setPropertyValue("visitDate",project.getPropertyValue("visitDate").toString());
tStep.setPropertyValue("doctorAssociationId",project.getPropertyValue("doctorAssociationId").toString());
testStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(tStep);
def runner = tStep.run(testRunner, testStepContext)
log.info ("runner status ....... : " + runner.hasResponse())

def f = new File("D:\\SOAPUiTool\\testingsheetresponse.txt")
saveToFile(f, testStepContext.response)

}

file.close();

def saveToFile(file, content) {
    if (!file.parentFile.exists()) {
         file.parentFile.mkdirs()
         log.info "Directory did not exist, created"
    }
    FileWriter fw = new FileWriter(file,true); //the true will append the new data
    fw.write("\n");//appends the string to the file
     fw.write(content);
     fw.write("\n");
     fw.close();
    assert file.exists(), "${file.name} not created"
}

 

Thanks

DEV

 

2 Replies