Cucumber, capybara and selenium - Submitting a form without a button

后端 未结 12 1867
挽巷
挽巷 2021-02-01 18:14

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
    Give         


        
相关标签:
12条回答
  • 2021-02-01 18:45

    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.

    0 讨论(0)
  • 2021-02-01 18:46

    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)
    
    0 讨论(0)
  • 2021-02-01 18:48

    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.

    0 讨论(0)
  • 2021-02-01 18:49

    You can try sending a newline:

    find_field('field2').native.send_key("\n")
    
    0 讨论(0)
  • 2021-02-01 18:51

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

     find_field('field2').native.send_key(:enter)
    
    0 讨论(0)
  • 2021-02-01 18:54

    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

    0 讨论(0)
提交回复
热议问题