2013-01-02 8 views

Odpowiedz

2

jego prosty można to zrobić za pomocą find_by_css lub za pomocą XPath jak postępować

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

Dzięki, skończyłem z następującym 'elem = page.find_by_id ('my_element_id'); elem.value.should eq Date.today.to_s' –

+1

Użyłem również ['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

po prostu to zrobić:

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

'visible: false' to kluczowa część tutaj. Dzięki! – LouieGeetoo

+0

To tylko po to, aby znaleźć element, ten post mówi o tym, jak go przetestować: http://stackoverflow.com/questions/15066884/how-to-get-the-hidden-element-value-in-capybara – Dean

4

Można także nakazać Capybara aby nie ignorować ukryte elementy globalnie w swojej spec_helper.rb lub odpowiednik. Domyślne zachowanie może być zmienione:

# 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) 

Przykłady zaczerpnięte z this article.

Powiązane problemy