繁体   English   中英

使用Groovy脚本在SoapUI上阅读XLS-RowSelector

[英]XLS reading on SoapUI using Groovy Script - RowSelector

我正在尝试读取XLS文件以供将来迭代使用。 但是我什至无法获得它的价值。

我的代码是:

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)

但是错误返回: Groovy错误

我发现“ RowSelector”变量为空。 也许这就是问题所在,但我无法解决。

观察员:我从以下网址获取了此代码示例: https : //community.smartbear.com/t5/SoapUI-Pro/Get-xls-data-in-loop-in-groovy-script/td-p/27864

getCell(a,b)方法使用整数作为参数,而您从testSuite属性获取的值是一个字符串。 从属性表中获取值时,它是一个字符串。 当您要更新属性表中的字段时,请记住这一点,因为您必须将整数值改回字符串。

更改行:

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

为此:

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

我将您使用的代码分解为自己的部分。 我个人认为尝试找出错误发生的位置时将代码分解会有所帮助。

// 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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