Capybara - 提交没有按钮的表格

时间:2022-11-24 10:23:47

I am trying to submit a form without button using just Capybara and Rspec (no Cucumber or Selenium, I know there is already a question about that).

我试图提交一个没有按钮的表单只使用Capybara和Rspec(没有Cucumber或Selenium,我知道已经存在一个问题)。

I've seen there is a gist to add a method to submit a form without button:

我已经看到有一个要点是添加一个方法来提交没有按钮的表单:

module SubmitRackTestFormWithoutButton
  def submit_form!
    Capybara::RackTest::Form.new(driver, form).submit({})
  end
end
Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton

https://gist.github.com/989533, but I've not gotten it to work and I left a comment on it:

https://gist.github.com/989533,但我没有让它工作,我对它发表了评论:

I get undefined method `submit_form!' for #Capybara::Node::Element:... actually by "Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton" the method submit_form! is added to the Node (not to the Element), but find return an Element

我得到未定义的方法`submit_form!' for #Capybara :: Node :: Element:...实际上是通过“Capybara :: RackTest :: Node.send:include,SubmitRackTestFormWithoutButton”方法submit_form!添加到节点(而不是元素),但找到返回元素

Do you have some idea to work out that gist, or some other solution to submit a form without button ?

您是否有一些想法可以解决这个问题,或者其他解决方案来提交没有按钮的表单?

Thanks

谢谢

6 个解决方案

#1


5  

Although it's possible to achieve what you want using capybara, the easier and more practical solution is to put a submit button on the form.

虽然使用水豚可以达到你想要的效果,但更简单,更实用的解决方案是在表单上放置一个提交按钮。

There is no reason to not have a button on the form, it's bad accessibility to not have a form and users that do not have a GUI or are using screen readers will have trouble submitting the form otherwise.

没有理由在表单上没有按钮,没有表单是不可访问的,没有GUI或使用屏幕阅读器的用户将无法提交表单。

If you don't want the form button to be visible, can I suggest using some CSS to make it hidden:

如果您不希望表单按钮可见,我是否可以建议使用一些CSS来隐藏它:

<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;">

#2


22  

All your production code should be testable, so if you add a code that is only used by the test than the test will make no sense...

您的所有生产代码都应该是可测试的,因此如果您添加的代码仅供测试使用而不是测试将毫无意义...

Try to do this instead:

尝试这样做:

page.execute_script("$('form#your-form').submit()")

#3


7  

Here's a simple solution that doesn't require capybary-webkit, qt, lmnop or whatever.

这是一个简单的解决方案,不需要capybary-webkit,qt,lmnop等等。

Does not require a submit button. People say you need it but whatever.

不需要提交按钮。人们说你需要它,但无论如何。

Just monkeypatch a class or two

只是monkeypatch一两个类

# /spec/support/capybara.rb
  class Capybara::Session
    def submit(element)
      Capybara::RackTest::Form.new(driver, element.native).submit({})
    end
  end

Then you can do something like

然后你可以做类似的事情

require 'support/capybara'

before do
  create :lead
  create :user, :different_email
end

it 'Searchable' do
  visit users_path
  page.should have_content 'Slicer'
  page.should have_content 'Dicer'

  fill_in 'Search', with: 'dice'

  form = find '#search-form' # find the form
  page.submit form           # use the new .submit method, pass form as the argument

  page.should have_content 'Dicer'
  page.should_not have_content 'Slicer'
end

It's kinda like jacob's answer on here but for his you have to define that in the middle of the test.

这有点像雅各布在这里的回答,但对于他来说,你必须在测试中定义它。

For this solution, you can define this in some file in the /support directory or at the beginning of that one spec, etc. It reduces the clutter in the test.

对于此解决方案,您可以在/ support目录中的某个文件中或在该规范的开头等处定义此项。它可以减少测试中的混乱。

#4


2  

I got this to work in capybara 1.1.2 with:

我让它在capybara 1.1.2中工作:

  form = page.find("form")
  class << form
    def submit!
      Capybara::RackTest::Form.new(driver, native).submit({})
    end
  end
  form.submit!

and it looks like a similar solution is described here: http://minimul.com/submitting-a-form-without-a-button-using-capybara.html

这看起来像是一个类似的解决方案:http://minimul.com/submitting-a-form-without-a-button-using-capybara.html

#5


2  

You can do this by pressing enter within the input

您可以通过在输入中按Enter键来完成此操作

find('form input').native.send_keys :enter

#6


1  

Now You should use click_on

现在你应该使用click_on

click_on 'Sign up'

#1


5  

Although it's possible to achieve what you want using capybara, the easier and more practical solution is to put a submit button on the form.

虽然使用水豚可以达到你想要的效果,但更简单,更实用的解决方案是在表单上放置一个提交按钮。

There is no reason to not have a button on the form, it's bad accessibility to not have a form and users that do not have a GUI or are using screen readers will have trouble submitting the form otherwise.

没有理由在表单上没有按钮,没有表单是不可访问的,没有GUI或使用屏幕阅读器的用户将无法提交表单。

If you don't want the form button to be visible, can I suggest using some CSS to make it hidden:

如果您不希望表单按钮可见,我是否可以建议使用一些CSS来隐藏它:

<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;">

#2


22  

All your production code should be testable, so if you add a code that is only used by the test than the test will make no sense...

您的所有生产代码都应该是可测试的,因此如果您添加的代码仅供测试使用而不是测试将毫无意义...

Try to do this instead:

尝试这样做:

page.execute_script("$('form#your-form').submit()")

#3


7  

Here's a simple solution that doesn't require capybary-webkit, qt, lmnop or whatever.

这是一个简单的解决方案,不需要capybary-webkit,qt,lmnop等等。

Does not require a submit button. People say you need it but whatever.

不需要提交按钮。人们说你需要它,但无论如何。

Just monkeypatch a class or two

只是monkeypatch一两个类

# /spec/support/capybara.rb
  class Capybara::Session
    def submit(element)
      Capybara::RackTest::Form.new(driver, element.native).submit({})
    end
  end

Then you can do something like

然后你可以做类似的事情

require 'support/capybara'

before do
  create :lead
  create :user, :different_email
end

it 'Searchable' do
  visit users_path
  page.should have_content 'Slicer'
  page.should have_content 'Dicer'

  fill_in 'Search', with: 'dice'

  form = find '#search-form' # find the form
  page.submit form           # use the new .submit method, pass form as the argument

  page.should have_content 'Dicer'
  page.should_not have_content 'Slicer'
end

It's kinda like jacob's answer on here but for his you have to define that in the middle of the test.

这有点像雅各布在这里的回答,但对于他来说,你必须在测试中定义它。

For this solution, you can define this in some file in the /support directory or at the beginning of that one spec, etc. It reduces the clutter in the test.

对于此解决方案,您可以在/ support目录中的某个文件中或在该规范的开头等处定义此项。它可以减少测试中的混乱。

#4


2  

I got this to work in capybara 1.1.2 with:

我让它在capybara 1.1.2中工作:

  form = page.find("form")
  class << form
    def submit!
      Capybara::RackTest::Form.new(driver, native).submit({})
    end
  end
  form.submit!

and it looks like a similar solution is described here: http://minimul.com/submitting-a-form-without-a-button-using-capybara.html

这看起来像是一个类似的解决方案:http://minimul.com/submitting-a-form-without-a-button-using-capybara.html

#5


2  

You can do this by pressing enter within the input

您可以通过在输入中按Enter键来完成此操作

find('form input').native.send_keys :enter

#6


1  

Now You should use click_on

现在你应该使用click_on

click_on 'Sign up'