how to read a test data file ( a json file) in a feature file only once - karate

Karate has callonce that will call a function or feature only once for all scenerios in a feaure file? Is there a similar feature for reading a json file only once in a feature file before executing all scenarios. Can this be achieved by passing a function to karate.callonce() and that function will then just use read function to read the json file. Kindly answer how can I do this correctly?
I do not want to use another feature file for this. Should be able to pass a function name to the callonce.
I tried karate.callSingle and pass read function to read the json file.

Personally I think reading a JSON file from the file-system is so cheap that you are un-necessary worrying about this.
The only way that I know of is like this:
Feature:
Background:
* def dataFn = function(){ return read('data.json') }
* def data = callonce dataFn
Scenario: one
* print data
Scenario: two
* print data
But you are quite likely to complain here that we are initializing the function dataFn for every Scenario ;) In that case, you may need to look for another framework.
And I personally think calling a re-usable feature (for data set-up) is fine. Programming languages do this kind of re-use all the time.
EDIT: well, I just remembered that this would work:
* def data = callonce read 'data.json'
Explained here: https://github.com/karatelabs/karate#call-vs-read

Related

Calling a feature and passing in javascript variables

Been trying to figure this out for a while, please could someone help.
I have a set of 5 lines which I'd like to make reusable.
The lines do a "check event XXX has fired".
The lines make use of the "karate" variable and also the "json" command.
They're of the form:
* def message = myUtils.grabEvent(karate, myMessageListener)
* json event = message.text
* match event contains { ... some json in here ... }
* json eventPayload = event.payload
* match event contains { ... some payload json in here ... }
How do I go about making this reusable?
I have tried:
(A) Putting it all into a Javascript function
This failed because I don't know how to replicate the "json" command in Javascript
(B) Putting it all into a .feature file and calling that
This failed because I don't know how to pass the "karate" and "myMessageListener" variables into parameters of the .feature file.
Is it possible to put this into a reusable code block, please?
TIA
Yes I would recommend making this a reusable feature. Refer the documentation here: https://github.com/intuit/karate#calling-other-feature-files
And passing parameters is simple it would look like:
* def result = call read('reusable.feature')
Because by default, the "called" feature will "inherit" the variables of the calling feature.

Can I use the same below feature for the Json Api request aswell

Xmlsample.feature
Feature: test A
Scenario: test apple
* table test_apple_one
| payload_file |
| 'sample/ball.xml' |
* def result = call read('classpath:........./samplereq.feature') test_apple_one
Jsonsample.feature
Feature: test B
Scenario: test Mango
* table test_Mango_one
| payload_file |
| 'sample/cat.`enter code here`json' |
samplereq.feature
Feature: sample
  Background:
#Common Config load  
* def sampleURL = baseURL+'/sample/test'      
* xml payload = read('classpath:.....'+payload_file)   
#OAuth Signature generator
* def authorization = "oauth string"
  Scenario: Make the sample API call
Given url sampleURL`enter code here`
Given header Authorization = authorization
Given request payload
And header Content-Type = 'application/xml;charset=UTF-8'
And header Host = host
And header Accept = content_type
When method post    
Then match header Content-Type contains 'application/xml'
I am using the above feature samplereq.feature for the XML API request and
I Want to keep this feature samplereq.feature as generic and use the same for the Json Api request.can I do the same with JSON(Jsonsample.feature) API request ,please suggest
I read your question multiple times and I am sorry I can't understand what you are trying to do at all. Also I think you have not understood Karate properly. So please listen to my suggestion and take it in the right spirit.
I think you are trying to un-necessarily complicate your test. My sincere suggestion is - please don't try for this extreme re-use. I have observed that when teams attempt to create a super-generic re-usable test script - it just complicates things and becomes hard to maintain.
So please use different feature files for JSON and XML. For each test you can have multiple scenarios. Now, the scenario data can be the same for JSON and XML and you can read a common JSON file. Refer to this example on how you can create XML out of JSON: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature
If you still insist on the JSON and XML both in a generic feature, please take a look at this example: https://github.com/intuit/karate/tree/master/karate-demo/src/test/java/demo/loopcall
The above also has an example of calling a JavaScript function. Also please read the docs carefully.

How to retrieve an array of values from Java API in Karate?

I'm reading WSDL path and XML Request from excel file. I need to use these two data in my feature file. Reading action has been done on Java side but I don't know how to pass these into Karate - feature file.
I'm aware of single value passing from Java API to Karate like this.
* xmlstring xmlVar = response
* def APIHelperClass = Java.type('com.org.utilities.APIHelperClass')
* def result = APIHelperClass.getResponseFromFeatureFile(xmlVar,'getMembersDetailsResponse.xml')
Suggest me how to receive multiple values / array from JavaAPI into Karate.
Thanks
Please refer to this example: cats-java.feature and the corresponding Java class: JavaDemo.java.
So if you return data as a Java List it will be a JSON array. And a Map becomes a JSON object. This is explained in the documentation.
In your case if you return a HashMap with 2 keys e.g. wsdlPath and xmlRequest you should be easily able to use it in Karate.

Karate Automation: Is there any way we can set the Scenario name dynamically from a json file [duplicate]

This question already has answers here:
Can we parameterize the request file name to the Read method in Karate?
(2 answers)
Closed 1 year ago.
I am using a JSON file which act as a test case document for my API testing. The JSON contain Test Case ID, Test case Description, Header and Request body details, which should be the driving factor of Automation
Currently i am looping a feature over this json file to set different header and body validations. However it will be helpful if i can set the Scenario name from JSON file while its iterating
Something like
serverpost.feature
Feature:re-usable feature to publish data
Scenario: TC_NAME # TC_NAME is avaliable in the JSON data passed to this feature. However, CURRENTLY ITS NOT TAKING THIS DATA FROM JSON FILE.
Given path TC_ID # TC ID is taken from JSON
Given url 'http://myappurl.com:8080/mytestapp/Servers/Data/uploadServer/'
And request { some: '#(BODY)' } # Request Body Details is taken from JSON
Please suggest
In my honest opinion, you are asking for a very un-necessary feature. Please refer to the demo examples, look for it in the documentation.
Specifically, look at this one: dynamic-params.feature. There are multiple ways to create / use a data table. Instead of trying to maintain 2 files - think of Karate as being both - your data table AND the test execution. There is no need to complicate things further.
If you really really want to re-use some JSON lying around, it is up to you but you won't be able to update the scenario name, sorry. What I suggest is just use the print statement to dump the name to the log and it will appear in the HTML report (refer to the doc). Note that when calling a feature in a loop using a JSON array, the call argument is ALREADY included the report, so you may not need to do anything.
Just an observation - your questions seem to be very basic, do you mind reading the doc and the examples a bit more thoroughly, thanks.

Jmeter - read parameters from csv and write back updated parameters

I'm using Jmeter for testing. I need to use some keys in order to perform login, and then change the keys.
I understood that the best way to do it is to create csv file that contains two variables.
I understand how I can read the parameters (using 'CSV Data Set Config'), but I still don't know how to extract specific parameters from result (new keys) and save them in file instead the old ones.
You can use Regular Expression Extractor to extract the values from the response. This site will give you an idea how it works.
It is NOT a good idea to write in the same file which is read by CSV dataset config. Instead, you can use Beanshell post processor to create a CSV file & write as you want.
import org.apache.jmeter.services.FileServer;
f = new FileOutputStream("/your/file/path/filename.csv", true);
p = new PrintStream(f);
p.println("content,to be,written,in,csv,file");
p.close();
f.close();