How to use Faker-variables in a pre-request-script in Postman? - postman

PostMan supports faker-library and provides several variables like "randomFirstName" and so on. However, I try to make them work in a pre-request script.
I've found a blogpost about this topic telling that it actually should work:
pm.variables.replaceIn("{{$randomProductName}}")
However, for me it doesn't work. The variable won't be replaced:
var firstName = "{{$randomFirstName}}"
console.log(firstName)
The output is:
{{$randomFirstName}}
How can I use these variables in a pre-request-script in PostMan?

You can't use the {{}} syntax in the pre-request and test tabs.
The following will do:
var firstName = pm.variables.replaceIn("{{$randomFirstName}}");

Related

Using XML response as variable in Postman

Blockquote
Hi All,
I have a question. I just started using Postman, so please sorry if I'm not using all the correct technical terms.
I want to have a Envirionment variable in postman that contains several XML tags.
F.e.
<Storess>
<Store>
<Id>322</Id>
</Store>
<Store>
<Id>323</Id>
</Store>
<Store>
<Id>324</Id>
</Store>
</Storess>
I want everything between the tags <Storess> and </Storess> to be copied to the environment variable so that I can use {{Storess}} in the next request that i'm sending.
How should I do this?
This may be useful: https://www.marklogic.com/blog/postman-test-scripting/
In the test script of the first request, you can set the result in an environment variable like this:
var result = pm.response.text();
console.log(result);
result = result.replace("<Storess>", "").replace("</Storess>", "").trim();
postman.setEnvironmentVariable("Storess", result);
console.log(postman.getEnvironmentVariable("Storess"));
You will be able to get the information in the next request like this:
{{Storess}}
If you want the XML inside the variable Storess to be well-formatted, you'll need to work it out a little bit.

how to request last segment of url in Flask

I feel like the answer is simple, yet I can't seem to figure it out. I have a URL:
http://127.0.0.1:5000/fight_card/fight/5
And I'm trying to use just the "5" in my code as that is the ID for the current fight in the SQL table. So far, I've tried
fight = Fight.query.filter_by(id=request.path).first()
However that returns:
fight_card/fight/5
Is there any way I can use "request" to target just the 5? Thank you in advance!
You should include a variable, current_fight_id, in your route definition. You can then access that variable in your view function.
#app.route('/fight_card/fight/<current_fight_id>')
def fight_route(current_fight_id):
print(current_fight_id) # use variable in route
Alternatively, you could use the approach you're using but modify the string that's returned. If you have a string:
endpoint = "fight_card/fight/5" # returned by your current code
You can access the five (current_fight_id) with:
current_fight_id = endpoint.split("/")[-1] # grab the segment after the last "/"
request.path would give you: /fight_card/fight/5. Then, you can split('/') to get a list of the parts.

retrieve and access stringified data

I have Get request which gives me response like below
{
"var1": "value1",
"var2": "value2"
}
I am saving it in an environment variable from Tests script as below
postman.setEnvironmentVariable("allData", JSON.stringify(responseBody));
In next Post request, I am trying to retrieve above values from Pre-request script as below
var jsonData = JSON.parse(allData)
However I am getting not defined error as below
There was an error in evaluating the Pre-request Script:
ReferenceError: allData is not defined
I can set each property in an individual variable and that works fine but that pollutes environment (as there are around 20 such properties). Please suggest better alternate for the same. Also suggest me how to access individual values in Body of the Post request. Can I do something like below?
{
"var1": "{{jsonData.var1}}",
"var2": "{{jsonData.var2}}"
}
OR I need to set values to individual variable in Pre-request script and use them in Body?
Thanks
If need to retrieve the data from the saved variable as a whole data set, you would need to make this reference to it when declaring the variable:
var jsonData = JSON.parse(pm.environment.get("allData"))
If you want to be able to use the single values from your variable in the Request Body you would need to parse them individually, in the Pre-Request Script, then store them as variables to use in the Request Body:
pm.environment.set("my_single_var_1", JSON.parse(pm.environment.get('allData')).var1)
pm.environment.set("my_single_var_2", JSON.parse(pm.environment.get('allData')).var2)
From here you can then set the {{my_single_var_1}} syntax in the Request Body and these placeholders would resolve to the values you have set.

Testing multiple JSON lines response

I am trying to make a test in Postman to verify some content in a JSON response. If I just try to verify a single line from the JSON response everything is fine. My problem starts when I need to test multiple lines of the JSON response. Is always failing. Any suggestion?
tests["Body matches string"] = responseBody.has("\"name\": null,
\"nameType\": \"NON_REFUNDABLE\"");
If I understand your question correctly I'd like to suggest that you approach this in a different way.
Instead of looking at the entire response body and seeing if the strings match you could alternatively test the individual Json properties that make up the response body. For example you could do the following:
var data = JSON.parse(responseBody);
tests["name is null"] = data.name === null;
tests["nameType is non-refundable"] = data.nameType === "NON_REFUNDABLE";
There are other alternatives as well but this is the first that comes to mind. For some more ideas about testing using postman check out their documentation and examples.

Magento: Building a store URL in CMS-syntax, using a variable

I am building an e-mail template. I want to build a URL like this:
http://store.com/path/to/page?shipmentid=123
This code builds the correct URL:
{{store url='path/to/page' _query_shipmentid=123}}
But the 123 part should be dynamic. I need it to pull from this variable:
{{var shipment.id}}
Is this even possible? I'm looking for something like this:
{{store url='path/to/page' _query_shipmentid=shipment.id}}
Use $ prefix to let Magento know that it is variable. This code should work:
{{store url='path/to/page' _query_shipmentid=$shipment.getId()}}