所以我有一张桌子看起来就像有书籍清单的那样,在第一栏中,每本书都有两个链接,即查看和删除。
我希望能够使用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
答案 0 :(得分: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)相关。
通过兄弟姐妹访问元素的最简单方法是:
parent
方法获取父元素(即tr)。例如,要获取“书名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
答案 1 :(得分: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