Karate gives you two ways on how you can do data driven testing in a karate project.we will see both the ways in this post.

1. The Cucumber Way
Cucumber has a concept of Scenario Outlines where you can re-use a set of data-driven steps and assertions, and the data can be declared in a very user-friendly fashion. Observe the usage of Scenario Outline: instead of Scenario:, and the new Examples: section
In the below example you can see how to create a GIT repo with random names using the random function, the function is defined in the karate-config.js file.
Feature: Creating a repo in GIT using karate
Background: Set the baseURL and call the authentication file
* url baseURL
* header Authorization = call read('basic-auth.js')
Scenario Outline: creating a repo and verifying the response
* path '/user/repos'
#Change the repo_name and any other fields
* def createRepoBody = read('createRepo.json')
#Get the data from the config function in the JS file
* def name = <Reponame>
#Setting the name for the request
* set createRepoBody.name = name
#Add the final body in the request
And request (createRepoBody)
* method post
#verify the status code
* status 201
* print response
#match the response with the name that was generated
* def repoName = response.name
* print repoName
* match repoName == <matchResponse>
# change the endpoint to GET
* path '/repos/'+username+'/'+repoName
* method get
#verify the status code
* status 200
* print response
#match the response with the name that was generated
* match response.name == <matchResponse>
#match type and data
* match response.private == false
Examples:
| Reponame | matchResponse |
| randomName | name |
| randomName + "QA" | name |
| randomName+ " "| name+"-"|
The limitation of the of Cucumber Scenario Outline: is that the number of rows in the Examples: is fixed. But the karate Dynamic Scenario Outline solves this problem for us.
2.The Karate Way
You can feed an Examples table from a JSON array, which is great for those situations where the table-content is dynamically resolved at run-time. This capability is triggered when the table consists of a single “cell”, i.e. there is exactly one row and one column in the table
Below example shows you how:
Feature: scenario outline using a dynamic table
Background:
#Define your json file in the "background"
* def createRepo = read('../callarray/Repo.json')
Scenario Outline: Repo name: <name>
Given url demoBaseUrl
And path '/user/repos'
And request { name: '<name>' }
When method post
Then status 201
And match response == { id: '#number', name: '<name>' }
# the single cell can be any valid karate expression
# and even reference a variable defined in the Background
Examples:
| createRepo |
The thing can be achieved using the CSV file as below
Scenario Outline: create <name> cat with desc = <desc> Given url ‘https://api.github.com/user/repo/user/repo’
And request { name: ‘<name>’, desc: ‘<desc>’ } When method post
Then status 201
And match response == { id:’#number’, name:’<name>’, desc:‘<desc>’ }
# the single cell in the table can be any valid karate expression: # csv, js, js function, json, variable, XPath or JsonPath.
Examples:
| read(‘gitCreate.csv’) |
For the below CSV file.
gitCreate.csv
name,desc
Repo1,One
Repo2,Two
Repo3,Three
There are also many other added features provided by karate using which you do variety of testing will less efforts.
Scenario Outline Enhancements
Karate has enhanced the Cucumber Scenario Outline as follows:
- Type Hints: if the Examples column header has a ! appended, each value will be evaluated as a JavaScript data-type (number, boolean, or even in-line JSON) – else it defaults to string.
- Magic Variables: __row gives you the entire row as a JSON object, and __num gives you the row index (the first row is 0).
- Auto Variables: in addition to __row, each column key-value will be available as a separate variable, which greatly simplifies JSON manipulation – especially when you want to re-use JSON files containing embedded expressions.
- You can disable the “auto variables” behavior by setting the outlineVariablesAuto configure setting to false.