Watir - Chrome - 如何检索并非全部可见的所有“li”条目?

时间:2013-10-10 15:07:07

标签: google-chrome watir watir-webdriver

我正致力于基于Chrome的聊天/即时消息平台的测试自动化。

其中一个案例是获取所有联系信息。联系人分组放置,可以通过切换单击组名来查看或隐藏。

我试图发布图片但被拒绝说我没有足够的声望点:(

HTML源代码如下:


    <div id="connectionListItems">
      <ul class="connections" style="height: 2783px;">
        <li class="group collapsed" data-item-id="SUGAR" data-name="SUGAR" style="top: 0px;"> ... /li>
        <li class="group collapsed" data-item-id="Desk Connections" data-name="Desk Connections" style="top: 23px;"> ... /li>
        <li class="group expanded" data-item-id="Default" data-name="Default" style="top: 46px;">
          <span class="name"> Default <span class="count">(0/118)</span> </span>  ... /li>
        <li class="connection" data-item-id="Default~klxkqbs_tu-yahoo" style="top: 69px;">
          <span class="name">(ABN) James</span>  </li>
        <li class="connection" data-item-id="Default~klxkqbs_tq-yahoo" style="top: 92px;">
          <span class="name">(ABN) Justin</span> </li>
        <li class="connection" data-item-id="Default~klxkqbs_wm-yahoo" style="top: 115px;">
          <span class="name">(ABN) Matt</span>  </li>
       .......
     </ul>
   </div>

我的Watir脚本如下所示:


    def getContactCount(browser, outputfile)
      iframe_chat = browser.iframe(:src => "/tribe/connectionlist/index.html#appId/3")
      ul_conns = iframe_chat.div(:id => "connectionList").div(:id => "connectionListItems").ul(:class => "connections")
      numGroups = ul_conns.lis(:class => /group/).length
      outputfile.puts "- Contact count -- total groups: " + numGroups.to_s
      grpName = Array.new
      ul_conns.lis(:class => /group/).each { |li| grpName.push li.data_name }
      for igrp in 1..numGroups
         list_grp = ul_conns.li(:data_name => grpName[igrp-1])
         outputfile.puts "     Group " + list_grp.text
         list_grp.click
         outputfile.puts "     Number of contacts found: " + ul_conns.lis(:class => "connection").length.to_s
         ul_conns.lis(:class => "connection").each { |li| outputfile.puts "        " + li.span(:class => "name").text }
         list_grp.click
      end
    end

这是输出:


    - Contact count -- total groups: 3
     Group SUGAR (0/11)
     Number of contacts found: 11
        (GFI) Joachim
        (ICAP) Kissel
        (JB Drax) James
        (JB Drax) Simon
        (JPM) Jason
        (JPM) Jeb
        (JSG) Steven
        (Macq) NY SB Desk
        (NE) Chris
        (RJO) Babe
        (RJO) Croce
     Group Desk Connections (1/88)
     Number of contacts found: 55
        ICM_am
        (ABN) Alex
        (ABN) James
        (ABN) Justin
        (ADM) Marie
        (Alliance) Victor
        (Arfinco) Bruno
        (Arfinco) Nico
        .......
     Group Default (0/118)
     Number of contacts found: 54
        (ABN) James
        (ABN) Justin
        (ABN) Matt
        ......

问题是,对于“桌面连接”组和“默认”组,脚本只检索了部分联系人(88个中的55个,118个中的54个,分别是)。

实际上,当我在Chrome浏览器中查看HTML源代码时,其中只有部分内容也被列出。但是当我向下滚动框架时,会显示更多的框架(但顶部的框架变得不可见)。

有人可以看看并帮忙吗?如何获得所有“li”(class =“connection”)?

如果需要更多信息,请告诉我。

非常感谢。

1 个答案:

答案 0 :(得分:0)

我认为解决问题的关键是滚动框架。您可以使用以下方式滚动到框架的结尾(即最后一个元素):

browser.frame(:id => 'frame_id).elements.last.wd.location_once_scrolled_into_view

理论上,一旦你这样做,它将导致下一组元素加载(而其他元素消失)。

由于您一次只能看到(在DOM中)一页结果,您需要捕获结果,滚动到底部,等待列表重新加载然后捕获更多结果。

这大致是你需要做的事情(虽然我猜测没有测试页):

# Get the results of your first 'page'
results = iframe_chat.lis.collect(&:text)

# Scroll to the bottom to get more results
iframe_chat.elements.last.wd.location_once_scrolled_into_view

# Wait for the list to refresh 
# I am checking that the last li disappears.
# You may need to check for something to appear as well.
iframe_chat.li(:text => results.last).wait_while_present

# Get the results of your second 'page'
results = iframe_chat.lis.collect(&:text)