Skip to content

Instantly share code, notes, and snippets.

@tiagotex
Last active August 5, 2020 12:24
Show Gist options
  • Save tiagotex/94a053d6a8fa5274227d7ec1fe753005 to your computer and use it in GitHub Desktop.
Save tiagotex/94a053d6a8fa5274227d7ec1fe753005 to your computer and use it in GitHub Desktop.
Select item and get selected item from choises.js select with Capybara and Selenium
module ChoicesHelpers
def select_from_choices(item_text, options)
field = find_field(options[:from], visible: false)
within(find("##{field[:id]}", visible: false).ancestor(".choices")) do
find(".choices__inner").click
find("input").send_keys(item_text, :return)
end
end
def selected_from_choices(from)
field = find_field(from, visible: false)
within(find("##{field[:id]}", visible: false).ancestor(".choices")) do
find(".choices__list--single").text
end
end
end
RSpec.configure do |config|
config.include ChoicesHelpers, type: :system
end
### Usage
# Instead of:
# select "option text", from: "field name"
# Do:
# select_from_choices "option text", from: "field name"
# Instead of:
# expect(page).to have_select "field name", selected: "option text"
# Do:
# expect(selected_from_choices("field name")).to eq "option text"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment