Home > Net >  Capybara will not click button for Stripe SCA authentication
Capybara will not click button for Stripe SCA authentication

Time:06-01

I cannot get Capybara to click on the SCA/3DS ‘Complete authentication’ button when running RSpec tests. Similar tests which do not trigger SCA pass just fine, and if I run VNC to view what Firefox is doing, the button is visible and I can click it myself in the browser.

My problem seems very similar to what’s discussed in the comments here, but the solutions do not work: I have tried changing the browser used, and flattening the iframe traversal.

Test code:

  scenario "SCA required" do
    create_payment_method(account, payment_method: "stripe", last_four: "1234")

    visit "/billing"
    click_on "Enter Card Payment"

    within "#main-content" do
      within_frame(find("iframe")) do # Stripe payment form is in an iframe.
        find("input#Field-numberInput").set("4000002760003184") # SCA-required test card.
        find("input#Field-expiryInput").set("1234")
        find("input#Field-cvcInput").set("123")
        find("input#Field-postalCodeInput").set("12345")
      end
    end
    find("button#submit").click

    # Stripe nests the popup in several layers of iframes.
    stripe_frame = find("body > div > iframe") # Popup is prepended to the body element.
    switch_to_frame(stripe_frame)
    challenge_frame = find("iframe#challengeFrame")
    switch_to_frame(challenge_frame)
    fullscreen_frame = find("iframe.FullscreenFrame")
    switch_to_frame(fullscreen_frame)

    click_on "Complete authentication"

    switch_to_frame(:top)

    expect(page).to have_content "ends in 3184"
  end

Is there some way to debug what Selenium is doing under the hood here? I don’t see any movement on the page when running click_on "Complete authentication", but if I click on the button myself in the Firefox instance being controlled by Selenium it does work.

Running click_on "Complete authentication" returns the element clicked, which appears to be the expected element when I drop into Pry and call .text.

CodePudding user response:

I have solved this by clicking on the button directly with JavaScript:

execute_script(%Q{ document.querySelector("button").click() })

However, this does not in any way explain why click_on is not working, and if anything makes it more strange that it is not. If anyone has a better solution or a way to dig into why Capybara/Selenium are failing then that would be welcome.

CodePudding user response:

Assuming no error is returned by the click_on call then I'm guessing the button is being clicked before it's ready to be clicked. You can test that by sleeping for a few seconds before calling 'click_on'/navigating through the frames. If that fixes it then you'd need to look at what changes on the button to indicate that the page has finished whatever work it's doing and the button is ready to be clicked.

  • Related