Karate gives us lots of options to work with data. Passing data from one feature file to another is very common requirement when it comes to automation.

Here is how you can pass data from one feature file another.

we need to have our first feature file which will be called from the second feature file.Here I’m trying to explain using the Git Repo APIs. In the first feature file creating a Git Repo.

karate dsl , karate automation testing, karate
Scenario: 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 = randomName
  #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 == name
  # 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 == name
  #match type and data
  * match response.private == false

Once you have the repo created you can pass it onto another feature file, here I’m passing the repo name to update feature file , where I’m trying to update same repo.

Scenario: updating a repo and verifying the response
  * def createRepoResponse = call read('createRepo.feature')
  * print createRepoResponse
  * def repoName = createRepoResponse.name
  #Change the repo_name and any other fields
  * def createRepoBody = read('createRepo.json')
  #Setting the description for the request
  * set createRepoBody.description="This is an updated description"
  * set createRepoBody.private = true
  #Add the final body in the request
  And request (createRepoBody)
  * path '/repos/'+username+'/'+repoName
  When method patch
  #verify the status code
  * status 200
  * print response
  #match the response with the name that was generated
  * match response.description == "This is an updated description"
  #match type and data
  * match response.private == true
  * match response.name == repoName