我正在使用RSpec和水豚,並且在嘗試根據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"> </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"> </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只是「測試」,無論我使用text,textContext還是實際更改字符串,此行始終處於選中狀態,並且在此行中按下的選項按鈕越多最終導致刪除錯誤的資產。
任何人都可以看到RSpec代碼或選擇行後面的邏輯的任何問題,我以爲assetName td中的文本將不得不匹配所查找的行,但這似乎並沒有發生。
有很多事情可以做,以改善測試和縮小錯誤。 XPath語法在這裏沒有任何好處,可能會導致錯誤,請使用像within(「.AssetAssetList.admin」)這樣的東西。 ':textContent'不是Capybara文檔AFAIK中的已知屬性。 ':text'選項可能會進行部分匹配,請嘗試完全重命名該行。 – 2012-03-15 18:11:21
嘿安德魯,感謝您的提示:) – Jay 2012-03-21 12:25:39