So my issue is that I want to have 2 tests for a single api call - one pass and one fail with missing params.
Here is what I have:
pm.test("Successful Login", function () {
pm.response.to.have.status(200);
});
pm.test("Missing Parameters", function () {
const currentUsername = pm.environment.get("username");
pm.environment.set("username", null);
pm.response.to.have.status(400);
//pm.environment.set("username", currentUsername);
});
So as you can see, I set username to null for the second test, only to set it back to is original value after the test. What I found was that instead of running the script sequentially, postman set my username to null before the first test could have been run, so I fail the first test. What should I do ?
Ok guys. Apparently you cannot set variables in the testing scripts because the testing script is run after the api call has been made. This needed to be set in the pre-request script. As for how to set all various tests in just on request I dont think this can be done. Therefore, I am just making a new request per test case.
Related
This question already has an answer here:
Pass Json to karate-config.js file
(1 answer)
Closed 1 year ago.
We have a set of services that all expose certain common endpoints such as a health check, version information etc. I am trying to use karate to write smoke tests for these multiple services in a reusable way that i can just pass in the service name and endpoint and have the tests executed for each service.
basicChecks.feature
Feature: Smoke Test. verify health check and version and index are ok
Scenario: Verify that test server health check is up and running
Given url '#(baseUrl)'
Given path '/health'
When method get
Then status 200
And match response == "'#(name)' ok"
Given path '/version'
When method get
Then status 200
And match response contains {'#(name)'}
testServices.feature
Feature: Smoke Test for services.
Scenario: Verify that test server health check is up and running
* call read('basic.feature') { name: 'service1' , baseUrl : service1Url }
* call read('basic.feature') { name: 'service2' , baseUrl : service2Url }
karate-config.js
function fn() {
var env = karate.env; // get java system property 'karate.env'
karate.log('karate.env system property was:', env);
if (!env) {
env = 'local'; // a custom 'intelligent' default
}
var config = { // base config JSON
appId: 'my.app.id',
appSecret: 'my.secret',
service1Url: 'https://myserver/service1'
service2Url: 'https://myserver/service2'
};
// don't waste time waiting for a connection or if servers don't respond within 5 seconds
karate.configure('connectTimeout', 5000);
karate.configure('readTimeout', 5000);
return config;
}
When i run this i get an error suggesting that the baseUrl is not being picked up when passed in
20:27:22.277 karate.org.apache.http.ProtocolException: Target host is not specified, http call failed after 442 milliseconds for url: /health#(baseUrl) 20:27:22.278 cas/src/test/java/karate/smoke/basic.feature:7 When method get http call failed after 442 milliseconds for url: /health#(baseUrl) cas/src/test/java/karate/smoke/basic.feature:7
I looked at https://intuit.github.io/karate/#code-reuse--common-routines but could not figure out how to use the same tests but pass in different endpoints?
Or maybe since i am totally new to karate there is a much better way of doing this than what i have outlined?
Thank you for your time.
Edit - I am trying to test different micro services in the same environment and not trying to switch different environments etc.
This is not the recommended approach. When you have different URL-s for different environments, you should switch environments using the approach in the documentation (setting karate.env) and NOT depend on re-use via "call" etc.
Example: https://stackoverflow.com/a/49693808/143475
And if you really want you can run suites one after the other switching the karate.env, although that is rare.
Or if you just trying "data" driven testing, there are plenty of ways, just read the docs and search Stack Overflow for Scenario Outline: https://stackoverflow.com/search?tab=newest&q=%5bkarate%5d%20Scenario%20Outline
If you are trying to do this "clever" re-use using "call" I strongly recommend that you don't and please read this for why: https://stackoverflow.com/a/54126724/143475
EDIT - I think you ran into this problem, read the docs please: https://github.com/intuit/karate#rules-for-embedded-expressions
I have a postman test script to use with test runner. I am trying to pass dynamic value from file to validate response with no success. I am able to pass value to request, but not able to use value from data file in test script. I want to validate response with data passed from CSV file. Is something like below possible in first place?
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include('{{$column1}}');
});
Found that variable usage in test script is different. Below works.
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include(pm.variables.get("column1"));
});
i would like post different API body every time the test case run.
i have set the variable at POST object
e.g. testID default value test0001
then the HTTP body as below, test and verify passed.
{
“drugId”: “$testID”,
}
what syntax/command i can use in test case like parameterize test step, so first time test case run
drugId = test0001
second time test case run, it will be
drugId = test0002
Your HTTP body should be something like
{
“drugId”: “${testID}”
}
And your request in code should look something like this
response = WS.sendRequest(findTestObject('requestObject',[('testID'): 'test0001']))
where requestObject is your request saved in the Object Repository.
Implementation
Now, if you want to iterate this 10 times, you can do the following:
create a new test case called "callee" with the following content
response = WS.sendRequest(findTestObject('requestObject',[('testID'): testID]))
create another test case called "caller" with the following content
String test = "test000"
for(i=0;i<10;i++){
WebUI.callTestCase(findTestCase("callee"), ["testID":"${test+i.toString()}"], FailureHandling.OPTIONAL)
}
run the "caller" test
Whenever I run following from command line
newman run https://www.getpostman.com/collections/abcd1234
I get output displaying the statistics of failed and execute.
But I am looking for the complete HTML or JSON response from the URL to be printed on terminal after executing the above Newman query.How can I achieve this?
You have to add some log output in your requests.
For the requests where you want to see the response output add the following in the Postman Tests tab:
console.log(responseBody); // full response body
If you want to log a specific part you have to parse the response body into a JSON object:
let response = JSON.parse(responseBody);
console.log(reponse.myprop); // part of the full response body
Now if you run this collection with newman the CLI reporter will print the console log parts as well.
You need to use Postman API.
So you need to run something like this
newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey
(see http://blog.getpostman.com/2018/06/21/newman-run-and-test-your-collections-from-the-command-line/)
You can get ApiKey in your Postman Cloud. You need to go to the workspace -> Integrations -> Browse Integrations -> Postman API View details -> Detail Get API Key/Existing API Keys
If you also need to add environment (if you use Variables), what you need is to run the same command with -e parameter 'newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey -e dev_environment.json'
But what if you have your environment in the cloud as well? According to this document https://www.getpostman.com/docs/v6/postman/collection_runs/command_line_integration_with_newman you can pass URL as value. So you may run something like this
newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey -e environments/{{environment_uid}}?apikey=myPostmanApiKey
It worked for me, hope this will help
I am using newman for webservices and microservices testing. This works fine for me.
summary.run.executions[0].response.text().toString()
After done event you should be able to get the response.
d is the collection exported from Postman.
newman.run({
collection: d,
// reporters: 'cli',
iterationCount: 1,
timeoutRequest: 10000,
timeoutScript: 5000,
delayRequest: 0,
insecure: false,
}).on('done', (err, summary) => {
if (err || summary.error) {
console.error('\ncollection run encountered an error.');
reject(summary.error);
}
else {
var xml = summary.run.executions[0].response.text().toString();
console.log(xml)
}
})
})
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