我已经发现,当我想设置值的文本字段,文本区域或密码字段,我可以用ID,名称或标记为something在fill_in something, :with => some_value.然而,当我尝试将值设置为<input type="hidden">字段时,这种方法失败了(我想这样做,因为那些通常是填充的客户端脚本,我单独测试).我怎么能用Capybara设置这样一个隐藏的场地?可能吗?
HTML:
<input id='offer_latitude' name='offer[latitude]' type='hidden'>
<input id='offer_longitude' name='offer[longitude]' type='hidden'>
Run Code Online (Sandbox Code Playgroud)
规格:
describe "posting new offer" do
it "should add new offer" do
visit '/offer/new'
fill_in 'offer[latitude]', :with => '11.11'
fill_in 'offer[longitude]', :with => '12.12'
click_on 'add'
end
end
Run Code Online (Sandbox Code Playgroud)
得到:
1) posting new offer should add new offer
Failure/Error: fill_in 'offer[latitude]', :with => '11.11'
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'offer[latitude]' found
Run Code Online (Sandbox Code Playgroud)
DVG*_*DVG 82
您需要找到隐藏字段并设置其值.有几种方法,这可能是最简单的方法
find(:xpath, "//input[@id='my_hidden_field_id']").set "my value"
Run Code Online (Sandbox Code Playgroud)
如果您正在生产中执行client_side脚本,您可以告诉capybara使用兼容javascript的驱动程序运行它
page.execute_script("$('hidden_field_id').my_function()")
Run Code Online (Sandbox Code Playgroud)
Lui*_*aca 52
有很多方法可以达到相同的效果.我最喜欢的是:
first('input#id.class', visible: false).set("your value")
Run Code Online (Sandbox Code Playgroud)
如果你使用poltergeist/phantomjs作为驱动程序并且jquery不适合你,那么总是有好的老式js:
page.execute_script("document.getElementById('#some-id').value = 'some-value'");
Run Code Online (Sandbox Code Playgroud)