断言关键字的响应与当前POST的响应相同

时间:2019-10-14 02:31:59

标签: assert katalon-studio

我正在使用assertThat检查响应B字段1 =当前的硬编码文本结果。 如何使用assertB响应B字段1 =响应A字段1(来自关键字A的那个字段)?这样我就不必编写很多测试用例来对断言结果进行硬编码。

示例: 关键字A:发布消息A并获取响应A以验证内容

	@Keyword
	def POSTA def dataFile){
		JsonBuilder ItemBuilder = new JsonBuilder()
		JsonSlurper jsonSlurper = new JsonSlurper()
		ExcelData data = findTestData(dataFile)
		def date = new Date()
		for (def index : (1..data.getRowNumbers())) {
			def TestItemId = data.getValue('ItemId', index) + "/" + data.getValue('ItemCode', index)
			ItemBuilder({
				ItemId(TestItemId)
				name(data.getValue('ItemId', index))
				Id(data.getValue('ItemCode', index))
				created(date)
				lastModified(date)
			})

			String ItemPostJson = JsonOutput.prettyPrint(ItemBuilder.toString())
			println(ItemPostJson)
			def item = ((findTestObject('Web Service Request/item-service/ApiItemPost')) as RequestObject)
			item.setBodyContent(new HttpTextBodyContent(ItemPostJson))


			//POST ITEM
			def response = WS.sendRequestAndVerify(item)
			if(dataFile == "Data Files/ItemData"){
				println(response.statusCode)
				println(response.responseText)
				WS.verifyResponseStatusCode(response, 201)
			}
			else if(dataFile == "Data Files/ItemDataPost400"){
				println(response.responseText)
				WS.verifyResponseStatusCode(response, 400)
			}
		}
	}

新测试用例B,发布消息B,并获得响应B

def NewItemMessage = consumeMessage("NewItem")

def responseList = new JsonSlurper().parseText(consumeMessage.getResponseText())

def decoded = new JsonSlurper().parseText(new String(responseList[0].value.decodeBase64()))
println('response text: \n' + JsonOutput.prettyPrint(JsonOutput.toJson(decoded)))

assertThat(decoded.item.ItemId).isEqualTo("123")
assertThat(decoded.item.name).isEqualTo("Test fdfs")

def consumeMessage(String topic) {
	WS.sendRequestAndVerify(findTestObject('Kafka/1b-Subscribe NewItem Topic', [('topic_name') : topic]))
	return consumeMessage = WS.sendRequestAndVerify(findTestObject('Kafka/1c-Consume Message from NewItem Topic'))
}

0 个答案:

没有答案