Saturday, March 13, 2010

Javascript for creating random numbers in Selenese

I use these javascript snippets in Selenese to generate random numbers in a Selenium IDE script. This is helpful when a value has to be unique.

Here's an example using javascript to get the current timestamp and use that value for the variable ("codigo" in this example):

storeEval
new Date()
today

storeEval
storedVars['today'].getTime()
codigo

type
bsa.olbServiceId
${codigo}

echo
Codigo is ${codigo} for WOV Everything

Below, I needed to generate random usernames that were 6-20 characters with letters and numbers. I used Javascript to get the current timestamp, converted it to a string, then contatenated with 'usr':

storeEval
new Date()
today

storeEval
storedVars['today'].getTime()
numtime

storeEval
storedVars['numtime'].toString()
time

storeEval
['usr']+storedVars['time']
username

echo
${username} is the username.

More info on Javascript and Selenese is in the Selenium documentation.