ruby+watir–隨機而不重複擷取Menu菜單的元素

來源:互聯網
上載者:User

測試案例是類似上面的Menu菜單,共9個

先看看元素定義(yaml):

#頻道切換-美食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_1_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_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')#頻道切換-商品div(:class,'navMenuBg').li(:id,'num_6').channel_3_link: link(:index,21)channel_3_link_on: span(:class,'curCorner')#頻道切換-酒店div(:class,'navMenuBg').li(:id,'num_7').channel_4_link: link(:index,22)channel_4_link_on: span(:class,'curCorner')#頻道切換-旅遊div(:class,'navMenuBg').li(:id,'num_8').channel_5_link: link(:index,23)channel_5_link_on: span(:class,'curCorner')#頻道切換-抽獎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_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')#頻道切換-往期團購div(:class,'navMenuBg').li(:id,'num_12').channel_8_link: link(:index,26)channel_8_link_on: span(:class,'curCorner')

測試案例:使用迴圈,隨機擷取9個Menu菜單,每個都必須點擊到,並驗證

  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

指令碼中迴圈9次,每次都取一個隨機值,隨機數rand()是從0開始,所以我在元素定義時從0開始對這9個Menu菜單的元素進行編碼,如:channel_0_link。

但是這裡有個問題rand()函數中取的有重複值,即有些Menu菜單被點擊2次或者多次,這就與我們的要求相駁。我幾乎找遍了API,沒有找到按順序或者隨機而不重複的方法。下一步我決定使用另外一種隨機的方法來解決,其實全部隨機播放分為兩種,random和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

以上的代碼,可以實現隨機擷取9個Menu菜單,每個都必須點擊到,並驗證的需求。each的方法是從數組中擷取資料;shuffle的方法是對擷取的值進行重新排列,在洗牌程式中也是使用這種方法來做的(不會產生重複)。

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

大家可以去試試

參考:http://blog.sina.com.cn/s/blog_6a55d9950100v4xu.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.