How to get distinct values from first api and pass in second api using karate

Viewed 44

Response from first api (Which has duplicate objects) :

[
{
  "siteHK": "B996F6FC9D3C743A0883A8F383",
  "siteName": "RESIDENTIAL"
},
{
  "siteHK": "EB79DD896F3BAFA754FBAEB27850",
  "siteName": "GROVE"
},
{
  "siteHK": "3C9101A8F0957846505F6E4CC",
  "siteName": "R8 ANADARA"
},
{
  "siteHK": "3C9101A8F0957846505F6E4CC",
  "siteName": "R8 ANADARA"
},
{
  "siteHK": "EB79DD896F3BAFA754FBAEB27850",
  "siteName": "GROVE"
}
]

Second api request:

"base_url"/api/test?siteHK={siteHK}

We need to validate this api by getting each value of "siteHK" from the first api response and pass in the second api request.

Lets call a helper feature file in test scenario:

Feature: Test Feature 

     Background: Test background
      Given url baseURL
      And path '/api/test/first'
      When method get
      Then status 200

    Scenario: Verify test case
        Given def result = call read('classpath:/features/file/testfeature.feature@BaseScenario') $[*]

Helper Feature file :

Feature: Helper Feature:

@BaseScenario
Scenario: Helper scenario
 Given url "base_url"/api/test
 And params {"SiteHk": #(siteHK)}
 When method get
 Then status 200
 match each response == ExpectedResponse

Now if we run test feature file, It will take each "siteHk" from response of first api and validate in second api, But we have duplicate values in response of the first api which are not necessary to validate as there is no need to validate with same set of test data again and again which actually increase our script execution time.

Please let me know how can we avoid duplicate values to be executed?

0 Answers
Related