JordanCarrillo
JordanCarrillo

Reputation: 25

Karate: How to send variable to json file

I'm trying to create a dynamic request that should receive 2 parameters (username and password). To do so, I defined the below JSON file:

{
  "grant_type": "password",
  "client_id": "myClient",
  "username": "#(username)",
  "password": "#(password)"
}

To call my JSON, I set the below feature file where my intention is to define a variable for each parameter i want to send

Scenario: Get Sugar access token
    Given url baseUrl
    And path 'oauth2/token'
    And def username = 'user'
    And def password = 'password'
    And def requestBody = read('classpath:jsonFiles/requests/authRequest.json')
    When request requestBody
    And method POST
    Then status 200
    * print 'Response: ', response

Unfortunately, when I run the scenario, i'm getting below error message

10:59:59.111 [main]  INFO  com.intuit.karate - Karate version: 1.4.0-SNAPSHOT
10:59:59.286 [main]  INFO  com.intuit.karate.Suite - backed up existing 'target\karate-reports' dir to: target\karate-reports_1672160399285
10:59:59.303 [main]  INFO  c.intuit.karate.core.FeatureRuntime - found scenario at line: 4
11:00:00.174 [main]  ERROR com.intuit.karate - src/test/java/myCompany/testautomation/features/auth/getToken.feature:11
And def requestBody = read('classpath:jsonFiles/requests/authRequest.json')
js failed:
>>>>
01: read('classpath:jsonFiles/requests/authRequest.json')
<<<<
org.graalvm.polyglot.PolyglotException: not found: jsonFiles/requests/seugarAuth/authRequest.json
- com.intuit.karate.resource.ResourceUtils.getResource(ResourceUtils.java:126)
- com.intuit.karate.core.ScenarioFileReader.toResource(ScenarioFileReader.java:129)
- com.intuit.karate.core.ScenarioFileReader.readFileAsStream(ScenarioFileReader.java:99)
- com.intuit.karate.core.ScenarioFileReader.readFileAsString(ScenarioFileReader.java:95)
- com.intuit.karate.core.ScenarioFileReader.readFile(ScenarioFileReader.java:54)
- com.intuit.karate.core.ScenarioEngine.lambda$new$0(ScenarioEngine.java:120)
- <js>.:program(Unnamed:1)

src/test/java/myCompany/testautomation/features/auth/getToken.feature:11

Upvotes: 1

Views: 713

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58128

The use of classpath: makes more sense for Java projects. It looks like you are using the Visual Studio code extension, in which case your workspace root folder will be the classpath unless to switch to "Maven mode" or equivalent: https://marketplace.visualstudio.com/items?itemName=karatelabs.karate#run-mode

Or another option is to use a Java "runner" and use the Java extension for VS Code.

If all that above doesn't make sense, for now just keep things simple and use relative paths. For example if authRequest.json is side-by-side with your feature file, this would work:

* def requestBody = read('authRequest.json')

Relative paths and the this: prefix can be used, but I leave that to you to research: https://github.com/karatelabs/karate#path-prefixes

Upvotes: 1

Related Questions