looking at the provided screenshot
In the response at Pretty section, I have 36 lines of data.
I would like to write a test for this request, which would check if the response body lines were 36 each time the request is run.
What would be your suggested test code for this test?
I had the same problem. Here's my solution:
let text = pm.response.text();
let lines = text.split("\n");
let length = lines.length;
pm.expect(length).to.eql(36);
Related
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.
I am testing an endpoint that takes a json body as request and return a json object with one field with encoded value for a pdf generation. I get 201 status code, but I could not get to print the response. It looks blank in console.
Sample Request Paylaod might be;
{'name': 'firstName',
'lname': lastname }
and response looks like below;
{ 'getPDF': 'JBKJGHUKSxjsnckdskcnejcnd/vdsv+VFDVfsd..........'}
And I just print the response as below;
* status 201
* print response
but it does not print anything. Is there a way to print the encoded value in the response?
Thanks.
It sounds to me that the response is actually empty. Anyway if it is bytes, you can try convert to a string:
* string response = responseBytes
If that doesn't work - who knows, Karate may have a bug :) So please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
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
I'm using the tests of Postman to log into the console some of the details included in a JSON response.
The part of the test that logs is the following:
var data = JSON.parse(responseBody);
if (data.result[0] !== undefined) {
console.log(data.result[0].number, "|", data.result[0].category;
}
else console.log(QUERYSTRING_PARAMETER, "is not present");
I've tried many sintaxes/formats to have the value of the QUERYSTRING_PARAMETER passed to the test. However when data.result is empty with every sintax I've tried, the test simply logs QUERYSTRING_PARAMETER not defined. How can I pass the value from the query string parameters in the URL to the test to be evaluated/logged?
Thanks in advance
I'm not sure if it corresponds to what you need, but if you want data from your query, you may use the 'request' object (refer to the Postman sandbox, paragraph 'Request/response related properties').
You can get request method, url, headers and (maybe the one for you) data.
Alexandre
I want to validate my response ok or bad using Response Assertion.
I have set the Response Field to Test as Response Message.I have set Patterns to test as "ok".
Bue the problem is,it shows green signal in result tree Listener even if i receive bad response. I have attached the screen shot.How to solve this?
This means that your response contains OK somewhere.
If you want to validate it returns a 200 code, check :
response code
equals
replace oK by 200
But also add a reponse assertion that check a text in you page, in this case replace OK by the text and check Text response / And substring.