Skip to content

Instantly share code, notes, and snippets.

@ddonahue99
Forked from jtanium/token_input_helpers.rb
Last active December 20, 2015 10:31
Show Gist options
  • Save ddonahue99/6116406 to your computer and use it in GitHub Desktop.
Save ddonahue99/6116406 to your computer and use it in GitHub Desktop.
jquery.tokeninput.js cucumber helper, updated with capybara 2.1 and theme support
module TokenInputHelpers
THEME = '' # set to 'facebook' or 'mac' if using themes
def token_input(locator, options)
raise "Must pass a hash containing 'with'" unless options.is_a?(Hash) && options.has_key?(:with)
theme = THEME.present? ? "-#{THEME}" : ""
field = _find_fillable_field(locator) # find the field that will ultimately be sent to the server, the one the user intends to fill in
# Delete the existing token, if present
begin
# This xpath is finds a <ul class='token-input-list'/> followed by a <input id="ID"/>
within(:xpath, "//ul[@class='token-input-list#{theme}' and following-sibling::input[@id='#{field[:id]}']]") do
find(:css, ".token-input-delete-token").click
end
rescue Capybara::ElementNotFound
# no-op
end
ti_field = _find_fillable_field("token-input-#{field[:id]}") # now find the token-input
ti_field.set(options[:with]) # 'type' in the value
within(:css, ".token-input-dropdown#{theme}") do
find(:xpath, "//li[contains(text(),'#{options[:with]}')]").click
end
end
protected
def _find_fillable_field(locator)
find(:xpath, "//input[@id='#{locator}']", visible: false)
end
end
World(TokenInputHelpers)
@markysharky70
Copy link

Just curious if this is working for you? I ask because I was just trying it out and on line 25, it always fails to find my item in the dropdown list even though I can see in the web browser that it has typed it in and the result it should be choosing does appear in the dropdown list.

Thanks,

Mark

@victorhazbun
Copy link

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