Skip to content

Instantly share code, notes, and snippets.

@bensheldon
Created April 14, 2025 15:01
Show Gist options
  • Save bensheldon/fd7e63f764310f4d175883a98983162c to your computer and use it in GitHub Desktop.
Save bensheldon/fd7e63f764310f4d175883a98983162c to your computer and use it in GitHub Desktop.
# spec/support/capybara_check_choose.rb
# frozen_string_literal: true
module System
module CapybaraCheckChoose
# Speed up `allow_label_click`, which is the default, so that it
# does the label click immediately rather than waiting for the
# default strategy to fail
def _check_with_label(selector, checked, locator,
allow_label_click: session_options.automatic_label_click, **options)
if allow_label_click
options[:allow_self] = true if locator.nil?
synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do
el ||= find(selector, locator, **options, visible: :all)
attempts = 0
until el.checked? == checked
attempts += 1
break if attempts > 2
first(:label, for: el, visible: true).click(**(Hash.try_convert(allow_label_click) || {}))
end
end
else
super
end
end
end
end
module Capybara
module Node
module Actions
prepend System::CapybaraCheckChoose
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment