【发布时间】:2015-10-22 15:40:46
【问题描述】:
我正在尝试将来自 JSON 响应的节点值与从我已为其构建数组的 Excel 电子表格收集的值进行比较。该数组包含来自多列的值,但我想查看 JSON 节点值是否在数组中。最终我需要将多个节点值与数组进行比较。目前我似乎无法让它只为一个人工作。我做错了什么??
import com.eviware.soapui.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
import groovy.lang.*;
import groovy.util.*;
import groovy.json.JsonSlurper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//Create an array to hold the cell values
def cellValues = [];
//Get the JSON in a format to easily access
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def ResponseMessage = testRunner.testCase.testSteps["ProductSkuLookupService"].getPropertyValue("response");
def jsonResult = new JsonSlurper().parseText(ResponseMessage);
//Get the number of recommended expressions nodes
def rspnsNumRcmndExprsns = jsonResult.product.recommendedExpressions.size();
//Get the file from the location specified in the test case custom properties
def String dataFilename = context.expand( '${#TestCase#dataFilename}' );
if(dataFilename != null && !dataFilename.equals(""))
{
XSSFWorkbook wb = new XSSFWorkbook(dataFilename);
XSSFSheet sheet = wb.getSheet("exprsnList");
XSSFRow row;
XSSFCell cell;
def totalNumRows;
def numRows;
//Get the total number of rows with data on the sheet
totalNumRows = sheet.getPhysicalNumberOfRows();
//log.info " The total number of rows are " + totalNumRows;
//Get the number of rows without the header row
numRows = totalNumRows - 1;
//log.info " The number of rows to use are " + rows;
def cols = 0;
cols = sheet.getRow(0).getPhysicalNumberOfCells();
//log.info " The total number of columns are " + cols;
if(numRows == rspnsNumRcmndExprsns)
{
for(int i = 1; i < totalNumRows; i++)
{
row = sheet.getRow(i);
//log.info " Row # " + i;
for(int j = 0; j < cols; j++)
{
//log.info " Cell # " + j;
cell = row.getCell(j);
//log.info " The value in the cell is " + cell;
cellValues.push(cell);
}
}
log.info jsonResult.product["recommendedExpressions"]["imageUrl"].every{it in [cellValues]}
}
else
{
assert false, "The number of nodes does not match the number of rows.";
}
}
else
{
assert false, "The file name is missing.";
}
return;
这是一段JSON(JSON中有多个子数组):
{
"responseHeader": {
"status": "SUCCESS",
"messages": []
},
"product": {
"recommendedExpressions": [
{
"imageUrl": "/images/products/expressions/ex015.jpg",
"id": "sku83283exp",
"description": " ",
"code": "EX015"
},
{
"imageUrl": "/images/products/expressions/ex100.jpg",
"id": "sku81486exp",
"description": " ",
"code": "EX100"
},
{
"imageUrl": "/images/products/expressions/ex036.jpg",
"id": "sku82518exp",
"description": " ",
"code": "EX036"
}
]
}
}
【问题讨论】:
-
在 Excel 中存储响应似乎有点疯狂?
-
实际上并未将响应存储在 Excel 中……我的测试数据(验证数据)是在 Excel 中提供给我的。我把它放到一个数组中。从那里我想比较对数组的响应。
-
数组长什么样子?
-
上面提供了数组的代码。