ruby-on-railsrspecphantomjscapybarapoltergeist

How to trigger keyup in capybara tests. I have tried all the standard methods


I have written the following rspec/capybara test:

scenario "admin can create patient invoices with discounted items", driver: :poltergeist do
    product = create(:product, practice_group: practice_group)
    patient = create(:patient)
    location = create(:location, practice_group: practice_group)

    practice_group

    login_as_admin(admin)

    visit "/invoices/new?patient_id=#{patient.id}"

    select location.name, :from => "invoice_location_id"
    add_product_to_invoice(product)
    add_discount_to_product(0.05)
    submit_invoice_form

    expect(Invoice.count).to eq(1)
    expect(Invoice.last.total_amount).to eq("£9.50")
end

def add_discount_to_product(discount)
    find(".discount-input").send_keys((discount * 100).to_s)
    page.execute_script("$('.discount-input').trigger('keyup')")
end

I have tried various different strategies for firing this key-up event but the jQuery .on listener just isn't firing during the test.

I am using poltergeist driver.

Any advice on how I can get this event to fire? It seems like capybara's .trigger doesn't work with poltergeist.


Solution

  • send_keys will send keyup events - It's more likely you actually have JS on your page that isn't runnable by Poltergeist since it doesn't support modern JS/CSS. Try running your test with selenium or one of the new direct direct to Chrome CDP drivers like apparition