Javascript to generate a random number in a text field

866 views
Skip to first unread message

Liquid Flux Testing

unread,
Apr 12, 2011, 4:23:07 PM4/12/11
to Selenium Users
Using Selenium IDE to generate a random number between 23 and 10,000
in a text field I used this code, can someone validate it's correct?

fieldName javascript{"MyText" + Math.floor(Math.random(23)*10000)}

I ran this once, and if I thought I remember it working correctly,
generating a random number, but every subsequent time I've ran the
test it has simply entered the word "field" into the text field,
instead of generating the number... Any idea why?

Pallasch M.

unread,
Apr 13, 2011, 8:47:05 AM4/13/11
to Selenium Users
I would use:

javascript{ do { number = Math.floor(Math.random()*10001); } while
(number < 23); fieldname_string = "MyText" + String(number); }

to create fieldName.

Works for me perfect.

Regards.

Pallasch M.

unread,
Apr 14, 2011, 8:16:43 AM4/14/11
to Selenium Users
Try this one:

javascript{ do { number = Math.floor(Math.random() *10000); } while
(number < 23); fieldname = "MyText" + String(number); }

Output would be like "MyText8365".

Liquid Flux Testing

unread,
Apr 13, 2011, 2:25:14 PM4/13/11
to Selenium Users
I finally got this script working by dropping some of the code I had
been using. The finalized code for anyone else interested in achieving
this random number generation turned out to be:
javascript{Math.floor(Math.random(x)*z)}
wherein x=the starting number, and z=the ending number.
Cheers!
Reply all
Reply to author
Forward
0 new messages