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

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.

Related

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

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

Is there any way to store array/list as variable/parameter in constructing an API in Postman?

I'm trying to parameterize a url in Postman and loop over that list to call multiple APIs one after another and append the JSON bodies.
For example:
if the url is GET https://location/store/{{user_id}}/date
and
user_id = ['1','3','5','2','6','8']
then how do I store user_idas a variable such that request can loop over each user_idin the url and generate an appended JSON body?
You can use data files. Store the user id values into a csv or JSON file and call that file in to your collection runner. The headers of the csv files can be used as variables names. please see the details of this approach in the below link
https://learning.postman.com/docs/postman/collection-runs/working-with-data-files/

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.

How to get XML file in camel

I am writing a code where I am sending some employees information inside an XML file to some other location using Apache camel. Now , I need to change some values in the XML. How can I parse the XML and change the value and then send it to the location. I tried to do using .process() , but it's not working. Any suggestions will be helpful.
Use Camel BeanIO and parse your XML into Java Models and process them use the same Camel BeanIO schema to convert them back to XML from Java Models.
Below you can see an example of modification of the XML document:
#Override
public void process(Exchange exchange) throws Exception {
//Get your XML from exchange (maybe, your need to convert them to DOM Document before processing)
Document doc = exchange.getIn().getBody(Document.class);
//Here you can modify your XML
//Modification example begin -------------
Element root = doc.getDocumentElement();
Element element = doc.createElement("newElement");
element.setTextContent("New element value");
root.appendChild(element);
//Modification example end ---------------
exchange.getIn().setBody(doc);
}
I think, in your attempt, you just did not set a changed body to the exchange.
If you want to work with Java POJO's and let a framework do the XML parsing / marshalling / unmarshalling, you can use the JAXB capability.
Then, you could use Java POJO's to do the "edits" (i.e change the values) and convert to (or from) XML as / when appropriate using the marshal / unmarshal feature. This avoids the need for parsing the XML directly yourself (though that would work too of course).
More info here.

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();