0
我想從JSON響應中比較節點值和從我爲其構建數組的Excel電子表格中收集的值。該數組包含來自多個列的值,但我想查看JSON節點值是否在數組中。最終我需要將多個節點值與數組進行比較。目前我似乎無法爲此僅爲一項工作。我究竟做錯了什麼??SoapUI/Groovy動態比較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中給了我。我正在把它放到一個數組中。從那裏我想比較數組的響應。 – FemITGeek
數組是什麼樣的? –