Module: CapybaraExt
- Defined in:
- lib/spree/testing_support/capybara_ext.rb
Instance Method Summary collapse
- #accept_alert ⇒ Object
- #click_icon(type) ⇒ Object
- #column_text(num) ⇒ Object
- #dismiss_alert ⇒ Object
- #eventually_fill_in(field, options = {}) ⇒ Object
- #find_label(text) ⇒ Object
- #find_label_by_text(text) ⇒ Object
- #page! ⇒ Object
- #select2(value, options) ⇒ Object
- #select2_no_label(value, options = {}) ⇒ Object
- #select2_search(value, options) ⇒ Object
- #select_select2_result(value) ⇒ Object
- #set_select2_field(field, value) ⇒ Object
- #targetted_select2(value, options) ⇒ Object
- #targetted_select2_search(value, options) ⇒ Object
-
#wait_for_ajax(delay = Capybara.default_max_wait_time) ⇒ Object
arg delay in seconds.
-
#wait_for_condition(delay = Capybara.default_max_wait_time) ⇒ Object
“Intelligiently” wait on condition.
- #within_row(num, &block) ⇒ Object
Instance Method Details
#accept_alert ⇒ Object
127 128 129 130 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 127 def accept_alert page.evaluate_script('window.confirm = function() { return true; }') yield end |
#click_icon(type) ⇒ Object
6 7 8 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 6 def click_icon(type) find(".icon-#{type}").click end |
#column_text(num) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 23 def column_text(num) if RSpec.current_example.[:js] find("td:nth-child(#{num})").text else all('td')[num - 1].text end end |
#dismiss_alert ⇒ Object
132 133 134 135 136 137 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 132 def dismiss_alert page.evaluate_script('window.confirm = function() { return false; }') yield # Restore existing default page.evaluate_script('window.confirm = function() { return true; }') 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 |
#find_label(text) ⇒ Object
98 99 100 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 98 def find_label(text) first(:xpath, "//label[text()[contains(.,'#{text}')]]") end |
#find_label_by_text(text) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 82 def find_label_by_text(text) label = find_label(text) counter = 0 # Because JavaScript testing is prone to errors... while label.nil? && counter < 10 sleep(1) counter += 1 label = find_label(text) end raise "Could not find label by text #{text}" if label.nil? label end |
#page! ⇒ Object
2 3 4 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 2 def page! save_and_open_page end |
#select2(value, options) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 49 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
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 58 def select2_no_label(value, = {}) raise "Must pass a hash containing 'from'" if !.is_a?(Hash) || !.key?(:from) placeholder = [:from] minlength = [:minlength] || 4 click_link placeholder select_select2_result(value) end |
#select2_search(value, options) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 35 def select2_search(value, ) label = find_label_by_text([:from]) within label.first(:xpath, './/..') do [:from] = "##{find('.select2-container')['id']}" end targetted_select2_search(value, ) end |
#select_select2_result(value) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 75 def select_select2_result(value) # results are in a div appended to the end of the document within(:xpath, '//body') do page.find('div.select2-result-label', text: %r{#{Regexp.escape(value)}}i).click end end |
#set_select2_field(field, value) ⇒ Object
31 32 33 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 31 def set_select2_field(field, value) page.execute_script %Q{$('#{field}').select2('val', '#{value}')} end |
#targetted_select2(value, options) ⇒ Object
69 70 71 72 73 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 69 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
43 44 45 46 47 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 43 def targetted_select2_search(value, ) page.execute_script %Q{$('#{[:from]}').select2('open')} page.execute_script "$('#{[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');" select_select2_result(value) end |
#wait_for_ajax(delay = Capybara.default_max_wait_time) ⇒ Object
arg delay in seconds
103 104 105 106 107 108 109 110 111 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 103 def wait_for_ajax(delay = Capybara.default_max_wait_time) counter = 0 delay_threshold = delay * 10 while page.evaluate_script("typeof($) === 'undefined' || $.active > 0") counter += 1 sleep(0.1) raise "AJAX request took longer than #{delay} seconds." if counter >= delay_threshold end end |
#wait_for_condition(delay = Capybara.default_max_wait_time) ⇒ Object
“Intelligiently” wait on condition
Much better than a random sleep “here and there” it will not cause any delay in case the condition is fullfilled on first cycle.
117 118 119 120 121 122 123 124 125 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 117 def wait_for_condition(delay = Capybara.default_max_wait_time) counter = 0 delay_threshold = delay * 10 until yield counter += 1 sleep(0.1) raise "Could not achieve condition within #{delay} seconds." if counter >= delay_threshold end end |
#within_row(num, &block) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 15 def within_row(num, &block) if RSpec.current_example.[:js] within("table.table tbody tr:nth-child(#{num})", &block) else within(:xpath, all('table.table tbody tr')[num - 1].path, &block) end end |