Multipart file does not take the variable for the file name - karate

I am trying to use the multipart file with the file as a variable, my feature looks like below
Feature: Test feature
Background:
* def JavaUtil = Java.type('com.intuit.karate.demo.util.JavaUtil')
* def file = JavaUtil.createBatchFile("1003");
# Scenario: test one
# * print " this is the first test: "
* url appUrl + '/api/partner/v1/bulk/'
* print 'file :', file
Given path 'jobs', jobId, 'batches'
And multipart file newBatchInfo = { read: file}
When method post
Then status 200
When the code executes the file has the right value but the multipart file does not accept the file variable which has the absolute path.
Is this the right usage? If there are some docs around this, can someone point me. Thanks.
This is my output

There is a file: prefix, which you can use in cases like this, where you generate a file. I recommend you generate files into target when using Maven for e.g.
Refer docs: https://github.com/intuit/karate#reading-files
Also note that you should use embedded expressions:
* def file = 'file:' + JavaUtil.createBatchFile("1003")
# ...
* And multipart file newBatchInfo = { read: '#(file)' }

Related

How do you parameterize the read function in karate

How do you parameterize the read function in karate?
For example:
* def testcases = read("../TestCases/TestCases.csv")
* def wflowjson = '<wflowjson>'-- wflowjson is coming from TestCases.csv
* def expectedResponse = read(wflowjson)
* print 'expectedResponse--->' expectedResponse
There is no such thing as parameterizing a read of a JSON or CSV file.
But when variables are defined, they can get substituted within the file using embedded expressions: https://stackoverflow.com/a/60434757/143475
Note that you can use variables for the file-name when you read for example:
* def myJson = read(someVar + '/foo.json')

In Karate - Feature file calling from another feature file along with variable value

My apologies it seems repetitive question but it is really troubling me.
I am trying to call one feature file from another feature file along with variable values. and it is not working at all.
Below is the structure I am using.
my request json having variable name. Filename:InputRequest.json
{
"transaction" : "123",
"transactionDateTime" : "#(sTransDateTime)"
}
my featurefile1 : ABC.Feature
Background:
* def envValue = env
* def config = { username: '#(dbUserName)', password: '#(dbPassword)', url: '#(dbJDBCUrl)', driverClassName: "oracle.jdbc.driver.OracleDriver"};
* def dbUtils = Java.type('Common.DbUtils')
* def request1= read(karate.properties['user.dir'] + 'InputRequest.json')
* def endpoint= '/v1/ABC'
* def appDb = new dbUtils(config);
Scenario: ABC call
* configure cookies = null
Given url endpoint
And request request1
When method Post
Then status 200
Feature file from which I am calling ABC.Feature
#tag1
**my featurefile1: XYZ.Feature**
`Background`:
* def envValue = env
Scenario: XYZ call
* def sTransDateTime = function() { var SimpleDateFormat = Java.type('java.text.SimpleDateFormat'); var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+00:00'"); return sdf.format(new java.util.Date()); }
* def result = call read(karate.properties['user.dir'] + 'ABC.feature') { sTransDateTime: sTransDateTime }
Problem is,
While executing it, runnerTest has tag1 configured to execute.
Currently, it is ignoring entire ABC.feature to execute and also not generating cucumber report.
If I mention the same tag for ABC.feature (Which is not expected for me as this is just reusable component for me ) then it is being executed but sTransDateTime value is not being passed from XYZ.feature to ABC.feature. Eventually, InputRequest.json should have that value while communicating with the server as a part of the request.
I am using 0.9.4 Karate version. Any help please.
Change to this:
{ sTransDateTime: '#(sTransDateTime)' }
And read this explanation: https://github.com/intuit/karate#call-vs-read
I'm sorry the other part doesn't make sense and shouldn't happen, please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Karate: Multipart file : read works but passing content in value fails

I have a endpoint with a multipart request which takes two files as part of the request parameter.
I tried with the below snippet with read and it worked, but my use case is to take content from a file and pass it to the value parameter.
Can I pass the content as a file?
The working code if my I try to read the file from directory is
Working feature::
Scenario:
* configure headers = {'Content-Type' : 'multipart/form-data', 'Authorization': 'Bearer sgahshshshs'}
Given url "http://filecompare.com/compare"
And multipart file oldfile = { read: './oldfile.json', filename: 'oldfile.json'}
And multipart file newfile = { read: './newfile.json', filename: newfile.json'}
When method post
Then status 200
Not working feature::
Scenario:
Given url "http://download-oldfile/oldfile"
When method get
* def oldfile = response
Given url "http://download-newfile/newfile"
When method get
* def newfile = response
* configure headers = {'Content-Type' : 'multipart/form-data', 'Authorization': 'Bearer sgahshshshs'}
Given url "http://filecompare.com/compare"
And multipart file oldfile = { value: '#(oldfile)', filename: 'oldfile.json'}
And multipart file newfile = { value: '#(newfile)', filename: newfile.json'}
When method post
Then status 200
The contents are printed correctly but api returns error when I use value
Please let me know if I am missing something as part of running with the value keyword in multipart file.
Thank you
Can't think of anything other than try to convert the value to a string:
* string oldfile = response
If still stuck, follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
Had the same issue, resolved without needing to save to file by defining a variable holding the JSON object and using set to manipulate its value.
In your case this would look like:
* def temp = {value: '', filename: 'oldfile.json'}
* set temp.value = oldFile
#[...]
And multipart file oldFile = temp

as for 'replace' usage, does it support to replace the placeholder with value in place (I mean "over-write"))?

I need to upload a file to the server side, however, before doing that I need to replace a placeholder in the file with a dynamic value. does it support to get the placeholder replaced in place dynamically?
I noticed that I can replace the placeholder with ease using the 'replace' keyword. The following is my script:
Given path 'common/upload'
And multipart fields read('classpath:mainFlow/labresultUpload.json')
* def filename = 'PKU.A22backup'
* def someString = read('PKU.A22backup')
* print someString
* replace someString
|token|value|
|labsampleid|'123456'|
* print someString
* multipart file file = { read: "#(filename)", filename: "#(filename)"}
When method post
Then status 200
* def result = response[0].result
However, I need to replace the placeholder in place (here, I mean over-write) dynamically and then upload the file to the server side.
You have the option to supply a value instead of the file-name: https://github.com/intuit/karate#multipart-file
* multipart file file = { value: "#(someString)", filename: "#(filename)" }

How to call a feature file using a for loop. The feature file being called will also accept parameters from the feature file calling it

example :
Contents of FooGetRequest.feature file
* eval for (var i=0; i<foobarInDB.length; i++) call read('../features/BarGetRequest.feature') { foo_code:'#(foo_code)' , bar_code:'#(foobarInDB[i])'}
This is how BarGetRequest.feature file looks like :
Background:
* url baseUrl
Given path
"/v1/foo/"+foo_code+"/skus/"+bar_code+"/bar"
When method get
Then status 200
When I execute FooGetRequest.feature file i get the following error
[java.lang.RuntimeException: javascript evaluation failed: Expected ; but found read
you can use driven data driven feature in karate for loop over feature multiple times
Assuming foobarInDB is an array of bar_code and foo_code will always be same
* set foobarInDB[*].foo_code = foo_code
* call read('../features/BarGetRequest.feature') foobarInDB
refer Data driven feature
I wrote a small java script to return me a map like { foo : bar}
* def fun = function(x){return {foo :x }}
* def fooBarMap = karate.map(fooBarMap,fun)
* def validateResponse = call read('../features/BarGetRequest.feature) propertySkuMap
and in the BarGetRequest.feature I read the values accordingly.