teamcapybara / capybara

Acceptance test framework for web applications
http://teamcapybara.github.io/capybara/
MIT License
10.03k stars 1.45k forks source link

Capybara with Selenium-webdriver: button found but not clicked #2780

Open tgentry-bamboo opened 1 month ago

tgentry-bamboo commented 1 month ago

Meta

Capybara Version: 3.40.0 Driver Information (and browser if relevant): selenium-webdriver 4.10.0 and Chrome

Expected Behavior

"Continue" button is clicked sending a POST request to my Controller and advancing the setup flow.

Actual Behavior

"Continue" button is located, click is performed but no action is taken.

Steps to reproduce

require 'rails_helper'

RSpec.feature 'Validation Configuration', type: :feature do
  let!(:system_admin) { admin_user }

  # Working scenario
  scenario "create new DEA validation" do
    login system_admin
    visit new_system_admin_validation_configuration_path

    expect(page).to have_css('h1', text: 'Create New User Validation Configuration')
    check('validation_configuration_run_on_auto_approval', visible: false)

    # Select Physician from select2 optgroup element
    find(:xpath, "//optgroup[@label='All Healthcare Professional']//option[text()='Physician (MD, DO)']").select_option

    choose('validation_configuration[validation_type]', option: 'dea', visible: false)
    click_on 'Continue'
    expect(page).to have_text('Review the information below and make sure it is correct.')
    click_on 'Submit'
    expect(page).to have_text('A new user validation configuration has been successfully completed.')
    click_on 'Back to User Validation Configurations Page'
    expect(page).to have_css('h1', text: 'User Validation Configurations')
  end

  # Scenario in which "Continue" button is not working
  scenario "create new WEB SERVICE validation" do
    login system_admin
    visit system_admin_web_service_configurations_path

    expect(page).to have_css('h1', text: 'Web Service Configurations')

    click_on 'Add New Web Service'

    fill_in 'Name', with: 'Web Service Name'
    fill_in 'Base URL', with: 'example.com'
    select 'Rest API', from: 'API Type'
    fill_in 'Access Token URL', with: 'example.com'
    fill_in 'Client Key', with: 'anykey'
    fill_in 'Client Secret', with: 'anysecret'
    fill_in 'Scope', with: 'anyscope'

    click_on 'Continue'

    find(:xpath, '//*[@id="content"]/div[3]/div/div/div[3]/div/button[1]').click

    expect(page).to have_css('h1', text: 'User Validation Configurations')
  end
end

Capybara is configured to use its default options for selenium-webdriver. The line that is causing the issue is:

find(:xpath, '//*[@id="content"]/div[3]/div/div/div[3]/div/button[1]').click

I've also tried:

click_on 'Submit'

The button needing to be clicked is nothing out of the ordinary:

Screenshot 2024-10-09 at 12 02 12 PM Screenshot 2024-10-09 at 12 02 56 PM

Can anyone help me track down this issue? I'm willing to open a PR if it's necessary but I'm at a loss for what could be causing the behavior to begin with. Thank you!

tgentry-bamboo commented 1 month ago

In doing some research (mostly reading this closed Issue from a merged PR: https://github.com/teamcapybara/capybara/pull/2422) I now see the we can send keypresses directly to the current session with Capybara.current_session.send_keys("ENTER").

Is there a way to send the keyboard control code or whatever the browser equivalent is for the ENTER key so that I can submit the form? Thank you!

tgentry-bamboo commented 1 month ago

Scratch that last question, for anyone else finding this thread in the future, the way to send control codes directly to the underlying driver (Selenium for Chrome in this case: https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings#api-example) is to simply send the key as a symbol like so:

Capybara.current_session.send_keys :enter
mrosso10 commented 4 weeks ago

I'm having a similar issue when the button to be clicked is near the bottom of the viewport. (If the button is totally out of the view, capybara scrolls and clicks the button correctly, the issue ocurrs only when the button is found inside the viewport but it is near the bottom border).

So a workaround i'm using is to modify the views to change the position of those buttons a bit.

jdonadob commented 1 week ago

I'm having a similar issue when the button to be clicked is near the bottom of the viewport. (If the button is totally out of the view, capybara scrolls and clicks the button correctly, the issue ocurrs only when the button is found inside the viewport but it is near the bottom border).

So a workaround i'm using is to modify the views to change the position of those buttons a bit.

Download chromedriver latest version.