Skip to content

Instantly share code, notes, and snippets.

@jtanium
Created September 20, 2011 17:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jtanium/1229684 to your computer and use it in GitHub Desktop.
Save jtanium/1229684 to your computer and use it in GitHub Desktop.
TokenInput Cucumber Helper
module TokenInputHelpers
def token_input(locator, options)
raise "Must pass a hash containing 'with'" unless options.is_a?(Hash) && options.has_key?(:with)
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' 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
wait_for_ajax
within(:css, ".token-input-dropdown") { find("li:contains('#{options[:with]}')").click } # find the matching element, and click on it
end
def wait_for_ajax
wait_until { page.evaluate_script('$.active') == 0 }
end
protected
def _find_fillable_field(locator)
find(:xpath, XPath::HTML.fillable_field(locator), :message => "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found")
end
end
World(TokenInputHelpers)
@jacklin10
Copy link

Agreed! This works like a charm! Thanks so much for sharing!

@artem-mindrov
Copy link

I've tried to make this work for inputs with multiple tokens. Seemed simple, but there's one weird gotcha. After the first token has been selected, Selenium can't fill in the token input field any longer, considering it invisible. I couldn't figure out what goes wrong, since the input is clearly visible both in the browser instance launched by the web driver and when testing manually. I had to work this around by triggering the subsequent searches with a script:

https://gist.github.com/artem-mindrov/5248796

Has anyone else encountered this? Any reasonable explanation or a better idea?

@dipil-saud
Copy link

Works great.
But, when you have more than one tokeninputs on the page, it fails on within(:css, ".token-input-dropdown") as there will be more than one ".token-input-dropdown" elements.
Replace with within(:css, ".token-input-dropdown:not(:empty)") to fix it.

@dipil-saud
Copy link

Works Great!!
But when you have more that one token inputs on the page, it fails on within(:css, ".token-input-dropdown") because there will be more than one ".token-input-dropdown" elements on the page.
Changing it to within(:css, ".token-input-dropdown:not(:empty)") will fix it.

@dipil-saud
Copy link

Works Great!!
But when you have more that one token inputs on the page, it fails on within(:css, ".token-input-dropdown") because there will be more than one ".token-input-dropdown" elements on the page.
A slight addition within(:css, ".token-input-dropdown:not(:empty)") will fix it

@ddonahue99
Copy link

I had some trouble with Capybara 2.1, so I forked it and tweaked things a bit to fix it: https://gist.github.com/ddonahue99/6116406

@victorhazbun
Copy link

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