Search code examples
restseleniumcucumberkarateweb-api-testing

How to parametrise a single Cucumber API test Scenario with Multiple Json request in KARATE framework


As of now, I have a single cucumber scenario which runs with a json file with single API request. I want to run the same scenario for multiple API request using same json. So I can verify multiple api test.

@Test1

Feature: Test_Multiple_API_Karate

Scenario: Exe - Individual Rule

* url restBaseApi
* configure headers = read('classpath:headers.js')
* def caseRequest = read('../data/caseRequest.json')
* def caseExpectedResponse = read('../data/caseExpectedResponse.json')


Given path ‘case-Karate-request’
And request caseRequest[i]
When method POST
Then status 200
Then print response
And match response == caseExpectedResponse[i]

We need to run karate request with caseRequest.json (as mentioned below)

[ { "srId": "1-2A1”, "process": “Information”, "area": “Software”, "subArea": “Technical”, "status": “Open” }, { "srId": “1-2A2”, "process": “Manufacture”, "area": “Software”, "subArea": “SAP”, "status": “Closed” } ]

How to run this scenario with the test data for both request in single execution.


Solution

  • please look into the documentation for Data Driven Scenarios

    @Test1
    
    Feature: Test_Multiple_API_Karate
    
    Background: 
    * def caseRequest = read('../data/caseRequest.json')
    * def caseExpectedResponse = read('../data/caseExpectedResponse.json')
    
    Scenario Outline: Exe - Individual Rule
    
    * url restBaseApi
    * configure headers = read('classpath:headers.js')
    
    
    Given path ‘case-Karate-request’
    And request __row
    When method POST
    Then status 200
    Then print response
    And match response == caseExpectedResponse[__num]
    Examples:
    |caseRequest|
    

    I also suggest you merge both JSON array into the singe JSON to avoid any confusion on index while altering