Karate Automation: How to Set Dynamic Path for a URL, from a json data - karate

From Feature file 1, i am reading the content of a json file and passing it to the serverpost.Feature
feature file 1
* def output = read('output.json')
* def result = call read('serverpost.feature') output
In feature file 2, i am trying to set the Path as TC_ID and request body as BODY from the json data.
However, i am not able to set the path using the below feature. Please assist
Feature file 2
Given path '#(TC_ID)'
Given url 'http://myappurl.com:8080/mytestapp/Servers/Data/uploadServer/'
And request { some: '#(BODY)' } #### Here i am able to get the BODY data from JSON
When method post
Then status 200

The '#(foo)' notation applies only to JSON, XML or the right-hand-side of a match statement.
Please use it like a normal JS expression:
Given path TC_ID
or
Given path output.TC_ID

Related

Karate / Post Request : Unable to pass random generator variable in json payload

I am trying to create Karate feature file for API Testing.
It is a post request and we have to pass random number for 1 field i.e. order in request payload every time.
Now, I am trying to use this function in the same feature file to pass that random generated value in json payload before sending post request.
Could someone please have a look at my feature file and help.
Thanks
Also, Is there a way if i want to pass this same random value for a json payload created as a separate request.json payload file
Your requestPayload is within double quotes, so it became a string.
Here's an example that should get you going. Just paste it into a new Scenario and run it and see what happens.
* def temp1 = 'bar'
* url 'https://httpbin.org/anything'
* def payload = { foo: '#(temp1)' }
* request payload
* method post
And please read the documentation and examples, it will save a you a lot of time.

How to pass karate.prevRequest and response are the arguments from one feature file to another feature file

I wanted to pass karate.prevRequest and response are the arguments to another feature
Background:
* def KLog = Java.type('com.support.KaratePrint')
#SmokeTest
Scenario: Fetch random quote
* print 'My method executed' +KLog.scenarioInfo('Scenario_1', tcDetails.Scenario_1)
Given path '/api/users/'+pageNo.Pageno
When method GET
Then status 200
Here I wanted to pass KLog, Karate.prevRequest and response are the arguments to another feature
The documentation is
https://github.com/intuit/karate#calling-other-feature-files
The example file is
https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/callfeature/call-feature.feature
Please see the other files in the directory
https://github.com/intuit/karate/tree/master/karate-demo/src/test/java/demo/callfeature

How to pass a json file as a value for a key under request body of a feature file

enter image description hereI am creating a feature file using Karate framework and as per that i need to pass a json file as key value pair in the request body
eg
Given url
And def textJson = text1
And request{test:'test1.json',test2:'description text'}
when post
then status 200
enter image description here
the json file is read into a variable in another feature file and passed on to this feature file.
As of now i am getting the requested file is missing basically its not reading
Given url
And def textJson = text1
And request{test:'test1.json',test2:'description text'}
So if I understand this correctly, you intend to input the data from a json file into the test key under request. You are currently passing in a string instead of actually reading in a file. To read in a json file and save it as a variable try the following syntax.
And def test1 = read('test1.json')
And request { test1: '#(test1)'}
I am a little confused at some of the other syntax you have included. I can point out a couple other problems in the syntax.
Given url #You need to actually have a url after the url keyword
And def textjson = test1 #This does not seem to point to a reference
And request {test: 'test1.json', test2: 'description text'} # The test is not actually reading json in but the string "test1.json"
I wasn't exactly sure the scope of this question, but I hope that this helps.

Unable to upload multipart file in karate, Required request part '' not present

ActualAPIRequest OutputFromKarate
Trying to upload a json file for an api using karate. Since api takes multipart input i am passing multipart configurations in karate.
But Required request part 'inputData' not present error is coming. Is there any solution for this please?
I have attached actual input and result from karate screenshot for reference
Just make sure that the data type of inputData and maybe swaggerFile is JSON. Looks like you are sending a string.
Please refer to this section of the doc: https://github.com/intuit/karate#type-conversion
If the server does not like the charset being sent for each multipart, try * configure charset = null

How to pass string and file as input for form parameters in a POST method using Karate

I am trying to call a POST method which accepts the below form parameters
Path – A string specifying the path
FileName - A binary file
Media Type: multipart/form-data
Below code helps with the binary file part
Given multipart file xxx= { read: 'classpath:xxx', filename: 'xxx'}
However, in the same request I need to pass the string parameter as well.
Please suggest a way.
Thanks,
Kanika
You can combine multipart field with multipart file, refer to this demo example also.
Given multipart file xxx = { read: 'classpath:xxx', filename: 'xxx' }
And multipart field yyy = 'myvalue'