package ScriptLibrary;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
import org.skyscreamer.jsonassert.JSONCompare; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map; import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.*; /**
* Created by jenny zhang on 2017/9/19.
*/
public class LooseyJSONComparator extends DefaultComparator {
private int scale; //精度,scale=5,表示精确到小数点后5位
private Double accuracy; //精度,accuracy=0.00001,表示精确到小数点后5位
String extraInfo;
def log;
ArrayList<String> ignoreKeys;
def testRunner
def context
def extraInfoMap public LooseyJSONComparator(JSONCompareMode mode, int scale,String extraInfo,def log) {
super(mode);
this.scale = scale;
this.accuracy = 1.0/Math.pow(10,scale);
this.extraInfo = extraInfo;
this.log = log;
} public LooseyJSONComparator(JSONCompareMode mode, int scale,def log,def context,def testRunner,def extraInfoMap,ArrayList<String> ignoreKeys) {
super(mode);
this.scale = scale;
this.accuracy = 1.0/Math.pow(10,scale);
this.log = log;
this.context = context
this.testRunner = testRunner
this.extraInfoMap = extraInfoMap
this.ignoreKeys = ignoreKeys;
} public static void assertEquals( String expected, String actual, int scale, def log,def context=null,def testRunner=null,def extraInfoMap=null,ArrayList<String> ignoreKeys=null) throws JSONException {
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, new LooseyJSONComparator(JSONCompareMode.NON_EXTENSIBLE,scale,log,testRunner,context,extraInfoMap,ignoreKeys));
if (result.failed()) {
def currentStepIndex = context.currentStepIndex
def testSuiteName = testRunner.testCase.getTestStepAt(currentStepIndex).getParent().getParent().getName() def failMessage = result.getMessage(); if(failMessage.contains("Expected:")&&failMessage.contains("got:")&&(testRunner!=null)&&(context!=null)&&(extraInfoMap!=null)){
//移除老板不想看的信息
def keysToRemoveForBoss = ["RequestIdBmk", "RequestIdTest"]
def extraInfoMapForBoss = extraInfoMap.findAll({!keysToRemoveForBoss.contains(it.key)}) def topRow=["Test Suite","Test Case"]
topRow = topRow+extraInfoMapForBoss.keySet()
topRow = topRow+["DataPoint","Bmk Env","Test Env","DetailedJsonPath"] //获取当前自动化测试Project所在的路径
String projectPath = context.expand( '${projectDir}' )
WriteExcel writeExcel = new WriteExcel(testRunner,context,log) //处理fail message,解析出来写入excel
ArrayList failMessageList = new ArrayList()
failMessageList = writeExcel.parseFailMessage(failMessage,extraInfoMapForBoss) //定义Excel的路径和名字
String filePath = projectPath+ "/TestResult/" + testSuiteName + "_Fail.xlsx"
File testResultFile = new File(filePath) //如果Excel不存在,就先创建带列名的Excel
if(!testResultFile.exists()){
writeExcel.createExcel(testResultFile,topRow)
}
//追加信息到Excel
writeExcel.addExcel(filePath,failMessageList)
}
//打印QA和Developer想要的错误信息到SoapUI报告
def extraInfoForQAAndDev = extraInfoMap.collect { k,v -> "$k=$v" }.join(',')
throw new AssertionError(extraInfoForQAAndDev+failMessage);
}
else{
log.info "pass";
}
} public static void assertEquals( String expected, String actual, int scale, String extraInfo,def log) throws JSONException {
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, new LooseyJSONComparator(JSONCompareMode.NON_EXTENSIBLE,scale,extraInfo,log));
if (result.failed()) {
def failMessage = result.getMessage();
throw new AssertionError(extraInfo + failMessage);
}
else{
log.info "pass";
}
} @Override
protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
String uniqueKey = findUniqueKey(expected);
if (uniqueKey == null || !isUsableAsUniqueKey(uniqueKey, actual)) {
// An expensive last resort
recursivelyCompareJSONArray(key, expected, actual, result);
return;
} Map<Object, JSONObject> expectedValueMap = arrayOfJsonObjectToMap(expected, uniqueKey, log);
Map<Object, JSONObject> actualValueMap = arrayOfJsonObjectToMap(actual, uniqueKey, log);
for (Object id : expectedValueMap.keySet()) {
if (!actualValueMap.containsKey(id)) {
result.missing(formatUniqueKey(key, uniqueKey, expectedValueMap.get(id).get(uniqueKey)),
expectedValueMap.get(id));
continue;
}
JSONObject expectedValue = expectedValueMap.get(id);
JSONObject actualValue = actualValueMap.get(id); compareValues(formatUniqueKey(key, uniqueKey, id), expectedValue, actualValue, result);
}
for (Object id : actualValueMap.keySet()) {
if (!expectedValueMap.containsKey(id)) {
result.unexpected(formatUniqueKey(key, uniqueKey, actualValueMap.get(id).get(uniqueKey)), actualValueMap.get(id));
}
}
} @Override
protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) {
Set<String> actualKeys = getKeys(actual);
for (String key : actualKeys) {
if ((!expected.has(key))&&(!ignoreKeys.find{it == key})) {
result.unexpected(prefix, key);
}
}
} private String getCompareValue(String value) {
try{
return new BigDecimal(value).setScale(scale, RoundingMode.HALF_EVEN).toString();
} catch (NumberFormatException e) {
return value; //value may = NaN, in this case, return value directly.
}
} private boolean isNumeric(Object value) {
try {
Double.parseDouble(value.toString());
return true;
} catch (NumberFormatException e) {
return false;
}
} public Map<Object, JSONObject> arrayOfJsonObjectToMap(JSONArray array, String uniqueKey,def log) throws JSONException {
Map<Object, JSONObject> valueMap = new HashMap<Object, JSONObject>();
for (int i = 0; i < array.length(); ++i) {
JSONObject jsonObject = (JSONObject) array.get(i);
Object id = jsonObject.get(uniqueKey);
id = isNumeric(id) ? getCompareValue(id.toString()) : id;
valueMap.put(id, jsonObject);
}
return valueMap;
} @Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException {
if (areLeaf(expectedValue, actualValue)) {
if (isNumeric(expectedValue) && isNumeric(actualValue)) {
//For special numeric, such as NaN, convert to string to compare
boolean equalsAsSpecialNumeric = (expectedValue.toString().equals(actualValue.toString())) //For normal numeric, such as 153.6960001, 1.56E5, subtract and then get abosolute value
boolean equalsAsGeneralNumeric = Math.abs(Double.parseDouble(expectedValue.toString())-Double.parseDouble(actualValue.toString()))<accuracy; if (equalsAsSpecialNumeric||equalsAsGeneralNumeric) {
result.passed();
} else {
result.fail(prefix, expectedValue, actualValue);
}
return;
}
}
super.compareValues(prefix, expectedValue, actualValue, result);
} private boolean areLeaf(Object expectedValue, Object actualValue) {
boolean isLeafExpectedValue = !(expectedValue instanceof JSONArray)&&!(expectedValue instanceof JSONObject);
boolean isLeafActualValue = !(actualValue instanceof JSONArray)&&!(actualValue instanceof JSONObject);
return isLeafExpectedValue&&isLeafActualValue;
}
}

  

[SoapUI] 通过JSONAssert比较两个环境的JSON Response,定制化错误信息到Excel的更多相关文章

  1. 使用Groovy处理SoapUI中Json response

    最近工作中,处理最多的就是xml和json类型response,在SoapUI中request里面直接添加assertion处理json response的话,可以采用以下方式: import gro ...

  2. 五、redis哨兵两套环境同一局域网容灾切换问题

    上周遇到个灵异事件,实验室有两套环境来搭建redis集群和哨兵,分别是: 第一套环境IP:67(master) 65(salve) 66(salve)第二套环境IP:115(master) 116(s ...

  3. 定制化Azure站点Java运行环境(5)

    Java 8下PermGen及参数设置 在上一章节中,我们定制化使用了Java 8环境,使用我们的测试页面打印出了JVM基本参数,但如果我们自己观察,会发现在MXBeans中,没有出现PermGen的 ...

  4. 【SoapUI】比较Json response

    package direct; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject ...

  5. [SoapUI] 重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息

    重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息 package d ...

  6. EpiiServer 更快捷更方便的php+nginx环境定制化方案

    EpiiServer是什么 更快捷更方便的php+nginx多应用部署环境. github仓库首页 https://github.com/epaii/epii-server gitee仓库 https ...

  7. Windows环境搭建Web自己主动化測试框架Watir(基于Ruby)

    web自己主动化測试一直是一个比較迫切的问题 图1-1 须要安装的工具 http://railsinstaller.org/ 由于安装Ruby还须要用到其它的一些开发工具集.所以建议从站点http:/ ...

  8. cocos2d-x3.9 NDK android 环境搭建过程中遇到的错误

    编译环境:Mac OS, NDK r9d 错误:arm-linux-androideabi-gcc: error trying to exec '/media/Project/adt-bundle-l ...

  9. 定制化Azure站点Java运行环境(4)

    定制化使用您自己的Tomcat版本和JDK环境 在上面章节中,介绍了如何通过web.config,定制默认的Azure website的Java运行环境,默认情况下,Azure站点的Tomcat是7. ...

随机推荐

  1. @Scope 注解

    @Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)这个是说在每次注入的时候回自动创建一个新的bean实例 @Scope(value=Config ...

  2. 算法之LOWB三人组之选择排序

    选择排序 思想是在一个列表中每次循环一遍,拿到最小值,接着再从剩下的无序区中继续拿最小值,如此循环,直到结束. 时间复杂度为O(n^2) # 最简单的一个选择排序,循环一个列表,拿到最小值,添加到一个 ...

  3. python内置函数getattr用法

    class Tests(object):    #定义类     aaa = '10'          #定义变量       def test(self):     #定义类的方法test     ...

  4. SolarWinds网络管理手册列表

    前段时间使用过SolarWinds管理思科的交换机,在使用的过程中自己做了一个简单的使用手册,SolarWinds是一款非常强悍的网管软件,手册中没有完全涉及SolarWinds的所有功能,Solar ...

  5. Dedecms织梦内容页获取当前页面顶级栏目名称方法

    Dedecms织梦做站的时候,需要在当前页面调用顶级栏目名称的时候,织梦默认{dede:field name='typename' /} 可以获取当前栏目页上一级栏目的名称,而不是当前栏目顶级栏目名称 ...

  6. 第九章 词典 (e)桶/计数排序

  7. Ubuntu下ssh的安装

    1.安装 Ubuntu缺省安装了openssh-client,,如果没有安装,可用apt-get安装上即可. 安装ssh-server sudo apt-get install openssh-ser ...

  8. Bitmap byte[] InputStream Drawable 互转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...

  9. ViewResolver和JSTL

    ---------------------siwuxie095                                 ViewResolver 和 JSTL         1.Spring ...

  10. [剑指Offer]62-圆圈中最后剩下的数(约瑟夫环问题)(法二待做)

    题目链接 https://www.nowcoder.com/practice/f78a359491e64a50bce2d89cff857eb6?tpId=13&tqId=11199&t ...