黄瓜、水豚和硒 - 提交不带按钮的表单

发布于 2024-08-31 11:26:25 字数 433 浏览 6 评论 0 原文

我使用 Cucumber、capybara 和 selenium 驱动程序进行了测试。该测试应填写表格并提交。正常的文本是

  Scenario: Fill form
    Given I am on the Form page
    When I fill in "field1" with "value1"
    And I fill in "field2" with "value2"
    And I press "OK"
    Then I should see "Form submited"

问题是我在表单中没有“确定”按钮 我需要一种方法来执行“form.submit”,而无需单击任何按钮或链接 - 与使用浏览器在表单字段中按 ENTER 时发生的情况相同。

我不知道如何告诉水豚提交表格。我该怎么做呢?

I have a test using Cucumber, capybara and selenium driver. This test should go to a form and submit it. The normal text would be

  Scenario: Fill form
    Given I am on the Form page
    When I fill in "field1" with "value1"
    And I fill in "field2" with "value2"
    And I press "OK"
    Then I should see "Form submited"

The problem is that I don't have the OK button in the form
I need a way to do the "form.submit", without clicking any button or link - the same as happens when you press ENTER when you are in a form field using the browser.

I don't know how to tell capybara to submit a form. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

葵雨 2024-09-07 11:26:25

您可以访问 selenium send_keys< /a> 调用返回事件的方法,例如

 find_field('field2').native.send_key(:enter)

You can access the selenium send_keys method to invoke a return event like

 find_field('field2').native.send_key(:enter)
甜味超标? 2024-09-07 11:26:25

一个简单的解决方案:

When /^I submit the form$/ do
  page.evaluate_script("document.forms[0].submit()")
end

使用 capybara-envjs 为我工作。也应该与硒一起使用。

A simple solution:

When /^I submit the form$/ do
  page.evaluate_script("document.forms[0].submit()")
end

Worked for me with capybara-envjs. Should work with selenium as well.

忘羡 2024-09-07 11:26:25

我只得自己解决这个问题。在 webrat 中我有这样的东西:

Then /^I submit the "([^\"]*)" form$/ do |form_id|
  submit_form form_id
end

我能够在 Capybara 中实现同样的事情:

  Then /^I submit the "([^\"]*)" form$/ do |form_id|
    element = find_by_id(form_id)
    Capybara::RackTest::Form.new(page.driver, element.native).submit :name => nil
  end

I just had to solve this problem myself. In webrat I had something like this:

Then /^I submit the "([^\"]*)" form$/ do |form_id|
  submit_form form_id
end

I was able to achieve the same thing with this in Capybara:

  Then /^I submit the "([^\"]*)" form$/ do |form_id|
    element = find_by_id(form_id)
    Capybara::RackTest::Form.new(page.driver, element.native).submit :name => nil
  end
淡淡绿茶香 2024-09-07 11:26:25

使用 capybara Selenium 驱动程序,您可以执行以下操作:

within(:xpath, "//form[@id='the_form']") do
  locate(:xpath, "//input[@name='the_input']").set(value)
  locate(:xpath, "//input[@name='the_input']").node.send_keys(:return)
end

With the capybara Selenium driver you can do something like this:

within(:xpath, "//form[@id='the_form']") do
  locate(:xpath, "//input[@name='the_input']").set(value)
  locate(:xpath, "//input[@name='the_input']").node.send_keys(:return)
end
撩发小公举 2024-09-07 11:26:25

简而言之:你不能。

某些浏览器根本不允许您在没有提交按钮的情况下提交表单(尤其是 Internet Explorer <= 6)。所以这种形式一开始就是个坏主意。添加一个提交按钮并使用 CSS 将其放置在屏幕之外。

Simply put: you can't.

Some browsers will not allow you to submit a form without a submit button at all (most notably Internet Explorer <= 6). So this kind of form is a bad idea to begin with. Add a submit button and position it off the screen with CSS.

酸甜透明夹心 2024-09-07 11:26:25

您可能会推出自己的步骤(例如,我使用链接“确定”提交表单),并自己模拟提交功能。

这里是 Rails 3 中删除的 javascript 模拟,以支持“unobtrusive”(强调引号)Javascript。这条线

Capybara::Driver::RackTest::Form.new(driver, js_form(self[:href], emulated_method)).submit(self)

可能是回答你的问题的线索。完整代码位于此处

You may probably roll your own step (And I submit the form with the link "Ok", for example), and emulate the submit functionality yourself.

Here it is the javascript emulation dropped in Rails 3 to support "unobtrusive" (emphasis on the quotes) Javascript. The line

Capybara::Driver::RackTest::Form.new(driver, js_form(self[:href], emulated_method)).submit(self)

is probably the clue to answer your problem. The full code is here

孤云独去闲 2024-09-07 11:26:25

我建议您添加一个提交按钮,然后使用 CSS 隐藏它。然后您可以测试表单提交,但仍然获得您想要的用户行为。

I'd recommend you add a submit button, then hide it with CSS. Then you can test the form submission, but still get the user behavior you want.

泪痕残 2024-09-07 11:26:25

使用 Webrat,您可以

When /^I submit the form$/ do
  submit_form "form_id"
end

: 307,RSpec 书

With Webrat you can just:

When /^I submit the form$/ do
  submit_form "form_id"
end

p. 307, The RSpec Book

只怪假的太真实 2024-09-07 11:26:25

display:none 解决方案不适用于使用 selenium 驱动程序的水豚,因为 selenium 抱怨与不可见元素交互。
如果您尝试上述解决方案,您最终可能会看到以下错误消息:

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotVisibleError)

The display:none solution does not work with capybara using selenium driver because selenium complains about interacting with invisible elements.
If you try the above solution you may end up seeing the following error message:

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotVisibleError)
人间☆小暴躁 2024-09-07 11:26:25

您可以尝试发送换行符:

find_field('field2').native.send_key("\n")

You can try sending a newline:

find_field('field2').native.send_key("\n")
递刀给你 2024-09-07 11:26:25

这有点hackish,但它满足了需求。我对 Capybara 进行了猴子修补,以支持元素上的 #submit 方法。

它并不健壮,因为它天真地从每个输入元素的 namevalue 属性创建 POST 参数。 (就我而言,我所有的 元素都是 hidden 类型,所以它工作正常)。

class Capybara::Node::Element
  # If self is a form element, submit the form by building a
  # parameters from all 'input' tags within this form.
  def submit
    raise "Can only submit form, not #{tag_name}" unless tag_name =~ /form/i

    method = self['method'].to_sym
    url = self['action']
    params = all(:css, 'input').reduce({}) do |acc, input|
      acc.store(input['name'], input['value'])
      acc
    end

    session.driver.submit(method, url, params)
  end
end

...

form = find('#my_form_with_no_submit_button')
form.submit

This is a bit hackish, but it filled a need. I monkey-patched Capybara to support a #submit method on elements.

It is not robust because it naively creates the POST parameters from every input elements's name and value attributes. (In my case, all my <input> elements were of type hidden, so it works fine).

class Capybara::Node::Element
  # If self is a form element, submit the form by building a
  # parameters from all 'input' tags within this form.
  def submit
    raise "Can only submit form, not #{tag_name}" unless tag_name =~ /form/i

    method = self['method'].to_sym
    url = self['action']
    params = all(:css, 'input').reduce({}) do |acc, input|
      acc.store(input['name'], input['value'])
      acc
    end

    session.driver.submit(method, url, params)
  end
end

...

form = find('#my_form_with_no_submit_button')
form.submit
画尸师 2024-09-07 11:26:25

试试这个..

find(:css, "input[name$='login']").native.send_keys :enter

Try this..

find(:css, "input[name$='login']").native.send_keys :enter
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文