0

在此处输入图像描述

所以我有一个看起来像这样的表格,其中有一个书籍列表,在第一列中,每本书都有两个链接,查看和删除。

我希望能够使用 Watir 找到具有指定书名的行,然后单击该书的查看按钮。

这是我到目前为止所拥有的

And /^click on View link of "(.*)" on the result set table$/ do |cell_name|
   cellButton("submit.view", cell_name, "")
end
#
#
And /^click on Delete link of "(.*)" on the result set table with no_wait$/ do |cell_name|
   cellButton("submit.delete", cell_name, "no_wait")
end
#
#
def cellButton(submit_type, s_name, s_wait_type)
   c_found = 0
      @browser.tables do |tbl|
         tbl.rows do |r|
            r.each do |c|
               if c.text.to_s.matches(s_name)
                   c_found += 1
                end
               break if c_found > 0
            end
            if c_found > 0
               if s_wait_type == "no_wait"
                  r.button(:name, submit_type).click_no_wait
               else
                  r.button(:name, submit_type).click
               end
            end
            break if c_found > 0
         end
      end
end

这是特定视图按钮的 html

<tr class="even">
     <td class="actionColumn">
         <span style="margin: 0px; padding: 0px; display: inline;">[</span>
         <input id="013c6e2c8187_885b_1bd1f6fc" name="submit.view" class="action_link" size="" value="View" type="button"  onclick="location.href='book_details.html'">
         <span style="margin: 0px; padding: 0px; display: inline;">]</span><br>                                                 
         <div>
             <span style="margin: 0px; padding: 0px; display: inline;">[</span>
             <input id="013c6e2c8187_1194_75ae28a8" name="submit.delete" class="action_link" size="" value="Delete" type="button">
             <span style="margin: 0px; padding: 0px; display: inline;">]</span>                                                  
         </div>
     </td>
     <td>
        book title
     <td>
     <td>
        more tds
     </td>
 </tr>

脚本运行时没有错误,但是没有按下视图链接。

我正在使用 Watir 4.0、Ruby 1.9.3 和 Cucumber 1.3.3

4

2 回答 2

2

假设您的表格 html 是:

<table>
    <tr>
        <td>
            <input id="013c6e2c8187_885b_1bd1f6fc" name="submit.view" class="action_link" size value="View" type="button" onclick="location.href='book_details.html'"> 
        </td>
        <td>
            book title 1
        </td>
    </tr>
    <tr>
        <td>
            <input id="013c6e2c8122_885b_1bd1f6fc" name="submit.view" class="action_link" size value="View" type="button" onclick="location.href='book_details2.html'"> 
        </td>
        <td>
            book title 2
        </td>
    </tr>
</table>

然后你可以看到书名和查看按钮是由它们的父行(tr)相关的。

通过同级元素访问元素的最简单方法是:

  1. 找到具有唯一属性的元素;在这种情况下,带有书名的 td。
  2. 使用方法获取父元素(即tr)parent
  3. 获取与父元素相关的所需元素(即行中的第一个视图按钮)。

例如,要获取“书名 2”的查看按钮,您可以执行以下操作:

book_title = 'book title 2'
table = browser.table(:id => 'my_table')
book_row = table.td(:text => book_title).parent
book_row.button(:value => 'View').click

上述解决方案将在任何列中查找书名。如果只希望书名文本在一列中,这很好。如果文本可能在另一列中,您将需要执行以下操作:

book_title = 'book title 2'
table = browser.table(:id => 'my_table')
book_row = table.trs.find{ |tr| tr.td(:index => 1).text == book_title }
book_row.button(:value => 'View').click
于 2013-07-11T02:31:20.583 回答
1

可能有一种更清洁的方法,但我会试一试。

假设这个简化的 HTML(即带有链接而不是按钮的 2 列表):

<table id="foo">
  <tr><td><a href="http://www.example.com">View</a></td><td>Title One</td></tr>
  <tr><td><a href="http://www.iana.org/domains/reserved">View</a></td><td>Title Two</td></tr>
  <tr><td><a href="http://www.iana.org/numbers">View</a></td><td>Title Three</td></tr>
</table>

此代码应通过定位书名来工作:

title = "Title Three"

trs = b.table(:id => "foo").rows
row_index = 0
trs.each do |tr|
  tr.each do |cell|
    if cell.text == title
      b.link(:text => "View", :index => "#{row_index}").click
      # alternately, using the fire_event method
      # b.link(:text => "View", :index => "#{row_index}").fire_event("onclick")
    end    
  end
  row_index += 1  
end
于 2013-07-11T00:28:51.397 回答