Skip to content

Instantly share code, notes, and snippets.

@jsugarman
Last active March 20, 2017 13:03
Show Gist options
  • Save jsugarman/3487e63d8013bf13b2e97e7252f2c9d5 to your computer and use it in GitHub Desktop.
Save jsugarman/3487e63d8013bf13b2e97e7252f2c9d5 to your computer and use it in GitHub Desktop.
module Capybara
module Node
module Actions
alias_method :old_check, :check
alias_method :old_uncheck, :uncheck
def check(locator, options = {})
Capybara.current_session.evaluate_script js_element_check(locator, true)
end
def uncheck(locator, options = {})
Capybara.current_session.evaluate_script js_element_check(locator, false)
end
private
def js_find_by_id locator
element = <<-SCRIPT.strip.gsub(/\s+/,' ')
element = (function () {
var element = document.getElementById(\"#{locator}\");
return element;
})()
SCRIPT
end
def js_element_check locator, switch = true
checkbox = js_find_by_id(locator)
<<-SCRIPT
(function () {
#{checkbox}.checked = #{switch}
})()
SCRIPT
end
end
end
end
@jsugarman
Copy link
Author

jsugarman commented Mar 20, 2017

The Gov uk element for builder uses pseudo elements and javascript to style the checkboxes. as a result the input checkbox fields cannot be checked with the standard check/uncheck helpers (using capybara 2.5.0 with poltergeist 1.14.0 at least).

This patches the aforementioned helpers to use javascript on the resulting dom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment