How to Parameterize and Loop a Batch File

Tags: 

Here is an example of how to parameterize a Selenium RC batch file, and loop through launching whatever number of instances you specify to the command line:

SET TimeStamp=
FOR /f "tokens=1-4 delims=/ " %%i in ("%date%") do set datestr=%%l%%j%%k
FOR /f "tokens=1-4 delims=.: " %%i in ("%time%") do set timestr=%%i%%j%%k%%l
SET TimeStamp=%datestr%%timestr%
set a=%2
set b=49
set /a c=%a%+%b%
FOR /l %%t IN (50, 1, %c%) do (
 Start java -jar C:\Selenium\2\selenium-server-standalone-2.42.2.jar -port %1%%t -singleWindow -userExtensions C:\selenium\2\user-extensions.js -firefoxProfileTemplate "c:\selenium\2\ffprofiles\2rc" -htmlSuite "*chrome" "http://sv-selenium-t1/%4-%1%%t/" "Z:\selenium\2\environment\development\suites\suite-other-testingOnly.html" "Z:\selenium\2\environment\development\suites\projects\other\testingOnly\results\testResults-%computername%-%3-%%t-%TimeStamp%.html"
 timeout /t %3
 ECHO %%t
)
START c:\windows\DelAge32 "Z:\selenium\scripts\2\environment\development\suites\projects\pcmh\pcmhLCCRegression\results\*.*" 3

In the example above, I have created 4 arguements for this batch file:

  • Argument 1: Represents the starting port number. My default is usally 50
  • Argument 2: Represents the number of virtual users that should be launched (looping through the start server command)
  • Argument 3: Represents the the interval to wait between launching the next virtual user, expressed in seconds
  • Argument 4: Represents a reservation id value that is used in the startingURL parameter to point the instance to the correct reservation number page on our framework where additional runtime parameters can be scraped and used in the selenium script.

If this batch file was named test.bat, you would supply the following on the command line to have it start up 3 instances of the Selenium RC server, the first starting on port 5001, the second on 5002, and so on. Each instance would launch within 10 seconds of each other:

test.bat 50 3 10 12

The script takes the 2nd arguement (%2), and uses it as a parameter in a loop ... then uses the %%t count to set the port number dynamically as well. This batch file also generates a filename friendly date stamp, used in the result filename.

The following batch file (I named processScheduleFiles.bat) executes to convert scheduled****.txt files dropped from the browser (using a data export module in Drupal to produce a txt file that the browser can automatically set to download to a consistent target directory where this batch file picks it up from) to batch files, then moves them to a "schedule" directory and calls them with arguments supplied in the dropped batch file:

@echo off
setlocal enabledelayedexpansion
if exist C:\Selenium\downloads\sch****.txt (goto COPYFILES) else (goto NOFILES)

:COPYFILES
for /f %%i in ('DIR /b C:\Selenium\downloads\sch****.txt') do (
    echo %%i
    set fn=%%i
    set fn=!fn:~11,8!
    move %%i C:\Selenium\schedules\!fn!
    ren C:\Selenium\schedules\%%i *.bat
)
for /f %%i in ('DIR /b C:\Selenium\schedules\sch****.bat') do (
    call C:\Selenium\schedules\%%i
    timeout /t 3
    DEL C:\Selenium\schedules\%%i
)
echo complete
goto:eof

:NOFILES
echo There are no files to move
@echo off
cls