ruby on rails - Kuinka saada Capybara suorittamaan POISTA -pyynnön kurkkuominaisuudessa?

original title: "ruby on rails - How to make Capybara do a DELETE request in a Cucumber feature?"


I am using Cucumber and Capybara. I need to make an HTTP DELETE request. Previously the features used webrat, so a simple statement like

visit "/comment/" + comment_id, :delete

worked, but now I am using Capybara.

The way to do a GET request is simply:

get 'path'

And to do a post request: 'path'

But how can I simulate a DELETE request?

I found out that the driver Capybara is using is Capybara::RackTest::Driver, if that is any help.

I have also tried:

Capybara.current_session.driver.delete "/comments/" + comment_id

But that does not work.

Käytän kurkkua ja kapybaraa. Minun on tehtävä HTTP POISTA -pyyntö. Aikaisemmin ominaisuudet käyttivät webratia, joten yksinkertainen lausunto, kuten käy "/ comment /" + comment_id,: delet ...

Tämä on yhteenveto käännöksen jälkeen. Jos haluat tarkastella koko käännöstä, napsauta käännä-kuvaketta

Kaikki vastaukset
  • Translate
    page.driver.submit :delete, "/comments/#{comment_id}", {}

    Documentation at:

  • Translate

    In case you are using a driver that doesn't support custom HTTP requests (e.g. Capybara-webkit, see closed issue and current driver), you could just temporary switch to RackTest driver to submit your request.

    For example:

    # Submit an HTTP delete request, using rack_test driver
    def http_delete path
      current_driver = Capybara.current_driver
      Capybara.current_driver = :rack_test
      page.driver.submit :delete, path, {}
      Capybara.current_driver = current_driver

  • Translate

    Use delete works fine. No need to use the lower level submit method for this.

    PS: tested with capybara-webkit 1.6.0 and capybara 2.4.4

  • Translate

    If you need a solution that will retain session information (e.g., if you need your DELETE request to be issued as an authenticated user), and that will work with both the :rack_test driver and the :webkit driver (assuming you have jQuery available in your app), try something like this:

    def manual_http_request(method, path)
      if Capybara.current_driver == :rack_test
        page.driver.submit method, path, {}
        page.execute_script("$.ajax({url: '#{path}', data: {}, type: '#{method}'});")
        # Wait for the request to finish executing...
        Timeout.timeout(10) { loop until page.evaluate_script('').zero? }

    I suspect this will also work with the :selenium driver but have not tested it. Maybe someone else can chime in on that.

  • Translate

    This works in selenium. It's assuming you have a logout button/link on any page it is called on

    Instead of:

    visit '/logout'


    def logout_user
      logout_link = page.first('#my_css_selector') if logout_link
    #... elsewhere in a step