Anyone know how to generate random letter or number in watir?

341 views
Skip to first unread message

Dms

unread,
Jun 25, 2008, 10:28:37 AM6/25/08
to Watir General
I think it would be very useful for test where need to save different
login each time when run test.
If anyone know, share your ideas, please.
Thanks, Dmitry.

anthonype

unread,
Jun 25, 2008, 11:01:09 AM6/25/08
to Watir General
For random generation of letters and numbers the following method
should work for you.
Typically for logins, i would just timestamp the login name i.e.
"qatest_#{Time.now.strftime("%m%d%Y%H%M%S")}@foo.com"

#------------------------------------------------------------------------------
# Method: random_alphanumeric
# In: string size
# Return: get a random string that is num characters long
#------------------------------------------------------------------------------
def String.random_alphanumeric(size=16)
s = ""
size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i
< 36) ? 55 : 61 ))).chr }
return s
end

aidy lewis

unread,
Jun 25, 2008, 11:15:51 AM6/25/08
to watir-...@googlegroups.com
Hi,

If you need unique values, why don't you use the current date\time

require 'date'
date_and_time = Time.now.asctime

Aidy

Leigh Sullivan

unread,
Jun 25, 2008, 11:21:37 AM6/25/08
to watir-...@googlegroups.com
Ruby allows you to do lots with random numbers/letters

Rand(10)

will return a random number (like 1, 2)

rand() returns a random decimal (like 0.6891121414121)

rand(9999) returns random digits (like 3212)


pick a random item in an array

myarray = ['dog','cat','orange']

myarray[rand(myarray.length)]

output like 'cat'


google search yielded this nice for generating random lowercase letters

http://snippets.dzone.com/posts/show/4169

Array.new(6) { (rand(122-97) + 97).chr }.join

Output like "aydshg"


Hope that helps.

Leigh


No virus found in this incoming message.
Checked by AVG.
Version: 7.5.524 / Virus Database: 270.4.1/1514 - Release Date: 6/23/2008 7:17 AM


No virus found in this outgoing message.
Checked by AVG.
Version: 7.5.524 / Virus Database: 270.4.1/1514 - Release Date: 6/23/2008 7:17 AM

Lisa Crispin

unread,
Jun 25, 2008, 12:02:42 PM6/25/08
to watir-...@googlegroups.com
Hi Dmitry,
You've already got lots of answers, but if it would help, I could send you our modules that do random characters and random names.
-- Lisa
--
Lisa Crispin
Co-author with Janet Gregory, Agile Testing
http://www.agiletester.ca
http://lisa.crispin.home.att.net
http://lisacrispin.blogspot.com

Dms

unread,
Jun 26, 2008, 3:54:39 AM6/26/08
to Watir General
Thanks a lot to everyone. Array.new(6) { (rand(122-97) +
97).chr }.join that solution was great for me.

niartseoj

unread,
Jun 26, 2008, 9:40:03 AM6/26/08
to Watir General
for random test data i use the gem randomdata and faker both are good
for building random data.
Reply all
Reply to author
Forward
0 new messages