From d81038824839068e5cf2faecaa436bef74876032 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 20 Nov 2014 09:28:25 +1100 Subject: [PATCH] Add have_select2 capybara matcher --- spec/support/matchers/select2_matchers.rb | 42 +++++++++++++++++++++++ spec/support/request/web_helper.rb | 1 + 2 files changed, 43 insertions(+) create mode 100644 spec/support/matchers/select2_matchers.rb diff --git a/spec/support/matchers/select2_matchers.rb b/spec/support/matchers/select2_matchers.rb new file mode 100644 index 0000000000..af7a5b4aff --- /dev/null +++ b/spec/support/matchers/select2_matchers.rb @@ -0,0 +1,42 @@ +RSpec::Matchers.define :have_select2 do |id, options={}| + + # TODO: Implement other have_select options + # http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Matchers#has_select%3F-instance_method + # TODO: Instead of passing in id, use a more general locator + + match_for_should do |node| + @id, @options = id, options + + #id = find_label_by_text(locator) + from = "#s2id_#{id}" + + found_element = node.has_selector? from + + if !found_element + false + + else + if options.key? :with_options + find(from).click + + all_options_present = options[:with_options].all? do |option| + node.has_selector? "div.select2-drop-active ul.select2-results li", text: option + end + + find(from).click + + all_options_present + end + end + end + + failure_message_for_should do |actual| + message = "expected to find select2 ##{@id}" + message += " with #{@options.inspect}" if @options.any? + message + end + + match_for_should_not do |node| + raise "Not yet implemented" + end +end diff --git a/spec/support/request/web_helper.rb b/spec/support/request/web_helper.rb index 870cf248c4..a4f3b5fd00 100644 --- a/spec/support/request/web_helper.rb +++ b/spec/support/request/web_helper.rb @@ -129,6 +129,7 @@ module WebHelper targetted_select2(value, options) end + # Deprecated: Use have_select2 instead (spec/support/matchers/select2_matchers.rb) def have_select2_option(value, options) container = options[:dropdown_css] || ".select2-with-searchbox" page.execute_script %Q{$('#{options[:from]}').select2('open')}