Selenium IDE - Generating random number using javascript with example

Sometimes you need random numbers in your regression test case of software web application. Let me give you examples.
1. searching records with record id on web application
2. Creating and accessing URLs for random record ids.

In this kind of conditions, you need to generate random numbers to feed data.Selenium IDE has not any built
in command or function to generate random number and you should have Advanced Selenium IDE software knowledge to generate it. We can do it easily with javascript. You can read my post about Generating Random Alpha Numeric Or Numeric String in selenium IDE. I have created example script for random number as bellow.

New Test
storejavascript{Math.floor(Math.random() * 1000)}i
storejavascript{"MyUID" + Math.floor(Math.random() * 100) + "";}j

In above example, 'javascript{Math.floor(Math.random() * 1000)}' will return any random number in between 0 to 1000. You can set any maximum limit for random number generation. Here we have set 1000 so it will generate random number in between 0 to 1000. Store command will store that random number in to variable 'i' and you can use it anywhere in your script. You can directly use javascript with any other commands like "type", "click" etc.

Sameway, 'javascript{"MyUID" + Math.floor(Math.random()*100) + "";}' will return any random number in between 0 to 100 but starting with 'MyUID' and ending with '' text. e.g. '', '' etc and store it in variable 'j'.

No comments:

Post a Comment