Last active
December 15, 2015 10:49
-
-
Save artem-mindrov/5248796 to your computer and use it in GitHub Desktop.
Helper for testing jquery tokeninput with Selenium & Capybara
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 fill_in_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) | |
ti = _find_fillable_field("token-input-#{field[:id]}").native | |
ti.clear | |
options[:with].split(", ").each do |item| | |
set_token_input(ti, item) | |
xpath = "//*[contains(@class, 'token-input-dropdown')]//li[contains(normalize-space(string()), '#{item}')]" | |
begin | |
find(:xpath, xpath).click | |
rescue Capybara::ElementNotFound # if nothing was found, try appending a semicolon to trigger tag creation, see controller code below | |
set_token_input(ti, ";") | |
find(:xpath, xpath).click | |
end | |
end | |
end | |
def set_token_input(input, text) | |
input.send_keys(text) | |
rescue | |
page.driver.browser.execute_script %Q{ | |
var msg = '#{text}'; | |
$('##{input[:id]}').val(msg); | |
var kp = jQuery.Event('keydown'); | |
kp.ctrlKey = false; | |
kp.which = msg.charCodeAt(0); | |
kp.keyCode = msg.charCodeAt(0); | |
$('##{input[:id]}').trigger(kp); | |
} | |
end | |
protected | |
def _find_fillable_field(locator) | |
find(:xpath, XPath::HTML.fillable_field(locator)) | |
end | |
end | |
World(RegistrationHelpers, InputHelpers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment