如何用 Capybara 填充隐藏字段? [英] How to fill hidden field with Capybara?

查看:19
本文介绍了如何用 Capybara 填充隐藏字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现,当我想为文本字段、文本区域或密码字段设置值时,我可以在 fill_in something 中使用 id、name 或 label 作为 something,:与 =>some_value.但是,当我尝试将值设置为 <input type="hidden"> 字段时,这种方法失败了(我想这样做是因为这些通常是我单独测试的客户端脚本).我怎么能用 Capybara 设置这样一个隐藏的字段?可能吗?

I've already found that when I want to set value to text field, text area or password field, I can use id, name or label as something in fill_in something, :with => some_value. However such approach fails when I try to set value to <input type="hidden"> field (and I want to do it because those are normally filled client-side scripts which I test separately). How could I set such a hidden field with Capybara? Is it possible?

HTML:

<input id='offer_latitude' name='offer[latitude]' type='hidden'>
<input id='offer_longitude' name='offer[longitude]' type='hidden'>

规格:

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

给出:

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

推荐答案

您需要定位隐藏字段并设置其值.有几种方法,这可能是最简单的

You need to locate the hidden field and set its value. There are a couple ways, this is probably the simplest

find(:xpath, "//input[@id='my_hidden_field_id']").set "my value"

如果您在生产环境中执行 client_side 脚本,您可以只告诉水豚使用符合 javascript 的驱动程序运行它

If you're executing a client_side script in production, you could just tell capybara to run it with a javascript-compliant driver

page.execute_script("$('hidden_field_id').my_function()")

这篇关于如何用 Capybara 填充隐藏字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆