使用:
Ruby: ruby 1.9.3dev (2011-09-23 revision 33323) [i686-linux]
Rails: 3.2.9
Capybara: 2.0.3
我有一個Rails應用程序,其中有點擊它應提交AJAX POST請求,並返回一個JS響應時的鏈接。
鏈接代碼:
link_to("Send Notification", notification_path(user_id: user_id), remote: true, method: :post)
的JS響應(的.js。HAML文件)應觸發鏈接存在頁面上的以下隱藏的div:
#notification_status(style='display:none')
js.haml文件內容:
:plain
var notificationStatusContainer = $('#notification_status');
notificationStatusContainer.val("#{@notification_status_msg}");
notificationStatusContainer.show();
我是在試探我發送通知並顯示 的場景向使用黃瓜的用戶發送通知狀態消息(黃瓜欄,內置 水豚支持)
我試圖測試有ID的元素:在我一步definition.For這個成功的響應notification_status是可見 我嘗試下面的語句:
page.find('#notification_status').should be_visible
page.should have_selector('#notification_status', visible: true)
page.should have_css('#notification_status', visible: true)
page.find('#notification_status', visible: true)
page.find(:css, 'div#notification_status', visible: true)
無論上述工作對我和我的失敗步上面所列的.out 5段 最後4失敗,以下錯誤:
'expected to find css "#notification_status" but there were no matches. Also found "", which matched the selector but not all filters. (Capybara::ExpectationNotMet)'
這是奇怪,因爲下面的語句是路過正確:
page.has_selector?('#notification_status')
事實上,我檢查使用
print page.html
這表明了
預期這
<div style='' id='notification_status'></div>
頁面的源代碼。
最後我發現這個鏈接capybara assert attributes of an element,它顯示瞭如何以原始方式檢查元素的屬性。
此外,我發現在水豚文件中可見?方法(http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#visible%3F-instance_method) 以下信息:
Not all drivers support CSS, so the result may be inaccurate.
因此,我得出的結論是元素的可見性測試時 不靠水豚的可見的結果嗎?使用CSS選擇器 和使用該解決方案時,方法鏈接建議capybara assert attributes of an element
我想出了以下內容:
module CustomMatchers
def should_be_visible(css_selector)
find(css_selector)['style'].should_not include('display:none', 'display: none')
end
end
World(CustomMatchers)
用法:
should_be_visible('#notification_status')
行的東西是什麼驅動程序是什麼?一旦XHR完成,預期的HTML是什麼? – 2014-10-15 09:08:45