Ruby + watir-random instead of repeated Menu Elements

Source: Internet
Author: User
Tags shuffle

Test cases are similar to the Menu above, a total of 9

First look at the Element Definition (yaml ):

# Channel switching-channel_0_link: div (: class, 'navmenubg '). li (: id, 'num _ 2 '). link (: href, 'HTTP: // beijing.xxxx.com/xxxshi') channel_0_link_on: div (: class, 'navmenubg '). li (: id, 'num _ 2 '). span (: class, 'curcorner') # channel switching-entertainment channel_link: div (: class, 'navmenubg '). li (: id, 'num _ 4 '). link (: href, 'HTTP: // beijing.xxxx.com/xxxxian') channel_1_link_on: div (: class, 'navmenubg '). li (: id, 'num _ 4 '). span (: class, 'curcorner') # channel switch-Life service channel_2_link: div (: class, 'navmenubg '). li (: id, 'num _ 5 '). link (: href, 'HTTP: // beijing.xxxx.com/xxxxxhuo') channel_2_link_on: div (: class, 'navmenubg '). li (: id, 'num _ 5 '). span (: class, 'curcorner') # channel switch-product div (: class, 'navmenubg '). li (: id, 'num _ 6 '). channel_3_link: link (: index, 21) channel_3_link_on: span (: class, 'curcorner') # channel switching-hotel div (: class, 'navmenubg '). li (: id, 'num _ 7 '). channel_4_link: link (: index, 22) channel_4_link_on: span (: class, 'curcorner') # channel switching-travel div (: class, 'navmenubg '). li (: id, 'num _ 8 '). channel_5_link: link (: index, 23) channel_5_link_on: span (: class, 'curcorner ') # channel switching-Lottery channel_6_link: div (: class, 'navmenubg '). li (: id, 'num _ 9 '). link (: href, 'HTTP: // www.xxxx.com/xxxxjiang') channel_6_link_on: div (: class, 'navmenubg '). li (: id, 'num _ 9 '). span (: class, 'curcorner ') # channel switching-promotion channel_7_link: div (: class, 'navmenubg '). li (: id, 'num _ 10 '). link (: href, 'HTTP: // www.xxxx.com/xxxxxiao') channel_7_link_on: div (: class, 'navmenubg '). li (: id, 'num _ 10 '). span (: class, 'curcorner') # channel switching-previous Group Buying div (: class, 'navmenubg '). li (: id, 'num _ 12 '). channel_8_link: link (: index, 26) channel_8_link_on: span (: class, 'curcorner ')

Test Case: Use a loop to obtain nine menus at random. Each Menu must be clicked and verified.

  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

The script repeats nine times, and a random value is obtained each time. The random number rand () starts from 0, therefore, when the element is defined, the nine Menu elements are encoded from 0, for example, channel_0_link.

However, the rand () function contains duplicate values, that is, some Menu menus are clicked twice or multiple times, which is in contrast to our requirements. I almost searched the API and did not find any sequential or random method without repetition. In the next step, I decided to use another random method to solve the problem. In fact, there are two types of random playback: 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 be used to obtain nine Menu menus at random, each of which must be clicked and verified. The each method is to get data from the array. The shuffle method is to re-arrange the obtained values. This method is also used in the shuffling Program (there will be no duplicates ).

    a=[1,2,3,4,5,6,7,8,9]    a.shuffle.each{    |i|    b = i    puts b    }

You can try it.

Reference: http://blog.sina.com.cn/s/blog_6a55d9950100v4xu.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.