简体   繁体   中英

XLS reading on SoapUI using Groovy Script - RowSelector

I'm trying to read a XLS file for future iteration an assertion. But I can't even get it's values.

My code is:

import jxl.*
import jxl.write.*

def value1
def value2
def value3

//pull value from test suite properties
def RowSelector = testRunner.testCase.testSuite.getPropertyValue( "RowSelector" )

//Read Excel
Workbook wb = Workbook.getWorkbook(new File("C:\\groovy\\excel-file.xls"))

log.info(RowSelector)
value1 = wb.getSheet(0).getCell(0, RowSelector).getContents() //cell A1
                                        testRunner.testCase.testSuite.setPropertyValue( "TestSuitevalue1", value1 )
                                        log.info("Value1 Is: " + value1)

But an error returns: Groovy error

I found that the "RowSelector" variable is null. Maybe that's the problem, but I can't solve that.

Obs.: I took this code sample from: https://community.smartbear.com/t5/SoapUI-Pro/Get-xls-data-in-loop-in-groovy-script/td-p/27864

The getCell(a,b) method uses integers as arguements and the value you are taking from the testSuite properties is a string. When you take a value from a properties table it is a string. Remember this when you want to update a field in a properties table, as you will have to change a integer value back to a string.

Change the line:

def RowSelector = testRunner.testCase.testSuite.getPropertyValue( "RowSelector" )

To This:

def RowSelector = testRunner.testCase.testSuite.getPropertyValue( "RowSelector" ).toInteger()

I broke down the code that you are using into their own sections. Personally I find breaking the code down helps when trying to figure out where the error is occurring.

// IMPORT THE LIBRARIES WE NEED
import jxl.*
import jxl.write.*
//Get cell row number
def RowSelector = testRunner.testCase.testSuite.getPropertyValue( "RowSelector" ).toInteger()
//Get workbook
Workbook wb= Workbook.getWorkbook(new File("C:\\ExcelFile.xls"))
//Get worksheet
Sheet ws = wb.getSheet(0)
//Get cell
Cell a = ws.getCell(1, RowSelector) // getCell(column,row)
//Close workbook
workbook.close()
//Asign value of cell to variable
A = a.getContents()

log.info A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM