Sunday 23 July 2023

Batch script to execute newman/postman collection on two different environments by opening two command prompts at a time

Hi, batch script below does the following: 

1) Delete newman html extra report if already exists in Reports folder under C drive
2) Starts two command prompts in the background and calls the node.js to run the same set of newman/postman smoke tests at a time on two different environments. 

As two start commands begins execution simultaneously , we need NOT to wait until the first one finishes so can avoid sequential execution and achieve kinda of parallel execution on two different environments with same collection.  

REM set the reports path
@echo on
set "filePath-1=C:\Reports\SmokeTestReportServer-1.html"
set "filePath-2=C:\Reports\SmokeTestReportServer-2.html"

REM Delete the reports if already exists in Reports folder
if exist "%filePath-1%" del /Q /F "%filePath-1%"
if exist "%filePath-2%" del /Q /F "%filePath-2%"

REM Executing Smoke Tests at a time by opening two command prompots for two different servers 
echo 'Server1'
start /B cmd /c call newman run "SmokeTests.postman_collection.json" -e "Server1Env.postman_environment.json" -r htmlextra --reporter-htmlextra-export "C:\Reports\SmokeTestReportServer-1.html" 
echo 'Server1 execution is done'

echo 'Server2'
start /B cmd /c call newman run "SmokeTests.postman_collection.json" -e "Server2Env.postman_environment.json" -r htmlextra --reporter-htmlextra-export "C:\Reports\SmokeTestReportServer-2.html"
echo 'Server2 execution is done' 

timeout /t 3 /nobreak
exit


start: Start a program, command or batch script, opens in a new/separate Command Prompt window.
More about start command and /B option -->  https://ss64.com/nt/start.html
/B         Start application without creating a new window. In this case
              Ctrl-C will be ignored - leaving Ctrl-Break as the only way to 
              interrupt the application.

cmd: Start a new CMD shell and (optionally) run a command/executable program.
More about cmd command and /C option: --> https://ss64.com/nt/cmd.html
   /C     Run Command and then terminate

del: Delete one or more files.
More about del command and /Q /F options  --> https://ss64.com/nt/del.html
      /F  Ignore read-only setting and delete anyway (FORCE) 
      /Q  Quiet mode, do not give a Yes/No Prompt before deleting.

I hope you find this useful! If you like it, pls do subscribe my YouTube channel for automation or tech updates.

No comments:

Post a Comment