我们可以在Katalon Studio中比较两个API的响应吗?
如果可能,请提供步骤。
预先感谢
答案 0 :(得分:0)
We can compare two APIs response and the difference will print in a file by using below code
//Required Packages
import groovy.json.JsonSlurper as JsonSlurper
import org.apache.commons.collections4.CollectionUtils as CollectionUtils
import java.util.List as List`enter code here`
import java.lang.String as String
import java.io.FileWriter as FileWriter
import com.google.gson.*
import java.util.Date as Date
import java.util.*
import java.text.*
// Get response code and verify it
response1 = WS.sendRequest(findTestObject('First API'))
WS.verifyResponseStatusCode(response1, 200)
response2 = WS.sendRequest(findTestObject('Second API'))
WS.verifyResponseStatusCode(response2, 200)
def slurper = new JsonSlurper()
def result1 = slurper.parseText(response1.getResponseBodyContent())
println('1st Response is .....' + result1)
def result2 = slurper.parseText(response2.getResponseBodyContent())
println('2nd Response is .....' + result2)
if (!(result1.equals(result2))) {
ArrayList<String> listOne = new ArrayList<String>(Arrays.asList(result1))
ArrayList<String> listTwo = new ArrayList<String>(Arrays.asList(result2))
listOne.removeAll(listTwo)
System.out.println('Additional Data of First Response' + listOne)
//Missing Data
listTwo.removeAll(listOne)
System.out.println('Missing Data of Second Response' + listTwo)
String fileName = new SimpleDateFormat('yyyyMMddHHmm\'.json\'').format(new Date())
// Print the difference in a file( file name will be current time and date)
FileWriter writer = new FileWriter('D:\\APIResponseData\\' + fileName)
Gson gson = new GsonBuilder().setPrettyPrinting().create()
String Additional = gson.toJson(listOne)
String Missing = gson.toJson(listTwo)
writer.write(('Additional Data.......' +Additional) + System.lineSeparator())
writer.write(('Missing Data......' + Missing) + System.lineSeparator())
writer.close()
}
else{
println("Both APIs data same")
}