Skip to content

Instantly share code, notes, and snippets.

@rickychilcott
Last active February 22, 2024 13:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickychilcott/caa06e2f1653f06cb3316875e6c60dc8 to your computer and use it in GitHub Desktop.
Save rickychilcott/caa06e2f1653f06cb3316875e6c60dc8 to your computer and use it in GitHub Desktop.
slim-select capybara helpers
## from @dinshaw in https://github.com/brianvoe/slim-select/pull/246
## Assumption is that you have a label who's parent also contains the select list you are choosing from.
def js_select(item_text, options)
container = find(:xpath, "//parent::*[label[text()='#{options[:from]}']]")
within "##{container[:id]}", visible: false do
find('.ss-arrow').click
input = find(".ss-search input").native
input.send_keys(item_text)
find('div.ss-list').click
end
end
# usage
js_select 'Item name', from: 'Label text'
## An alternative
module MultiSelectHelper
def select_from_slim_select(item_text, options)
from = options.fetch(:from)
if !from.include?("#")
label = find("label", text: from)
from = "##{label["for"]}"
end
select_field = find(from, visible: false, wait: 2)
slim_select_id = select_field["data-ssid"]
slim_select_container = find("div.#{slim_select_id}")
within(slim_select_container) do
find(".ss-arrow, .ss-add").click
sleep(0.5)
input = find(".ss-search input").native
input.send_keys(item_text)
find("div.ss-list").click
end
click_off
expect_ss_list_to_not_be_visible
end
private
def expect_ss_list_to_be_visible
expect(page).to have_css("div.ss-list")
end
def expect_ss_list_to_not_be_visible
expect(page).to_not have_css("div.ss-list")
end
def click_off
page.execute_script('document.querySelector("body").click();')
end
end
# usage
select_from_slim_select("Option 1", from: "Name of label")
select_from_slim_select("Option 1", from: "#id-of-label")
@rickychilcott
Copy link
Author

If you use https://github.com/brianvoe/slim-select and capybara, consider using one of these helpers.

Thanks to https://github.com/brianvoe and https://github.com/dinshaw!

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