根据文本字符串查找特定的表行

时间:2012-03-13 13:53:08

标签: ruby-on-rails rspec find capybara rspec2

我正在使用RSpec和Capybara,并且在尝试根据:textContent或:text属性选择特定行时遇到问题,但无论在测试中输入的字符串是否始终选择第一行。

HTML代码如下:

<table class="LearningAssetList admin" data-id="1">
  <tbody>
    <tr class="CategoryHeader">
      <td class="expandCell" colspan="9">
        <span>Admin Pro / Scheduling</span>
      </td>
    </tr>
    <tr class="headerRow ui-droppable">
      <td class="blank"></td>
      <td></td>
      <td>Name</td>
      <td>Description</td>
      <td class="center">Length</td>
      <td class="center">User Rating</td>
      <td style="width:20px;padding:0px;"></td>
      <td style="width:20px;padding:0px;"></td>
    </tr>
    <tr class="assetRow ui-draggable ui-droppable" data-id="49">
      <td class="blank">&nbsp;</td>
      <td class="assetPlay icon">
      <td class="assetName">
        <a onclick="openModal('http://www.youtube.com/v/C0DPdy98e4c','Learning Asset    
Test Upload')" href="#">Learning Asset Test Upload</a>
      </td>
      <td class="assetDescription">
      <td class="assetDuration">
      <td class="assetRating icon">
      <td class="assetFunctions center">
      <td class="assetDrag center">
      <td class="blank">&nbsp;</td>
    </tr>
  </tbody>
</table>

我的RSpec代码如下:

it "should allow asset to be deleted by Admins" do
visit 'http://localhost:3000/'  
click_link 'Admin'  
within(:xpath, '//*[@class="LearningAssetList admin"]') do
  #row = find('tr>td.assetName>a', :textContent => "Learning Asset Test Upload")
  row = find('tr>td.assetName>a', :textContent => "Learning Asset Test Upload".to_s)
  within(row) do
    find(:xpath, '//*[@class="popupMenu"]').click       
  end   
  sleep 5
  find(:xpath, '//*[@class="delete"]').click

  popup = page.driver.browser.switch_to.alert
  popup.text.should eq('Are you sure you would like to delete this asset?')
  popup.accept        
  assetList = find(:xpath, '//*[@class="LearningAssetList admin"]')
  assetList.should have_content('Learning Asset Test Upload')
  sleep 5     
end

我在此条目上方的表中有另一行,其中assetName只是“Test”,无论我是否使用text,textContext,或者确实更改了字符串,此行总是被选中,并且在此处按下更多选项按钮随后最终删除错误资产的行。

任何人都可以看到RSpec代码的任何问题或选择行背后的逻辑,我原以为assetName td中的文本必须与要找到的行匹配,但这似乎不会发生。< / p>

0 个答案:

没有答案