2013-01-02 28 views
20

Tôi có biểu mẫu có trường ẩn chứa ngày hiện tại.Giá trị thử nghiệm Capybara của trường ẩn

Tôi đang cố gắng để tìm ra cách để viết một công cụ tìm Capybara tới:

  1. Kiểm tra lĩnh vực này là có
  2. Kiểm tra giá trị của lĩnh vực

Đây có phải là có thể với Capybara?

Trả lời

2

của nó đơn giản bạn có thể làm điều đó bằng cách sử dụng find_by_css hoặc bằng cách sử dụng xpath như sau

page.find_by_css('#foo .bar a') #foo is the id the foo has .bar class 
page.find('/table/tbody/tr[3]') #path of the element we want to find 
+6

Cảm ơn, tôi đã kết thúc với 'elem = page.find_by_id' ('my_element_id') sau đây; elem.value.should eq Date.today.to_s' –

+1

Tôi cũng đã sử dụng ['find_by_id'] (http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders:find_by_id):' assert_equal photo .updated_at.to_datetime.to_s, page.find_by_id ('hidden_field_photo_updated_at'). value.to_datetime.to_s' – user664833

41

chỉ làm điều này:

find("#id_of_hidden_input", :visible => false).value 
+6

'hiển thị: false' là phần chính ở đây. Cảm ơn! – LouieGeetoo

+0

Đây chỉ là để tìm các yếu tố, bài viết này nói về làm thế nào để kiểm tra chống lại nó: http://stackoverflow.com/questions/15066884/how-to-get-the-hidden-element-value-in-capybara – Dean

4

Bạn cũng có thể hướng dẫn Capybara để không bỏ qua yếu tố ẩn trên toàn cầu trong bạn spec_helper.rb hoặc tương đương. Hành vi mặc định có thể được ghi đè:

# default behavior for hidden elements 
# Capybara.ignore_hidden_elements = false 

# find all elements (hidden or visible) 
page.all(".articles .article[id='foo']") 

# find visible elements only (overwrite the standard behavior just for this query) 
page.all(".articles .article[id='foo']", :visible => true) 


# changing the default behavior (e.g. in your features/support/env.rb file) 
Capybara.ignore_hidden_elements = true 

# now the query just finds visible nodes by default 
page.all(".articles .article[id='foo']") 

# but you can change the default behaviour by passing the :visible option again 
page.all(".articles .article[id='foo']", :visible => false) 

Ví dụ lấy từ this article.

Các vấn đề liên quan