Can I use environment variables set in a previous test in the payload I am posting?
eg.
POST /list
{
"some_key": environment.saved_value
}
Yes, you can do that. You send it up like this
{
"some_key" : "{{environment_variable_name}}"
}
So, if in your previous test you had set it with something like
postman.setEnvironmentVariable("id","some_value")
You can use it in your POST with
{
"some_key" : "{{id}}"
}
Hopefully, that answers your question
Related
Im currently trying to get used to POSTMAN and i was wondering if there is a way to store variables from my request JSON Body via Pre Request in some environment variable so ican resuse it in the tests for response value cheks
This is how my json File might look like
{
"text" : "myText",
"attachments": {
"text": "myText2",
"anotherText" : "myText3"
}
So i want to get all Values, store them in a variable before sending my request, and then test if they match the expected value in my response
(example: myText2 gets mapped to green, myText3 gets mapped to red and so on)
That would make it possible to write one test for several request
Thanks a lot!
You can write the following in your script:
let body = JSON.parse(pm.request.body);
_.forEach(body, (value, key) => pm.environment.set(key, JSON.stringify(value)));
This will set each key and it's associated value as an environment variables.
Note you'll need to JSON.parse the value in the test script before using it for testing.
For eg in your test script you'll need to do something like this:
let attachments = JSON.parse(pm.environment.get('attachments'));
pm.test('All attachments are of correct value', function () {
// ...write your test here using the `attachments` variable
});
Is there a way to unset environment variables dynamically?
I would like to access the environment vars and do a find & replace/delete action so I can test more dynamically.
For instance, say I want to test the creation of users, I create vars like {{tmp-username}}, {{tmp-email}}, etc... replace them with other values for the next test and remove them when I'm done.
I would do a stringsearch on tmp- if I knew how to access these using code...
Thanks in advance for any reply
To clarify, this question is different: Postman: How to delete/clear postman environment variable at run-time
This deals with knowing the exact name of the var you wish to unset. I want to search or iterate trough the vars to remove or edit them.
Could you use a function in the Tests tab to iterate through the variables and clear them out after the last test had run?
For example:
function cleanup() {
const clean = _.keys(pm.environment.toObject())
_.each(clean, (arrItem) => {
pm.environment.unset(arrItem)
})
}
cleanup()
This wouldn't 'replace' the values but this would give you confidence that the ones that are set during the run are not being used again.
EDIT
If you wanted to clear out a specific set of variables, ones that you have given a certain prefix, you could use this:
function cleanup() {
const clean = _.keys(pm.environment.toObject())
_.each(clean, (arrItem) => {
if (arrItem.startsWith("tmp")) {
pm.environment.unset(arrItem)
}
})
}
cleanup()
If you want to see all the keys and the values you could use this to log them to the console:
_.map(pm.environment.toObject(), (value, key) => console.log(`The key is '${key}' and the value is '${value}'`))
Assume that my server got a GET Request as
www.example.com/?hub.mode=subscribe&hub.challenge=1320729329&hub.verify_token=Hello
I want to echo the part hub.challenge back. How can I do that in Ballerina language?
You need to use #http:QueryParams for this. Refer following example :
import ballerina.net.http;
import ballerina.lang.system;
#http:BasePath {value:"/shop"}
service echo {
#http:GET{}
#http:Path {value:"/order"}
resource echoGet (message m, #http:QueryParam {value:"orderid"}string orderid) {
system:println("orderid" + orderid);
reply m;
}
}
A GET request as http://localhost:9090/shop/order?orderid=123 would get set to variable orderid, which you can then use in further implementation. (Please note I used system:println for the purpose of the example)
Based on loopback documentations, We can override remote methods.I want to override PUT : /products/{id} requests.
I try this:
module.exports = function (product) {
product.save = function(data,callback){
callback();
}
};
I try it with : update , updateAttributes , upsert and all related methods but still not working...
Although overriding create method working but update no!
Is there any suggestion?
Use product.prototype can trigger put requests like this :
product.prototype.updateAttributes = function (data,callback) {
console.log('updateAttributes');
callback();
};
If you want to disable the API end point, you can use the following,
Product.disableRemoteMethod('update', true)
Product.disableRemoteMethod('updateAttributes', true)
Product.disableRemoteMethod('upsert', true)
Documentation Link - https://docs.strongloop.com/display/public/LB/Exposing+models+over+REST#ExposingmodelsoverREST-Exposingandhidingmodels,methods,andendpoints
I want to be able to retrieve a certain conversation when its id is entered in the URL. If the conversation does not exist, I want to display an alert message with a record not found.
here is my model hook :
model: function(params){
return this.store.filter('conversation', { status : params.status}, function(rec){
if(params.status == 'all'){
return ((rec.get('status') === 'opened' || rec.get('status') === 'closed'));
}
else{
return (rec.get('status') === params.status); <--- Problem is here
}
});
}
For example, if I want to access a certain conversation directly, I could do :
dev.rails.local:3000/conversations/email.l#email.com#/convid
The problem is when I enter a conversation id which doesn't exist (like asdfasdf), ember makes call to an inexisting backend route.
It makes a call to GET conversation/asdfasdf. I'm about sure that it is only due to the record not existing. I have nested resources in my router so I'm also about sure that it tries to retrieve the conversation with a non existing id.
Basically, I want to verify the existence of the conversation before returning something from my hook. Keep in mind that my model hook is pretty much set and won't change, except for adding a validation on the existence of the conversation with the id in the url. The reason behind this is that the project is almost complete and everything is based on this hook.
Here is my router (some people are going to tell me you can't use nested resources, but I'm doing it and it is gonna stay like that so I have to work with it because I'm working on a project and I have to integrate ember in this section only and I have to use this setup) :
App.Router.map(function(){
// Routing list to raw namespace path
this.resource('conversations', { path : '/' }, function() {
this.resource('conversation', { path : '/:conversation_id'});
});
});
This also happens when I dont specify any id and I use the hashtag in my url like this :
dev.rails.local:3000/conversations/email.l#email.com#/ would make a call to conversation/
I know it is because of my nested resource. How can I do it?
By passing a query to filter (your { status : params.status}) you are asking Ember Data to do a server query. Try removing it.
From the docs at http://emberjs.com/api/data/classes/DS.Store.html#method_filter:
Optionally you can pass a query, which is the equivalent of calling find with that same query, to fetch additional records from the server. The results returned by the server could then appear in the filter if they match the filter function.
So, remove the query:
model: function(params){
return this.store.filter('conversation', function(rec) {
if (params.status == 'all') {
return rec.get('status') === 'opened' || rec.get('status') === 'closed';
} else {
return rec.get('status') === params.status;
}
});
}
Ok so here is what I did. I removed my nested resource because I realised I wasn't using it for any good reason other than redirecting my url. I decided to manually redirect my url using javascript window.location.
This removed the unwanted call (which was caused by the nested resource).
Thanks to torazaburo, you opened my eyes on many things.