在SOAPUI Groovy失败中将结果写入现有的Excel中

时间:2017-10-22 20:31:16

标签: excel groovy soapui

我需要在同一个excel表中写入结果(通过/失败),其中脚本在FOR循环中读取参数。这是一个错误:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'jxl.read.biff.WorkbookParser@3da0525b' with class 'jxl.read.biff.WorkbookParser' to class 'jxl.write.WritableWorkbook' error at line: 16

我的代码:

import jxl.*;
import jxl.write.*;
import java.io.*;
import groovy.json.JsonSlurper

//Get project path

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

def projectPath = groovyUtils.projectPath

def testCaseName = testRunner.testCase.name

//Read excel file and get the input value
	
	WritableWorkbook xlwb = Workbook.getWorkbook(new File ("${projectPath}\\${testCaseName}.xls"))
	Sheet inputxlsh = xlwb.getSheet(0)
	inputRowCount = inputxlsh.getRows();
	WritableSheet outputxlsh = xlwb.getSheet(1)
	outputRowCount = outputxlsh.getRows();

	log.info "Executing Test Case " + testCaseName
	log.info "Total records to send API request to webservice from the file : " +  inputRowCount -1
	
	for (i=0;i<inputRowCount-1;i++)
	{
		Cell requestParam1 = inputxlsh.getCell(0,i+1)
		affkey = requestParam1.getContents()

		Cell requestParam2 = inputxlsh.getCell(1,i+1)
		etid = requestParam2.getContents()
				
		def soapTestCase = context.testCase

//Set the request property value (Parameter)

		requestPropertyVariable = soapTestCase.getTestStepByName("requestProperty")
		requestPropertyVariable.setPropertyValue("affkey",affkey)
		requestPropertyVariable.setPropertyValue("etid",etid)
		
		log.info "Reading record " + (i+1) + " from input file"
		log.info "Sending request with affkey " + affkey
		log.info "Sending request with etid " + etid
		
//Post a request to webservice

		def responseContent = testRunner.runTestStepByName("showetidRequest").getResponse()
		def responseText = responseContent.getContentAsString()

//Save the output file

		def fileObj = new File("${projectPath}\\API_Response\\${testRunner.testCase.name}\\${etid}_Response.txt")
		saveToFile(fileObj, responseText)

//Get the response value

		JsonSlurper jsonResponseContent = new JsonSlurper()

		def jsonResponseObject = jsonResponseContent.parseText(responseText)

//Validate results

			Cell headerParam1 = outputxlsh.getCell(0,0)
			Cell headerParam2 = outputxlsh.getCell(1,0)
			Cell headerParam3 = outputxlsh.getCell(2,0)
			Cell headerParam4 = outputxlsh.getCell(3,0)
			Cell headerParam5 = outputxlsh.getCell(4,0)
			Cell headerParam6 = outputxlsh.getCell(5,0)
			
		for (k = 0; k < outputRowCount-1; k++) {	
			Cell responseParam1 = outputxlsh.getCell(0,k+1)
			Cell responseParam2 = outputxlsh.getCell(1,k+1)
			Cell responseParam3 = outputxlsh.getCell(2,k+1)
			Cell responseParam4 = outputxlsh.getCell(3,k+1)
			Cell responseParam5 = outputxlsh.getCell(4,k+1)
			Cell responseParam6 = outputxlsh.getCell(5,k+1)
			
			expectedAffiliatesWithContent = responseParam1.getContents()
			expectedEntityName = responseParam2.getContents()
			expectedName = responseParam3.getContents()
			expectedSaleMessageId = responseParam4.getContents()
			expectedTitle = responseParam5.getContents()
			expectedetid = responseParam6.getContents()
				
			if(etid==expectedetid){

			responseAffiliatesWithContent = jsonResponseObject.AffiliatesWithContent.getAt(0).getAt(0)
			responseEntityName = jsonResponseObject.Genre.EntityName.getAt(0)
			responseName = jsonResponseObject.Genre.Name.getAt(0)
			responseSaleMessageId = jsonResponseObject.SaleMessage.Id.getAt(0)
			responseTitle = jsonResponseObject.Title.getAt(0)
									
				log.info responseAffiliatesWithContent
				log.info responseEntityName
				log.info responseName
				log.info responseSaleMessageId
				log.info responseTitle

				if (responseAffiliatesWithContent==expectedAffiliatesWithContent&&responseEntityName==expectedEntityName&&responseName==expectedName&&responseSaleMessageId==expectedSaleMessageId&&
					responseTitle==expectedTitle)
					{
					log.info "The data is matched for record " + (k +1) + " hence test case passed "
		                   Label l = new Label(7, k +1, "Pass");
		                   outputxlsh.addCell(l);
		                   xlwb.write(); 					
					}
					else {
					log.info "The data is matched for record " + (k +1) + " hence test case failed "
					}
			}
		}
		}

//Clear Property
	requestPropertyVariable.setPropertyValue("affkey","")
	requestPropertyVariable.setPropertyValue("etid","")
	
//Write file method

	def saveToFile(fileObj, content) {
		if (!fileObj.parentFile.exists()) {
	         fileObj.parentFile.mkdirs()
	    }
		fileObj.write(content) 
	     log.info "Response for etid " + etid + " is stored in " + "${etid}_Response.txt"
		assert fileObj.exists(), "${fileObj.name} not created"
	}

2 个答案:

答案 0 :(得分:0)

如果您想要一个可写的复制电话createWorkbook功能

WritableWorkbook xlwb = Workbook.createWorkbook(new File ("${projectPath}\\${testCaseName}.xls"))

如果您不希望在Workbook对象中使用可写副本保存

Workbook workbook = Workbook.getWorkbook((new File ("${projectPath}\\${testCaseName}.xls"))

答案 1 :(得分:0)

我已根据以下代码

达到了要求
    def inptDataWb = new HSSFWorkbook(xlwb);
    def inputxlsh = inptDataWb.getSheetAt(2);
    wrtResult = outputxlsh.getRow(k+1).getCell(3);
    wrtResult.setCellValue("P");
    wrtResult = outputxlsh.getRow(k+1).getCell(5);
    wrtResult.setCellValue("");
    inptDataWb.write(xlOwb);