KarateDSL Parsing a value from a response and adding to next scenario payload not working - karate

New to Karate and I have read a load of the Karate tutorials and cannot get my head around what looks to be a trivial issue.
I have one post request that successfully lists all applications, from the response I get the ID of the first application and print it to console and it displays with no issue. However, when I come to use the applicationId in the payload for the next scenario (int he same feature file, the applicationId is not added to the payload.
Scenario: List all applications
Given path '/ListApplications'
And request {"request":{},"Session":'#(session)'}
When method POST
Then status 200
And def applicationId = response.Applications[0].Id
* print 'Hello ' + applicationId //i see the application id of 7203 here in the console log
Scenario: Get Application
Given path '/GetApplication'
And request {"request":{"ApplicationId":'#(applicationId)'},"Session":'#(session)'}
When method POST //post here just uses string applicationId instead of 7203
Then status 200
Any help greatly appreciated

You are using 2 Scenarios where you should have only one.
Please read this section of the docs: https://github.com/intuit/karate#script-structure
I think if you comment out this line it will work:
# Scenario: Get Application

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 multiple json records using data driven approach in Karate DSL?

We have gone through the Karate documentation where we can compare the exact JSON object as a response (which contains multiple data records) but how can we pass and read the JSON in a single scenario?
Below is my sample.JSON and I want to read this in the request payload.
[{"name":"John","salary":"10000","age":"25"},
{"name":"Maria","salary":"20000","age":"27"}]
I have tried the JSON structure in the above format, however, I am getting below exception. Kindly help me in this.
status code was: 400, expected: 200, response time: 4315
Kindly suggest how to read and pass it in request payload of single scenario.
Thank you.
Status code 400 means you have made some other mistake with the request. Karate is working fine, it is just an HTTP client, maybe the request was not in the "shape" the server was expecting. Talk to the server-side team if you can or check the documentation of the API.
Here is a simple example that works, paste it and try:
* def body = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]
* url 'https://httpbin.org/post'
* request body
* method post
* status 200
EDIT: for looping, please read the documentation.
The below example is just one way of doing it - please identify what best you are comfortable with: https://github.com/intuit/karate#data-driven-tests
Feature:
Background:
* def data = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]
Scenario Outline:
* url 'https://httpbin.org/post'
* request __row
* method post
* status 200
Examples:
| data |

karate | xml post method exeuction

I’m having issue with xml post request where post method is not executed. When I try to post same request body in post man it worked.My test is success with 200 but actual request is not executed.
Please let me know if I’m missing
To pass the request body,I’m calling through java object and payload is correctly constructed and printed.In execution test is success and doesn’t print response.But actually test is not executed.
Only headers are printed.
***************** create-user.feature*****************
Feature: create ims user for provided country
Requires country code,
Background:
# load secrets from json
* def createuser = Java.type('com.user.JavaTestData')
* def create = createuser.createUser("US")
Scenario: get service token
Given url imscreateuserurl
And request create
When method post
Then status 200
* print response
***************** create-user.feature*****************
Here is java class
public class JavaTestData {
private static final Logger logger = LoggerFactory.getLogger(JavaTestData.class);
public static String createUser(String countryCodeInput) {
logger.debug("create user for country code input", countryCodeInput);
Unless you post a full working example, no one can help you. Pretty clear that the value of create is null or empty.
Also I personally think you are wasting your time using Java. The whole point of Karate is to avoid using Java as far as possible.
Look at these examples for ideas: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature
Edit: also refer to the doc on type-conversion: https://github.com/intuit/karate#type-conversion
#Peter, here is my feature file
Feature: create ims user for provided country
Requires country code,
Background:
# load secrets from json
* def createuser = Java.type('com.adobe.imscreateuser.JavaTestData')
* def create = createuser.createUser("US")
Scenario: get service token
Given url imscreateuserurl
And header Content-Type = 'application/xml; charset=utf-8'
And request create
When method post
Then status 200
* print response
I have performed print for create and showing complete payload.At when method post -> statement its going as null or empty...
Not sure where it is missing

Use URL as an API method for Slackbot in Express js

I am still new to javascript and trying to write a Slackbot in express js. I want to use the method defined in https://api.slack.com/methods/channels.history. How should this look syntacticly and how do I use it since the method is simply a URL?
You need to make an http request for the URL and you'll be returned a response with an object containing the status (ok:true|false), if there are more messages (has_more:true|false), and then an array of the actual messages (messages:array).
The response should look something like this:
{
has_more:true
messages:Array[100]
ok:true
}
The url that you make the get request to should look something like:
https://slack.com/api/channels.history?token=BOT_TOKEN&channel=CHANNEL_ID&pretty=1
Where BOT_TOKEN is the token attached to the bot you created, and CHANNEL_ID is the ID (not the name) of the channel whos history you want to get (9 uppercase alphanumeric characters, starts with a "C").
There are also a few other parameters you can include in the url. For example, "latest=", "oldest=", "inclusive=", "count=", and "unreads=". Details about those parameters can be found on the page you linked to (https://api.slack.com/methods/channels.history).
If you want to test it out in your browser's console, find a page where jQuery is loaded, open your dev tools and head into the console, and enter the following (with your bot token and channel id swapped in):
$.get('https://slack.com/api/channels.history?token=BOT_TOKEN&channel=CHANNEL_ID&pretty=1', function(response){console.log(response)});

Apache JMeter : How to compare responses of same HTML request during loop?

Please reply me with the solution for following problem:
With Apache JMeter I have ran the 2000 requests per 2 minutes with 10 loops.
The request is for checking hotel availability for mentioned start and end dates.
Following 3 important parameters are there:
Hotel id, Start date and End date
With the help of 'CSV data set Config' I have stored input from 2 text files (1 text file has hotel ids and another having start,End dates). and used variables into http requests.
By taking listeners View Result Tree and summary report, I checked the responses of each request. For some requests I am getting blank responses, as hotel is not available. Now I want to find out the exact count of the blank responses. Please let me know if anybody has solution for it.
Thanks in advance
You can use i.e. Size Assertion or Response Assertion to mark blank requests as failed ones.
If you just need just number and don't want to record failures on blank responses I would suggest using a Beanshell Post processor as follows.
Define a User Defined Variable called i.e. blank with the value of 0
Add a Beanshell Post Processor to each request which may produce blank response
At the end add a Beanshell Sampler which will report the final variable
Example Post Processor code:
int blank = Integer.parseInt(vars.get("blank")); //get current "blank" variable value
String response = new String(data); //get response data as string
if (response.length() == 0) // if response length equals zero (feel free to update this as requred)
{
blank++; // increment "blank" variable
}
vars.put("blank", String.valueOf(blank)); // update "blank" variable value
Example final Beanshell sampler code:
log.info("Pages with blank response count: " + vars.get("blank")); // print current "blank" variable value to "jmeter.log" file
You should see something like below in jmeter.log file:
2014/05/15 18:36:34 INFO - jmeter.util.BeanShellTestElement: Pages
with blank response count: 15
2014/05/15 18:36:34 INFO -
jmeter.threads.JMeterThread: Thread finished: Thread Group 1-1
2014/05/15 18:36:34 INFO - jmeter.engine.StandardJMeterEngine:
Notifying test listeners of end of test
2014/05/15 18:36:34 INFO -
jmeter.gui.util.JMeterMenuBar: setRunning(false,local)
See How to use BeanShell: JMeter's favorite built-in component guide for JMeter extension with scripting walkthrough and a kind of Beanshell cookbook.