apitestingkaratedata-driven

How do i parameterize parameters i am passing in my API request & execute through Karate?


I am testing API's for my application & each API has multiple parameters to be passed, ex. below:

https://abc.xyz.com/***.svc/restful/GetSummary?FromDate=2019/06/28&ToDate=2019/06/28&CompAreaId=15&RegId=4

Each parameter in the request has multiple values (within a defined set of values), so if I want to parameterize each parameter with all the values it could possibly have, how can I create a scenario that will help me achieve this?

I would appreciate any hints/views.

I have been passing parameters as shown in the code below, but unable to pull off the scenario above mentioned, it would be time-consuming & repetitive to pass parameters in a separate scenario each time.

Scenario: Verify if GetContext API returns data with params

Given path 'GetContext'
And param FromDate = '2019/06/27'
And param ToDate = '2019/06/27'
And param CompAreaId = 20
And param RegId = 4
When method get
Then status 200
* def res = response
* print 'response:', response

Solution

  • You can use “Scenario Outline” to achieve that. The following modified code below will run for the 3 rows in the example. (related link: https://github.com/intuit/karate#the-cucumber-way)

    Scenario Outline:
    Given path 'GetContext'
    And param FromDate = '<FromDate>'
    And param ToDate = '<ToDate>'
    And param CompAreaId = <CompAreaId>
    And param RegId = <RegId>
    When method get
    Then status 200
    * def res = response
    * print 'response:', response
    
      Examples:
        | FromDate   | ToDate      | CompAreaId | RegId |
        | 2019/06/27 | 2019/06/27  | 20         | 4     |
        | 2019/06/28 | 2019/06/28  | 21         | 5     |
        | 2019/06/29 | 2019/06/29  | 22         | 6     |
    

    Instead of a static count, if you have a dynamic number of rows, you can store the parameter values in a json or CSV and reference it in the example. (related link: https://github.com/intuit/karate#dynamic-scenario-outline)