ehsan shirzadi
ehsan shirzadi

Reputation: 4859

Karate - How to import json data

I want to import some JSON data to my tests.
In order to documentation I should do that like this:

* def data = read('classpath:init/data.json')

I've created my JSON file with this content:

{
    "name": "ehsan"
}

This is my code:

  Background:
    * def data = call read('classpath:init/data.json')

  Scenario:
    * print data

But it prints nothing and says:

16:11:30.898 [main] WARN com.intuit.karate - not a js function or feature file: read('classpath:init/data.json') - [type: JSON, value: com.jayway.jsonpath.internal.JsonContext@7d61eccf]

Upvotes: 4

Views: 8531

Answers (2)

Homayoun Behzadian
Homayoun Behzadian

Reputation: 1163

Below code is correct:

* def data = read('classpath:init/data.json')

Only you must remove [call]

Upvotes: 10

Peter Thomas
Peter Thomas

Reputation: 58058

Yes, read the error message (and the doc) carefully - there is no meaning in 'calling' a JSON file, the moment you read it - you have your re-usable data already. Just do this:

Background:
    * def data = read('classpath:init/data.json')

  Scenario:
    * print data

Upvotes: 6

Related Questions