如何在空手道中将JSON值与响应值匹配

时间:2019-09-05 20:38:08

标签: karate

我想将JSON值与空手道中的Response值进行匹配。 如何从JSON和Response中检索所有字段值并进行匹配? 任何解决方案都值得赞赏 以下是示例JSON和响应

this.props.navigation.getParam('name', 'name is coming')

1 个答案:

答案 0 :(得分:0)

可以在https://intuit.github.io/karate/上找到有关其工作原理的粗略描述。

我想给你看一个https://www.baeldung.com/karate-rest-api-testing

的例子

4.2。测试响应 让我们写另一个场景来测试REST端点是否返回特定响应:

Scenario: Testing the exact response of a GET endpoint
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ == {id:"1234",name:"John Smith"}
The match operation is used for the validation where ‘$' represents the response. So the above scenario checks that the response exactly matches ‘{id:”1234″,name:”John Smith”}'.

我们还可以专门检查id字段的值:

And match $.id == "1234"
The match operation can also be used to check if the response contains certain fields. This is helpful when only certain fields need to be checked or when not all response fields are known:

Scenario: Testing that GET response contains specific field
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ contains {id:"1234"}

还有https://aboullaite.me/karate-framework-rest-testing/

中的另一个示例

让我们看一下测试POST端点并采用请求正文的最终方案:

  Scenario: Create and retrieve a Product
    Given path 'products'
    And request { "name": "My product", "type": "Super Type", "price": 123, "shipping": 0, "upc": "041345324016", "description": "My super nice aweome product", "manufacturer": "Feo Hero", "model": "QB2400B4Z", "url": "some.url", "image": "some.image" }
    When method POST
    Then status 201
    And def product = response

    Given path '/products/'+product.id
    When method GET
    Then status 200
    And match $ contains {id:'#(product.id)',name:'#(product.name)',type:'#(product.type)',price:#(product.price)}

肥皂有些不同,例如下面的https://intuit.github.io/karate/#request

Given request read('soap-request.xml')
When soap action 'QueryUsageBalance'
Then status 200
And match response /Envelope/Body/QueryUsageBalanceResponse/Result/Error/Code == 'DAT_USAGE_1003'
And match response /Envelope/Body/QueryUsageBalanceResponse == read('expected-response.xml')