Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 当存在具有相同显示文本的其他链接时,使用Watir单击特定链接_Ruby_Cucumber_Watir_Automated Tests - Fatal编程技术网

Ruby 当存在具有相同显示文本的其他链接时,使用Watir单击特定链接

Ruby 当存在具有相同显示文本的其他链接时,使用Watir单击特定链接,ruby,cucumber,watir,automated-tests,Ruby,Cucumber,Watir,Automated Tests,因此,我有一个类似的表,其中有一个图书列表,在第一列中有两个链接,每本书的view和delete 我希望能够使用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

因此,我有一个类似的表,其中有一个图书列表,在第一列中有两个链接,每本书的view和delete

我希望能够使用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>

[
]
[ ] 书名 更多tds
运行脚本时没有错误,但是,未按下“查看”链接


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

可能有更干净的方法,但我会尝试一下

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


可能有更干净的方法,但我会试试看

假设这种简化的HTML(即带有链接而不是按钮的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>
上述解决方案将在任何列中查找图书标题。如果书名文本仅应在一列中,则这是很好的。如果文本可能位于另一列中,您需要执行以下操作:

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

假设您的表格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>
上述解决方案将在任何列中查找图书标题。如果书名文本仅应在一列中,则这是很好的。如果文本可能位于另一列中,您需要执行以下操作:

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)我认为
click\u no\u wait
方法已被弃用(如果我没有弄错的话);2)按钮上有一个
onclick
属性,这意味着您可能必须使用该方法。在给出您正在使用的html示例时,务必给出所有相关的html。给我们“链接”很好,因为我们可以看到它实际上是一个看起来像链接的按钮。但是,我们不知道表的其余部分是什么样子(即书名与按钮的关系)。我添加了更多html代码,我还没有机会查看您的答案。谢谢有几个观察结果:1)我认为
click\u no\u wait
方法已被弃用(如果我没有弄错的话);2)按钮上有一个
onclick
属性,这意味着您可能必须使用该方法。在给出您正在使用的html示例时,务必给出所有相关的html。给我们“链接”很好,因为我们可以看到它实际上是一个看起来像链接的按钮。但是,我们不知道表的其余部分是什么样子(即书名与按钮的关系)。我添加了更多html代码,我还没有机会查看您的答案。谢谢实际上,我无法按id搜索表,因为类似的表可以位于具有不同id的不同页面上,但我的脚本仍然需要使用该id。但那不是你的错,是我不清楚的错。最后,我只是将您的代码放入@browser.tables.each do|table循环中。并相应地修改您的代码。但我很惊讶,你竟然在4行中做到了我所需要的……维卢斯,我的很多行。谢谢你的帮助!实际上,我无法按id搜索表,因为类似的表可以位于具有不同id的不同页面上,但我的脚本仍然需要使用该id。但那不是你的错,是我不清楚的错。最后,我只是将您的代码放入@browser.tables.each do|table循环中。并相应地修改您的代码。但我很惊讶,你竟然在4行中做到了我所需要的……维卢斯,我的很多行。谢谢你的帮助!这比我所拥有的要干净得多,但贾斯汀的答案是代码更少,效率更高。谢谢你的贡献。我确实采纳了你关于不推荐的点击号的其他一些建议_wait@livelaughlove:这就是苏的美。每个人都有贡献,社区决定什么是最好的。很高兴我能帮上点忙。这比我的要干净得多,但贾斯汀的回答是代码更少,效率更高。谢谢你的贡献。我确实采纳了你关于不推荐的点击号的其他一些建议_wait@livelaughlove:这就是苏的美。每个人都有贡献,社区决定什么是最好的。很高兴我能帮上忙。
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