Another example Selenium RC DOS Batch file

Tags: 

Below is an example of how the test script DOS batch file is currently configured:

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%
START java -jar S:\selenium-server-standalone-2.31.0.jar -port 4445 -log S:\scripts\SVT\results\%TimeStamp%-selenium.log -browserSideLog -firefoxProfileTemplate "C:\ffprofiles\selenium" -htmlSuite "*chrome" "https://www.somedomain.org" "S:\scripts\SVT\suite-svt.html" "S:\scripts\SVT\results\%TimeStamp%-svtresults.html"
START c:\windows\DelAge32 "S:\scripts\SVT\results\*.*" 3

The first four lines setup the variables needed for creating a dynamic date value that will be used to create report folders and used in report file names (i.e., "S:\scripts\SVT\suite-svt.html" "S:\scripts\SVT\results\%TimeStamp%-svtresults.html"). Doing so allows us to not only save each version of the report file instead of over-writing it and only having the current version, but it conveniently tags the report name with the actual date so the files are easy to find and ordered in Windows by the date they were created.

Lines four and five are the most important for launching the script with the RC server. Below is a breakdown of each of the commands and parameters:

  • START java -jar S:\selenium-server-standalone-2.31.0.jar: This part uses the Windows command START to start the JAVA runtime environment and pass to it the JAR file that shold be executed ... which in this case is selenium-server-standalone-2.31.0.jar. This file is downloaded from http://www.seleniumhq.org/download.
  • -port 4445: This part tells the server which port to run under. What is important to note here, is that two instances cannot run under the same port number. If you want to run two instances concurrently, you will need to set one to port X, and the other to port Y.
  • -log S:\scripts\SVT\results\%TimeStamp%-selenium.log -browserSideLog: This part tells the selenium server to log both server and client side actions to a time stamped log file. To force Selenium to store the log file in a location other then where the script was run from, you MUST specific the full path. Setting a relative path will cause the application to NOT launch properly when started from a Windows Scheduled Task.
  • -firefoxProfileTemplate "C:\ffprofiles\selenium": This part tells the selenium server that you want to use a custom Firefox profile to launch the test (highly recommended). Instructions for setting up a custom profile can be found here: http://wtdev64-box20/book/selenium/setting-and-automation-friendly-firefox-profile
  • -htmlSuite: tells the server that the test file to run will be an HTML Selenese based test, not a test in some other programming language.
  • "*chrome" : This part tells the server the browser to be launched will be of the chrome kind.
  • "https://www.somedomain.org": This part tells the server what the baseurl for the test to execute will be.
  • "S:\scripts\SVT\suite-svt.html": This part is the actual path to the suite file to launch
  • "S:\scripts\SVT\results\%TimeStamp%-svtresults.html": This part tells the server where to write the result files

The last line of the batch file uses a freeware command line utility named Delage32 (from: http://www.horstmuc.de/wbat32.htm) that makes it easy to delete any report files in the scripts results directory that are older than X number of days (as specified on the command line). A copy of this application has been attached to this page as well as an htm file with instructions on its usage. To install, just unzip the attached file, and copy the delage32.exe file to your c:\Windows directory. Then call it as per the example on the last line above.

NOTE: There must not be any spaces or special characters in the file names, or folder paths for any path used in the batch file. While the path may work when manually used with RC, it will not work when called from the Windows Scheduled Tasks command line.

2 Comments

One idea to extend this batch

One idea to extend this batch file is to use the DOS %CD% command. It is used to print the "Current Directory" of where ever the script is currently running. Since Selenium can run using relative paths to the suite and test files, it is possible to use this command to dynamically generate the first part of the path where copies of the same file may exist in multiple directories by environment (like, development, test, production, etc.). As long as the suite file can be located in a reliable location, pathing could look like this:

Start java -jar Z:\selenium-server-standalone-2.43.1.jar -port 4451 -singleWindow -userExtensions Z:\user-extensions.js -firefoxProfileTemplate "z:\ffprofiles\seleniumAutomationProfile" -htmlSuite "*chrome" "http://www.google.com" "%CD%\suite-test.html" "%CD%\test\results\testResults-TimeStamp.html"

As long as the directory relationship between the suite file and the batch file remain relevent, or in the same directory, you could move this file around between environments without having the change the path after deployment. Ideal!

Another idea to extend the

Another idea to extend the batch file is to modify the report file filename to include the %computername% variable, like so:

Start java -jar Z:\selenium-server-standalone-2.43.1.jar -port 4451 -singleWindow -userExtensions Z:\user-extensions.js -firefoxProfileTemplate "z:\ffprofiles\seleniumAutomationProfile" -htmlSuite "*chrome" "http://www.google.com" "%CD%\suite-test.html" "%CD%\test\results\%computername%-testResults-TimeStamp.html"