New Discussion

Notifications

You’re not receiving notifications from this thread.

Test some simple javascript with Minitest and Capybara

1
Testing

I'm seeking assistance on testing JavaScript that executes on the response page after a POST request.

So I have something like this

class YourControllerTest < ActionDispatch::IntegrationTest
  test 'submitting a POST request' do
    post '/your_controller/your_action', params: { your_param: 'some_value' }

    # Check if the response was a redirect (status code 202)
    assert_response :success

    # Now you can assert against the content of the redirected page
    assert_select 'h1', text: 'Expected Header'
  end
end

Which works, but the page object only contains an empty HTML page (<html><head></head><body></body></html>).

And I should be able to interact with the `page?, according to this https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends

I've experimented with both Selenium and the JavaScript driver for Capybara. In my test, there's plain JavaScript that alters the context of an HTML block, and I want to verify its functionality with a Minitest.

Can someone guide me on how to approach this effectively?

To test JavaScript that runs after a POST request using Minitest and Capybara, you need to use Capybara's system tests or feature tests with a JavaScript-enabled driver like Selenium. IntegrationTest alone won’t execute JS. Try writing a Capybara::DSL test with js: true option and use visit or click_button to trigger actions. This way, Capybara can wait for JS changes and let you interact with the updated page.

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.