I have an HTML form:
<%= form_with(url: '#', method: 'GET') do |f| %>
<%= f.select :client_id,
options_for_select(
Client.accessible_by(current_ability)
.map{|c| [c.name, c.id]}, selected_client_id
),
{include_blank: defined?(include_blank) ? include_blank : false},
{
class: "form-control selectpicker border",
id: "client_id",
onchange: "this.form.submit()",
data: {'live-search': true, style: "btn-white"}
}
%>
<% end %>
I have a Capybara test:
def check_grand_total(expected_value)
visit current_path
click_link "Balance"
# this select has `onChange="this.form.submit()"` on it, so it should trigger a page load
# however, the Capybara test fails and the screenshot appears as if it never selected
# this option, even though the option is there, and I can manually click it myself
select "Some Client", from: "client_id", visible: :all
$stdin.gets # I threw in this line here to troubleshoot...
# when the test pauses here, it does indeed appear as though Capybara didn't select anything
# I tried adding this line here to manually trigger the change event
page.execute_script("$('#client_id').trigger('change')")
# this doesn't appear to do anything either.
actual_value = page.first('.grand-total').text
assert_equal(expected_value, actual_value)
end
From all of the research I’ve done, it should “just work” and Capybara should “just wait” until the content appears on the page.
What’s weird is, sometimes this code works, sometimes it doesn’t. I’m pretty much baffled at why it works some of the time and not all of the time.