//Create a new filereader object, using the context variable so it can be used between test components
context.fileReader = new BufferedReader(new FileReader('C:/data.csv'))
//Read in the first line of the data file
//this is the code fro the testcase
firstLine = context.fileReader.readLine()
//Split the first line into a string array and assign the array elements to various test case properties
String[] propData = firstLine.split(",")
testCase.setPropertyValue("data1",propData[0])
testCase.setPropertyValue("data2",propData[1])
testCase.setPropertyValue("data3",propData[2])
//Rename request test steps for readability in the log; append the element name to the test step names
testCase.getTestStepAt(0).setName("data1-" + propData[0])
testCase.getTestStepAt(1).setName("data2-" + propData[1])
testCase.getTestStepAt(2).setName("data3-" + propData[2])
//this is the Code that reads from CSV file
context.fileReader = new BufferedReader(new FileReader('C:/data.csv'))
/*Read in the next line of the file
We can use the same fileReader created in the Setup script because it
was assigned to the context variable.*/
nextLine = context.fileReader.readLine()
/*If the end of the file hasn't been reached (nextLine does NOT equal null)
split the line and assign new property values, rename test request steps,
and go back to the first test request step*/
if(nextLine != null){
String[] propData = nextLine.split(",")
curTC = testRunner.testCase
curTC.setPropertyValue("data1",propData[0])
curTC.setPropertyValue("data2",propData[1])
curTC.setPropertyValue("data3",propData[2])
curTC.getTestStepAt(0).setName("data1-" + propData[0])
curTC.getTestStepAt(1).setName("data2-" + propData[1])
curTC.getTestStepAt(2).setName("data3-" + propData[2])
testRunner.gotoStep(0)
}
这是我得到的错误。有谁有想法吗?我正在尝试从CSV文件中读取超过3列,请帮忙。
TestCase failed [java.lang.IndexOutOfBoundsException: Index: 2, Size: 2:java.lang.IndexOutOfBoundsException: Index: 2, Size: 2], time taken = 0
这是CSV文件数据:
Hydrogen,1,H,1.00797,20.4
Carbon,6,C,12.0115,5100
Oxygen,8,O,15.9994,90.2
Gold,79,Gd,196.967,3239
Uranium,92,U,238.03,4091
答案 0 :(得分:2)
使用OpenCSV代替使用Java或Groovy解析CSV文件。您可以使用Grape将jar添加到Groovy类路径(并动态解析它们的依赖项):
@Grab(group='com.opencsv', module='opencsv', version='3.3')
答案 1 :(得分:0)
你很幸运。你是SoapUI的新手没问题,因为OpenCSV与SoapUI没有任何关系:)
@Grab('com.opencsv:opencsv:3.5')
import com.opencsv.CSVReader
/*
* Mock some CSV data
*/
def reader = new StringReader(
'''column1,column2,column3,column4,column5
Hydrogen,1,H,1.00797,20.4
Carbon,6,C,12.0115,5100
Oxygen,8,O,15.9994,90.2
Gold,79,Gd,196.967,3239
Uranium,92,U,238.03,4091''')
/*
* A nice mapping to give each field in the CSV file a name.
* Much better than a bunch of propData[n] all over the place.
*/
def field = [
ELEMENT: 0
]
reader.withReader {
new CSVReader(it).eachWithIndex {list, index ->
if(index == 0) {
/*
* Do whatever you need to do with the header of the CSV file.
* Example:
* testCase.setPropertyValue("data1",list[field.ELEMENT])
*/
} else {
/*
* Do whatever you need to do with the remaining rows.
* Example:
* curTC.setPropertyValue("data1",list[field.ELEMENT])
*/
}
}
}
你会注意到在eachWithIndex()
循环中有一个if-else。这样就可以处理标题,然后继续处理剩下的行,而不必重新开始读取文件。
您应该能够将特定于SoapUI的代码插入相应的部分。
如果由于某种原因,您的数据行并非都具有相同数量的字段,您可以检查这样的字段数:list.size()