0

I am unable to read request body while running a feature file:

Details:

user.json contains:

{"userId": "", "user":           {            "userName": ""          }}

I am setting these values to some random data generation using func:

util -> common-utilis.featurefunction() {return Java.type('util.Utilities').generateRandomString(4);}

Feature file to set up random values:

setupData.feature

Feature: Set random dataScenario: Set data* def requestBody = read('classpath:../template/user.json')

  #random data generation
* set requestBody.userId = "User_ID" + "_" + randomString()
* set requestBody.user.userName = "UserName" + "_" + randomString()

set up header details in seperate feature file:

header.featureFeature: Run Check

Scenario: Run Check* url BASE_URLGiven path 'user'And header Content-Type = 'application/json'And request requestBodyWhen method post

Scenario file:

userTest.feature

Feature: As a user, I want user service to support async call

Background: Setup

* def requestBody = callonce read('../template/setupData.feature')
* print requestBody
* def result = callonce read('../header.feature')
* print result
* def response = result.response
* def responseHeaders = result.responseHeaders

Scenario: Successful response for a request Then assert result.responseStatus == 200

What I tried so far:

  • I ran setupData.feature and can see system creates request body with random data successfully
  • I ran userTest.feature but system is displaying extra {} set

here is o/p while executing my feature file: (request body)

{"requestBody": {"userId": "User_Id_CsmY","user": {"userName": "UserName_PNKP"}}}

I can see extra {} and requestBody text

Not sure why these extra things are populated

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • I'm sorry I pass on this one unless you can simplify your question. may I point you to: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Mar 23 '22 at 04:23
  • all good @PeterThomas - I will recreate simple project one more time and see if can find an issue. Two things I want to achieve, 1. Generate random data on each run - to do so I created the func and reuse it i all test and this led to a situation where feature file is not easy to read so decided to work on point 2. where move req body in one file and setting values in second file and calling these two in main feature file – Heta Nanawala Mar 23 '22 at 04:40
  • just a recommendation, please don't over-engineer tests. I think you need to read this: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Mar 23 '22 at 06:09

0 Answers0