03 Multiple Records - biswajitsundara/Karate GitHub Wiki

If we want to check for multiple records then use the below approach

MultipleRecords.feature

Feature: Check multiple records from response
  
  Background:
      * url 'https://reqres.in'
      * def expectedoutput = read('./data/mulrecords.json')
  
  
    Scenario Outline: Get call test for multiple records
    Given path '/api/users/' + <path>
    When method GET
    Then status=200
    And match response == expectedoutput[<record>]
    
    Examples:
    | path  | record | 
    | 2     | 0      | 
    | 3     | 1      | 

MulRecords.json

src\test\java\data\mulrecords.json

[
   {
		"data": {
			"last_name": "Weaver",
			"id": 2,
			"avatar": "https://reqres.in/img/faces/2-image.jpg",
			"first_name": "Janet",
			"email": "[email protected]"
		},
		"support": {
			"text": "To keep ReqRes free, contributions towards server costs are appreciated!",
			"url": "https://reqres.in/#support-heading"
		}
	},
	{
		"data": {
			"last_name": "Wong",
			"id": 3,
			"avatar": "https://reqres.in/img/faces/3-image.jpg",
			"first_name": "Emma",
			"email": "[email protected]"
		},
		"support": {
			"text": "To keep ReqRes free, contributions towards server costs are appreciated!",
			"url": "https://reqres.in/#support-heading"
		}
	}

]

MultipleRecordTestRunner.java

import org.junit.runner.RunWith;
import com.intuit.karate.junit4.Karate;

@RunWith(Karate.class)
public class MultipleRecordTestRunner {

}

Explanation

  • Here we have combined multiple scenarios and multiple files
  • The same scenario outline can be split to two scenarios
  • We can read two json files and store the file data into two variables
  • Then match the two result variable with the two responses
  • Just imagine if we have 100s of scenarios, it will never be efficient
  • So this is the best approach, combine all the json data into one file
  • Then use index to compare with the json array
⚠️ **GitHub.com Fallback** ⚠️