Created
September 20, 2011 17:11
-
-
Save jtanium/1229684 to your computer and use it in GitHub Desktop.
TokenInput Cucumber Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.