2010-05-09 109 views
23

我有一个测试使用黄瓜,水豚和硒驱动程序。这个测试应该去表格并提交。正常的文本将黄瓜,水豚和硒 - 提交表单没有按钮

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" 

的问题是,我没有在形式 我需要一种方法做OK按钮,在“form.submit”,没有任何点击按钮或链接 - 当您在使用浏览器的表单字段中按ENTER时会发生同样的情况。

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

回答

31

您可以访问硒send_keys方法来调用返回事件像

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

请注意,至少对于我来说,作为一名司机,你必须使用':Enter' – PragTob 2016-02-03 10:05:24

3

简而言之:你不能。

某些浏览器将不允许您在没有提交按钮的情况下提交表单(最值得注意的是Internet Explorer < = 6)。所以这种形式首先是一个坏主意。添加一个提交按钮,并使用CSS将其定位在屏幕外。

+5

显示:没有人想到。它在那里,但它不是! – Robus 2010-05-09 15:29:03

+1

问题是我无法添加按钮,因为我正在测试一个网站,我无法更改源代码。 – 2010-05-09 21:25:21

+0

“display:none”为我工作。 – samullen 2011-10-04 20:22:46

1

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

这里是在Rails 3中支持“不引人注目”(强调引号)Javascript的JavaScript模拟。该行

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

可能是解决您的问题的线索。完整的代码是here

+0

这将适用于Selenium驱动程序? – 2010-05-15 16:31:27

+0

我认为它的确如此。我现在正在使用表单提交,确认对话框在应用程序中没有问题,并且它们正常工作。 – Chubas 2010-05-15 23:33:56

19

一个简单的办法:

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

为我工作与水豚,envjs。还应该与硒一起工作。

+0

也适用于Poltergeist。 – 2014-02-24 12:08:28

+0

还有另外一个类似的解决方案:[page.execute_script(“$('form#your-form')。submit()”)](http://stackoverflow.com/a/13712246/664833) - 还有,检查注释[Capybara :: Session#execute_script](http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session:execute_script)(它表示*'execute_script'应该用在'evaluate_script'之上可能*) - 这只是一个需要考虑的信息。 – user664833 2014-06-24 02:39:33

1

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

5

随着水豚硒驱动,你可以做这样的事情:

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 
1

随着Webrat你可以:

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

页。 307,RSpec的图书

9

我必须解决这个问题我自己。在webrat我有这样的事情:

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

我能够实现这个同样的事情在水豚:

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 
+2

做伎俩..应该是第一个回答 – 2013-01-05 05:11:56

+1

这是_awesome_,因为它甚至不需要javascript!所以它不应该依赖于驱动程序的实现(就像世界上其他所有的东西一样)。特别是,它根本不需要js。 – bchurchill 2013-05-13 13:49:42

0

这是一个有点hackish,但它满足了需求。我猴子补丁水豚支持元素的方法#submit

这是不健壮,因为它天真地创建从每一个输入元素的namevalue属性POST参数。 (在我的情况下,我所有的<input>元素都是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 
1

显示:none解决方案无法使用selenium驱动程序与水豚,因为硒抱怨与不可见元素相互作用。 如果你尝试,你最终可能会看到以下错误消息上面的解决方案:

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

应该是一个评论 – 2013-01-05 04:37:43

1

你可以尝试发送一个换行符:

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

试试这个..

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