Module: Spree::TestingSupport::CapybaraExt
- Defined in:
- lib/spree/testing_support/capybara_ext.rb
Instance Method Summary collapse
- #click_icon(type) ⇒ Object
- #column_text(num) ⇒ Object
- #eventually_fill_in(field, options = {}) ⇒ Object
- #fill_in_quantity(table_column, selector, quantity) ⇒ Object
- #fill_in_with_force(locator, with:) ⇒ Object
- #find_label_by_text(text) ⇒ Object
- #select2(value, options) ⇒ Object
- #select2_no_label(value, options = {}) ⇒ Object
- #select2_search(value, options) ⇒ Object
- #select2_search_without_selection(value, options) ⇒ Object
- #select_select2_result(value) ⇒ Object
- #targetted_select2(value, options) ⇒ Object
- #targetted_select2_search(value, options) ⇒ Object
- #wait_for_ajax ⇒ Object
-
#within_entire_page(&block) ⇒ Object
Executes the given block within the context of the entire capybara document.
- #within_row(num, &block) ⇒ Object
Instance Method Details
#click_icon(type) ⇒ Object
6 7 8 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 6 def click_icon(type) find(".fa-#{type}").click end |
#column_text(num) ⇒ Object
34 35 36 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 34 def column_text(num) find("td:nth-of-type(#{num})").text end |
#eventually_fill_in(field, options = {}) ⇒ Object
10 11 12 13 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 10 def eventually_fill_in(field, = {}) expect(page).to have_css('#' + field) fill_in field, end |
#fill_in_quantity(table_column, selector, quantity) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 38 def fill_in_quantity(table_column, selector, quantity) Spree::Deprecation.warn <<-WARN.strip_heredoc fill_in_quantity is deprecated. Instead use: within(#{table_column.inspect}) do fill_in #{selector.inspect}, with: #{quantity.inspect} end WARN within(table_column) do fill_in selector, with: quantity end end |
#fill_in_with_force(locator, with:) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 15 def fill_in_with_force(locator, with:) if Capybara.current_driver == Capybara.javascript_driver field_id = find_field(locator)[:id] page.execute_script <<-JS var field = document.getElementById('#{field_id}'); field.value = '#{with}'; var event = new Event('change', { bubbles: true }); field.dispatchEvent(event); JS else fill_in locator, with: with end end |
#find_label_by_text(text) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 117 def find_label_by_text(text) # This used to find the label by it's text using an xpath query, so we use # a case insensitive search to avoid breakage with existing usage. # We need to select labels which are not .select2-offscreen, as select2 # makes a duplicate label with the same text, and we want to be sure to # find the original. find('label:not(.select2-offscreen)', text: /#{Regexp.escape(text)}/i, match: :one) end |
#select2(value, options) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 83 def select2(value, ) label = find_label_by_text([:from]) within label.first(:xpath, ".//..") do [:from] = "##{find('.select2-container')['id']}" end targetted_select2(value, ) end |
#select2_no_label(value, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 92 def select2_no_label(value, = {}) raise "Must pass a hash containing 'from'" if !.is_a?(Hash) || !.key?(:from) placeholder = [:from] click_link placeholder select_select2_result(value) end |
#select2_search(value, options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 50 def select2_search(value, ) = { search: value, # by default search for the value select: true }.merge() label = find_label_by_text([:from]) within label.first(:xpath, ".//..") do [:from] = "##{find('.select2-container')['id']}" end select2_search_without_selection([:search], from: [:from]) select_select2_result(value) if [:select] end |
#select2_search_without_selection(value, options) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 63 def select2_search_without_selection(value, ) find("#{[:from]}:not(.select2-container-disabled):not(.select2-offscreen)").click within_entire_page do find("input.select2-input.select2-focused").set(value) end end |
#select_select2_result(value) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 108 def select_select2_result(value) # results are in a div appended to the end of the document within_entire_page do expect(page).to have_selector('.select2-result-label', visible: true) find("div.select2-result-label", text: /#{Regexp.escape(value)}/i, match: :prefer_exact).click expect(page).not_to have_selector('.select2-result-label') end end |
#targetted_select2(value, options) ⇒ Object
102 103 104 105 106 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 102 def targetted_select2(value, ) # find select2 element and click it find([:from]).find('a').click select_select2_result(value) end |
#targetted_select2_search(value, options) ⇒ Object
71 72 73 74 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 71 def targetted_select2_search(value, ) select2_search_without_selection(value, from: [:from]) select_select2_result(value) end |
#wait_for_ajax ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 126 def wait_for_ajax Spree::Deprecation.warn <<-WARN.squish, caller wait_for_ajax has been deprecated. Please refer to the capybara documentation on how to properly wait for asyncronous behavior: https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends WARN counter = 0 while page.evaluate_script("typeof($) === 'undefined' || $.active > 0") counter += 1 sleep(0.1) raise "AJAX request took longer than 5 seconds." if counter >= 50 end end |
#within_entire_page(&block) ⇒ Object
Executes the given block within the context of the entire capybara document. Can be used to ‘escape’ from within the context of another within block.
79 80 81 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 79 def within_entire_page(&block) within(:xpath, '//body', &block) end |
#within_row(num, &block) ⇒ Object
30 31 32 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 30 def within_row(num, &block) within("table.index tbody tr:nth-of-type(#{num})", &block) end |