Technology Without Borders, to carry forward the selfless spirit of sharing
2013/03/24
ruby + watir - random rather than obtain duplicate the Menu menu of elements
Test case: using a loop, random to get nine Menu menu, each must click and verify:
def channel
@b.goto URL
channel = 0
while channel <= 8
times = rand(9).to_s
AutoTest("channel_#{times}_link").click
sleep 1
assert_true(AutoTest("channel_#{times}_link_on").exists?)
channel += 1
end
end
Cycle nine times in the script, to take a random value every time, random number rand () is 0, so I element defines 0 of these nine elements of the Menu menu coding, such as: channel_0_link.
But there is a problem rand () function take duplicate values, that some Menu menu click on 2 or more times, which our barge. I almost hunted API is not found without repeated sequentially or randomly. Next, I decided to use another random method to solve, in fact, random play is divided into two types, random and shuffle。
def channel_food
@b.goto URL
linkid=[0,1,2,3,4,5,6,7,8]
linkid.shuffle.each{
|i|
times = i
AutoTest("channel_#{times}_link").click
sleep 1
assert_true(AutoTest("channel_#{times}_link_on").exists?)
}
end
The above code, can achieve randomized to get nine Menu menu, each must click and verify demand. each method is to get the data from the array; shuffle method to obtain the value of the re-arranged to use this method to do the shuffle program (does not generate repeat).
a=[1,2,3,4,5,6,7,8,9]
a.shuffle.each{
|i|
b = i
puts b
}
订阅:
博文评论 (Atom)
没有评论:
发表评论