Showing posts with label postman. Show all posts
Showing posts with label postman. Show all posts

Saturday, 16 July 2022

If else tests - based on xml response has certain text/string in postman | pm.response.text().has

Hi, 

Below piece of code verifies if the xml response has Text1 in it then performs Text1 tests else performs Text2 and Text3 tests respectively. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
if( pm.response.text().has("Text1")){
	
	console.log("Received : body contains Text1");
	
	pm.test("Body contains Text1",() => {
  		pm.expect(pm.response.text()).to.include("Text1");
	});
	
}else{
	
	console.log(" Not Received : Text1 so verifying for Text2 and Text3");

	pm.test("Body contains Text2",() => {
  		pm.expect(pm.response.text()).to.include("Text2");
	});

	pm.test("Body contains Text2",() => {
  		pm.expect(pm.response.text()).to.include("Text2");
	});
	
}


Append date and time to newman html extra report

Below command on node.js command line generates html report with date and time.  

newman run TestCollection.postman_collection.json  -e TestEnvironment.postman_environment.json -r htmlextra --reporter-htmlextra-export D:\posman\newman\TestCollectionReport_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%.html

Or

D:\postman>newman run "SmokeTests.postman_collection.json" --folder SmokeTestAPI -e "QALabEnv.postman_environment.json" -r htmlextra --reporter-htmlextra-export "C:\postman\newman\SmokeTestsAPIReport_%date:/=-%_%time::=-%.html" 


Example:
D:/postman/newman/TestCollectionReport_20221507_1720.html


Tap on the below image for best view



Friday, 20 May 2022

How to set JWT Token as postman environment variable and use it for "Bearer Token" type in POST request Authorization

 Hi, 

Bearer token response body: 

{
	"token_field1": "TokenField1ValueInEncodedFormat",
	"token_lasts_for_howmanyms": 7200, //2 mins
	"type_of_token": "Bearer"
}

Write below script in "Tests" tab of SOAP POST request (not in Pre-request scripts)

NOTE:
Token generated will get stored in VarJWTToken where VarJWTToken is an environment variable created in the environment.  
var data = JSON.parse(responseBody);
postman.clearEnvironmentVariable("VarJWTToken");
postman.setEnvironmentVariable("VarJWTToken", data.token_field1);

Use the VarJWTToken environment variable in request Authorization as shown in below image: 
Postman collection format: 
Token  (Token generated in this request will get stored in environment variable)
   SOAP POST Request (using environment variable POST authentication will be done) 

Print the environment name on console and based on environment selection write if else condition to execute a particular test in newman/postman

Hi, 

1) Print the selected environment on console
2) if else condition to execute a particular test based on the environment. 
 
console.info(pm.environment.name);
pm.environment.set("currentEnvName",pm.environment.name);
var environementName = pm.environment.get("currentEnvName");
console.log("Environement Name Current Used Is : "+environementName);

if(pm.environment.name=="Server1" || pm.environment.name=="Server2"
	||pm.environment.name=="Server3"||pm.environment.name=="Server4" ){
		postman.setNextRequest(null);
} else
{
	postman.setNextRequest("TestCase123");
}

References:
https://stackoverflow.com/questions/58612238/postman-get-the-environment-name-as-a-variable