Rails : Capybara Webkit : Issue : Multiple JS events in single example

I recently moved from Selenium Web Driver to Thoughtbot’s Capybara Webkit as JS Driver for Capybara. The main reason is, webkit is super fast in running the test suite and its headless (no browser pops up ) .

Problem I faced

I was changing the option in select box and expecting the change in span’s content. Actually some sort of calculation should have been triggered. Webkit was not triggering the ‘change‘ event properly.

One of the Solutions

You need to manually trigger the ‘change’ event like

page.execute_script("$('[name=additional_child]').trigger('change');")

Example

it "Should show the total price of 1 normal and 2 additional \
    monthly pact as $#{monthly_cost + additional_monthly_cost*2}" do
  visit '/signup'

  # Additional purchase
  select 'Monthly', from: 'subscription_type'
  fill_in 'additional_child', with: '2'
  trigger_onchange_event(page)
  expect(page).to have_css('#grand_total',
                           text: "#{monthly_cost + additional_monthly_cost*2}")

end

def trigger_onchange_event(page)
  page.execute_script("$('[name=additional_child]').trigger('change');")
end
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s