2017-10-22 1 views
0

Ich habe eine Anforderung, um ein Ergebnis (Pass/Fail) in der gleichen Excel-Tabelle zu schreiben, wo das Skript den Parameter in einer FOR-Schleife liest. Es wirft einen Fehler:Ergebnisse in existierende Excel schreiben in SOAPUI Groovy Fails

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[email protected]' with class 'jxl.read.biff.WorkbookParser' to class 'jxl.write.WritableWorkbook' error at line: 16 

Mein Code:

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 
 
\t 
 
\t WritableWorkbook xlwb = Workbook.getWorkbook(new File ("${projectPath}\\${testCaseName}.xls")) 
 
\t Sheet inputxlsh = xlwb.getSheet(0) 
 
\t inputRowCount = inputxlsh.getRows(); 
 
\t WritableSheet outputxlsh = xlwb.getSheet(1) 
 
\t outputRowCount = outputxlsh.getRows(); 
 

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

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

 
//Set the request property value (Parameter) 
 

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

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

 
//Save the output file 
 

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

 
//Get the response value 
 

 
\t \t JsonSlurper jsonResponseContent = new JsonSlurper() 
 

 
\t \t def jsonResponseObject = jsonResponseContent.parseText(responseText) 
 

 
//Validate results 
 

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

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

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

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

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

+0

Sind Sie OK, um Ergebnis in Komma getrennte Datei (.csv) zu schreiben, die Sie in Excel öffnen können? – Rao

+0

http://www.vogella.com/tutorials/JavaExcel/article.html - sehen, ob dies hilft. – Rao

+0

No Rao .. Da das Excel enthält Diagramme für Testfall Status auf der Grundlage der Wert in der Pass/Fail-Spalte aufgefüllt, können wir diese Datei nicht in CSV konvertieren –

Antwort

0

Wenn Sie ein Schreib können Kopie rufenden createWorkbook Funktion

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

Wenn Sie don‘ t wollen schreibfähige Kopie speichern in Workbook Objekt stattdessen

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

Vielen Dank für Ihre Kommentare. Aber ich möchte Pass oder Fail-Ergebnis in der vorhandenen Datei schreiben, anstatt es in die neue Datei zu schreiben –

+0

Kann mir bitte jemand dabei helfen? –

+0

Zum Anhängen siehe https://stackoverflow.com/questions/5509073/appending-data-in-a-excel-file – user7294900

Verwandte Themen