John Bachir
John Bachir

Reputation: 22751

How can I simulate two fast clicks on a submit button in Capybara?

I have a bug in my app which occurs if a user clicks the submit button of a form very quickly twice in a row. I can reliably reproduce this on production and in my development environment.

Is it possible for me to reproduce this with Capybara/poltergeist/phantomjs?

find("#my-button").double_click, find("#my-button").click.click, execute_script("$('#register-button').click().click();"), and other variations with execute_script do not work.

Everything I tried only caused my server-side code to be invoked once, even though console.log logging showed that click was indeed invoked twice. So I suspect there's something fundamentally missing from the capabilities of capybara and poltergeist simulate this behavior.

Is there a way to somehow invoke phantomJS at a lower level and achieve this?

I did try increasing the concurrency of my web server, it didn't help.


Upvotes: 4

Views: 2467

Answers (2)

gwcodes
gwcodes

Reputation: 5690

Taking a slightly different approach here: what is the fix to this bug? Would it be easier to test the fix, rather than test that these actions can't be carried out?

For example, if the fix is to use Rails UJS disable-with on an <input type=submit>, then your test could be something like:

# replace `your_path` as required
visit your_path

# important: this stops the submission so the page doesn't change;
# but crucially doesn't stop UJS from doing its thing
execute_script("$('form').submit(function() { event.preventDefault() })")

# replace 'Submit form' and 'Processing...' text as appropriate
click_button('Submit form')
expect(page).to have_button('Processing...', disabled: true)

Upvotes: 2

Thomas Walpole
Thomas Walpole

Reputation: 49960

As you guessed - double_click is not the same as click twice. To have two clicks you can do

find("#my-button").click.click

Or, depending on your bug you may need to sleep a little between those two clicks

find("#my-button").tap do |b|
  b.click
  sleep 0.1
  b.click
end

Upvotes: 2

Related Questions