spec/support/basepack_helper.rb in basepack-0.1.0 vs spec/support/basepack_helper.rb in basepack-0.2.0

- old
+ new

@@ -30,34 +30,65 @@ within(select2_container) do have_selector(".select2-drop li", text: options[:selected]) end end - def select2(value, options = {}) + def get_select2_container(options = {}) #TODO: maybe inspire by: https://gist.github.com/onyxrev/6970632 which is more clear solution raise "Must pass a hash containing 'from' or 'xpath'" unless options.is_a?(Hash) and [:from, :xpath].any? { |k| options.has_key? k } if options.has_key? :xpath select2_container = first(:xpath, options[:xpath]) else + # find by label select_name = options[:from] select2_container = first("label", text: select_name).find(:xpath, '..').find(".select2-container") end + # return signe or multiple value if select2_container.has_selector?('.select2-choice') - select2_container.find(".select2-choice").click - else - select2_container.find(".select2-choices").click + select2_container.find(".select2-choice") + else + select2_container.find(".select2-choices") end + end + + def select2(value, options = {}) + #TODO: maybe inspire by: https://gist.github.com/onyxrev/6970632 which is more clear solution + select2_container = get_select2_container(options) + select2_container.click + #find(:css, "div[style*=block].select2-drop-active input[type=text].select2-input").set(value) select2_container.find(:css, "input[type=text].select2-input").set(value) find(:xpath, "//body").find(".select2-drop li", text: value).click end - def fill_in_select(id, options) + def add_select2(label, options) raise "Must pass a hash containing 'with'" if options[:with].nil? - select2(options[:with], {from: id}) + select2(options[:with], {from: label}) + end + + def remove_select2(label, options) + raise "Must pass a hash containing 'with'" if options[:with].nil? + within(get_select2_container(from: label)) do + # there is no identifier in options must find by name + all('.select2-search-choice').each do |item| + item.first('.select2-search-choice-close').click if item.has_content?(options[:with]) + end + end + sleep 0.2 + end + + def remove_all_select2(label) + within(get_select2_container(from: label)) do + # find all crosses links and click on each one + all('.select2-search-choice-close').each do |cross_link| + cross_link.click + end + end + # sleep to assure that all options are removed before next instructions + sleep 0.2 end def fill_in_datepicker(id, options) raise "Must pass a hash containing 'with'" if options[:with].nil? find(:css, "input##{id} + input").set(options[:with])