我正在尝试使用groovy进行一些自动化。我正在接收另一个请求的响应数据,并使用jxl jar将这两个请求和两个输出存储在excel中。但如果我这样做,我的excel文件就会被破坏。但当我做单一请求响应时,它工作正常。所以我想知道jxl是否存在任何内存问题?如果是,如何克服这个?
这是我的剧本:
import jxl.*
import jxl.write.*
import groovy.sql.Sql
//Datasheet read define start
def projectLocation="E:/"
def dataFileLocation=projectLocation+"zip.xls"
def workbook = Workbook.getWorkbook(new File(dataFileLocation))
def readSheet = workbook.getSheet(0)
def rowCount = readSheet.getRows()
def colCount = readSheet.getColumns()
def myTestCase = context.testCase
//db config
def url = 'jdbc:hsqldb:hsql://localhost/'
def user = 'sa'
def password = ''
def driver = 'org.hsqldb.jdbcDriver'
def sql = Sql.newInstance(url, user, password, driver)
propTestStep = myTestCase.getTestStepByName("Properties");
//Datasheet read end
//Content Write define start
WorkbookSettings s = new WorkbookSettings();
s.setUseTemporaryFileDuringWrite(true);
// s.setInitialFileSize(100000000)
WritableWorkbook workbook1 = Workbook.createWorkbook(new File(projectLocation+"output1.xls"),s)
WritableSheet writeSheet = workbook1.createSheet("new", 0)
//
//WritableWorkbook workbook2 = Workbook.createWorkbook(new File(projectLocation+"output2.xls"))
//WritableSheet writeSheet1 = workbook2.createSheet("newOutput", 0)
def project = testRunner.testCase.testSuite.project
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
//Content Write define end
for(int i = 1;i < rowCount; i++)
{
for(int j = 0;j < colCount; j++)
{
val= readSheet.getCell(j,i).getContents()
// log.info "before storing in zip val="+ val
// propTestStep.setPropertyValue("zip",val)
def req=groovyUtils.getXmlHolder( "GetInfoByZIP#Request" )
req["//web:USZip"] = val
req.updateProperty()
testRunner.runTestStep( project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByZIP'] )
// log.info "before sending to db val="+val
sql.eachRow('select state,city from zip where zip=?',[val]){ row ->
st= row.state
ct= row.city
}
//log.info st+" "+ct
def res=groovyUtils.getXmlHolder( "GetInfoByZIP#Response" )
def state = res.getNodeValues( "//STATE" )
def city=res.getNodeValues("//CITY")
def ct1=city[0]
log.info ct +" " + ct1
if(ct1.equalsIgnoreCase(ct)){
// log.info "city equals"
}
else{
// log.info "not eq"
}
def req1=groovyUtils.getXmlHolder( "GetInfoByState#Request" )
req1["//web:USState"]=state[0]
req1.updateProperty()
testRunner.runTestStep( project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByState'] )
def respo1= testRunner.testCase.testSteps["GetInfoByState"].testRequest.response.contentAsString
Label labelReq = new Label(j,i,context.testCase.getTestStepByName("GetInfoByZIP").getProperty("request").value)
Label labelReq1 = new Label(j+2,i,context.testCase.getTestStepByName("GetInfoByState").getProperty("request").value)
def response = testRunner.testCase.testSteps["GetInfoByZIP"].testRequest.response.contentAsString
Label labelResp = new Label(j+1,i,response);
Label labelResp1 = new Label(j+3,i,respo1);
writeSheet.addCell(labelReq)
writeSheet.addCell(labelResp);
log.info respo1
writeSheet.addCell(labelReq1)
writeSheet.addCell(labelResp1);
}
}
workbook1.write()
workbook1.close()
答案 0 :(得分:0)
最有可能的是,你无意中压倒了一些东西。我不知道它是什么。但是,一个好的起点是更清晰的代码。所以我建议使用Frosted Sheets(由Apache POI支持)而不是JXL。它使得处理工作簿很多 很多变得更容易。
我写了这个库,因为我厌倦了看到一些最糟糕的代码,我知道它可以做得更多...... Groovy-ly。
import com.emmanuelrosa.frostedsheets.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import groovy.sql.Sql
def projectLocation="E:/"
def dataFileLocation="${projectLocation}zip.xls"
def workbook = new FrostedWorkbook(new HSSFWorkbook(new FileInputStream(dataFileLocation)))
def rowCount = readSheet.getRows()
def colCount = readSheet.getColumns()
def myTestCase = context.testCase
def url = 'jdbc:hsqldb:hsql://localhost/'
def user = 'sa'
def password = ''
def driver = 'org.hsqldb.jdbcDriver'
def sql = Sql.newInstance(url, user, password, driver)
propTestStep = myTestCase.getTestStepByName("Properties");
def workbook1 = FrostedWorkbook.createXLS()
def writeSheet = workbook1['new']
def project = testRunner.testCase.testSuite.project
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
/*
* cellIterator() returns a special Iterator which traverses
* through the cells by row, then column; top-down, left-right.
* It eliminates the need for nested loops.
*/
workbook[0].cellIterator().each { cell ->
req["//web:USZip"] = cell.cellValue
def req=groovyUtils.getXmlHolder( "GetInfoByZIP#Request" )
req.updateProperty()
testRunner.runTestStep( project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByZIP'] )
sql.eachRow('select state,city from zip where zip=?',[val]) { row ->
st= row.state
ct= row.city
}
def res=groovyUtils.getXmlHolder( "GetInfoByZIP#Response" )
def state = res.getNodeValues( "//STATE" )
def city=res.getNodeValues("//CITY")
def ct1=city[0]
log.info ct +" " + ct1
if(ct1.equalsIgnoreCase(ct)) {
// log.info "city equals"
}
else{
// log.info "not eq"
}
def req1=groovyUtils.getXmlHolder( "GetInfoByState#Request" )
req1["//web:USState"]=state[0]
req1.updateProperty()
testRunner.runTestStep( project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByState'] )
writeSheet[cell.rowIndex + 1].with {
def stateInfo = testRunner.testCase.testSteps["GetInfoByState"].testRequest.response.contentAsString
def zipValue = context.testCase.getTestStepByName("GetInfoByZIP").getProperty("request").value
def stateValue = context.testCase.getTestStepByName("GetInfoByState").getProperty("request").value
def zipInfo = testRunner.testCase.testSteps["GetInfoByZIP"].testRequest.response.contentAsString
// Add the columns to the row
append [zipValue, zipInfo, stateValue, stateInfo]
}
}
new FileOutputStream("${projectLocation}output1.xls").withStream { stream ->
workbook1.write(stream)
}
答案 1 :(得分:0)
我认为您的响应数据超出了Excel的单元格限制。你可以通过复制粘贴响应来检查那个东西,检查有多少个字符。 Excel可以在一行中包含32,767个字符。如果超过它可能会腐败。