How to input variable parameter inside a string in testscript?

I need to input a parameter inside a string in testscript( eg: {{currentdate}} ), Inorder to verify the date in the response matches the date(variable) that I gave in the request URL
For example:
Request: www.google.com/search?q={{currentdate}}
Response: "Invalid date q should not be ‘somedate’

I need to check whether the {{currentdate}} parameter which inputs present date is same as the ‘somedate’ displayed in the response

I hope my question is clear, thanks in advance

1 Like

Save the {{currentdate}} in your environment variable or global variable, you can use the same in your request header as well as validation in your response.

Thanks Amit, but what happens is when I use it in script. I will be verifying like this

pm.test(“Check errorMessage”, function () {
var jsonData = pm.response.json();

pm.expect(jsonData.errorMessage).to.eql("Invalid date range, \"date\" cannot be after current date {{currentdate}}");

the current date value is inside a string so I’m not able to use it in {{currentdate}} format

Hey @immanuelsamuel,
You should be able to get the value of the variable current data in the script as pm.variables.get("currentdate");
And if you want to concat it with a string, you could try the following:

pm.expect(jsonData.errorMessage).to.eql(`Invalid date range, \"date\" cannot be after current date ${pm.variables.get("currentdate")}`);

Thanks,
Meena

1 Like

Thanks a lot @meenakshi.dhanani, It works flawlessly. I would be requiring more help, I just started creating testscripts in postman. So I think I would be posting more questions. It would be very helpful If you share your knowledge.
Thanks a lot again.

1 Like

Faced with error message | AssertionError: expected ‘Unexpected number of accounts for acc…’ to deeply equal ‘Unexpected number of accounts for acc…’
Response body has
“message”: “Unexpected number of accounts for account number 7777, expected 1 found 0”,
7777 value is variable and called as {{incorrect_Customer_acc}}
so I make such tets:

var account = pm.variables.get(“incorrect_Customer_acc”);
pm.expect(message).to.eql(‘Unexpected number of accounts for account number {{account}}, expected 1 found 0’);

@cryosat-observer-223

I’m assuming that your test is failing?

This is using a JavaScript feature called template literals.

JavaScript Template Literals (w3schools.com)
Scroll down to the section for “Variable Substitutions”.

You reference the variable using ${variableName} not {{variableName}}

let message = "Unexpected number of accounts for account number 7777, expected 1 found 0"
let account = "7777";

pm.test("Unexpected number of accounts", () => {
    pm.expect(message).to.equal(`Unexpected number of accounts for account number ${account}, expected 1 found 0`);
});

Those are back ticks `` around the string.

1 Like

Still the same error - AssertionError: expected ‘Unexpected number of accounts for acc…’ to deeply equal ‘Unexpected number of accounts for acc…’

@cryosat-observer-223

Did you try the code I pasted, you can see that it works. You just need to transpose the elements for the actual response.

If in doubt, console log the two values, so you can see them side by side.

console.log(message);
console.log(`Unexpected number of accounts for account number ${account}, expected 1 found 0`)

Both lines should be identical.

console.log(‘Unexpected number of accounts for account number ${account}, expected 1 found 0’);
shows me:
Unexpected number of accounts for account number ${account}, expected 1 found 0

inverse ` should be used, but I had ’ or "
NOW it Works
Thanks @michaelderekjones

It has to be backticks for the template literal to work (which is what allows you to use variable substitutions).

I use this all of the time to create dynamic test case names.

1 Like