-
-
Save thijsc/1391107 to your computer and use it in GitHub Desktop.
def select_from_chosen(item_text, options) | |
field = find_field(options[:from]) | |
option_value = page.evaluate_script("$(\"##{field[:id]} option:contains('#{item_text}')\").val()") | |
page.execute_script("$('##{field[:id]}').val('#{option_value}')") | |
end |
It only works for a single selection of chosen dropdown but not on grouped options chosen select
Hey guys,
The implementation of @flexybiz above seems to work fine, however if, for whatever reason, I run select_from_chosen
two or more times on the same field, this will select multiple options even though my chosen is configured to be a standard select, not a multiple one.
My workaround was creating deselect_from_chosen
.
So in case you guys need to deselect fields in your tests, here's how I did it:
def deselect_from_chosen(field)
field = find_field(field, visible: false)
page.execute_script("$('##{field[:id]}').val('').trigger('change').trigger('chosen:updated')")
end
Funny to find this gist again 4 years later with so much discussion :-). I just needed this again, this one based on the snippet by @corporealfunk works well in Selenium:
def select_from_chosen(item_text, options)
field = find_field(options[:from], :visible => false)
find("##{field[:id]}_chosen").click
find("##{field[:id]}_chosen ul.chosen-results li", :text => item_text).click
end
For deselecting a specific value :
def deselect_from_chosen(item_text, options)
field = find_field(options[:from], visible: false)
page.execute_script("$(\"##{field[:id]} option:contains('#{item_text}')\").removeAttr('selected')")
page.execute_script("$('##{field[:id]}').trigger('chosen:updated')")
end
Here's another deselect function that's more in-line with @thejsc's approach. I like this approach because it triggers the change
hook.
def deselect_from_chosen(item_text, options)
field = find_field(options[:from], visible: false)
find("##{field[:id]}_chosen ul.chosen-choices li.search-choice", :text => item_text).find("a.search-choice-close").click
end
For anyone whos interested in just selecting whichever option is first without having to know the text
def select_first_from_chosen(from)
field = find_field(from, visible: false)
find("##{field[:id]}_chosen").click
first("##{field[:id]}_chosen ul.chosen-results li").click
end
Use like this: select_first_from_chosen('post[name]')
To guarantee an exact match rather than a partial match aka - find 'OR' and not match 'MORE'
replace
the line
find("##{field[:id]}_chosen ul.chosen-results li", text: item_text).click
with
find("##{field[:id]}_chosen ul.chosen-results li", text: /\A#{Regexp.quote(item_text)}\z/).click
For those of you using rails who need to escape javascript when setting
option_value
the first time.in the
Module
/Class
where this method lives, and changeto
As an aside, if your select box is prepopulated, and you want to CHANGE the value, you must clear the value of the select box before calling
select_from_chosen
, or it will attempt to select both values.