Module: CapybaraExt
- Defined in:
- lib/spree/testing_support/capybara_ext.rb
Instance Method Summary collapse
- #click_icon(type) ⇒ Object
- #column_text(num) ⇒ Object
- #disable_html5_validation ⇒ Object
- #dismiss_alert ⇒ Object
- #eventually_fill_in(field, options = {}) ⇒ Object
- #find_label(text) ⇒ Object
- #find_label_by_text(text) ⇒ Object
- #handle_js_confirm(accept = true) ⇒ Object
- #native_fill_in(selector, 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
- #spree_accept_alert ⇒ 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
#click_icon(type) ⇒ Object
11 12 13 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 11 def click_icon(type) find(".icon-#{type}").click end |
#column_text(num) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 28 def column_text(num) if RSpec.current_example.[:js] find("td:nth-child(#{num})").text else all('td')[num - 1].text end end |
#disable_html5_validation ⇒ Object
143 144 145 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 143 def disable_html5_validation page.execute_script('for(var f=document.forms,i=f.length;i--;)f[i].setAttribute("novalidate",i)') end |
#dismiss_alert ⇒ Object
128 129 130 131 132 133 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 128 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
15 16 17 18 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 15 def eventually_fill_in(field, = {}) expect(page).to have_css('#' + field) fill_in field, end |
#find_label(text) ⇒ Object
101 102 103 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 101 def find_label(text) first(:xpath, "//label[text()[contains(.,'#{text}')]]") end |
#find_label_by_text(text) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 85 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 |
#handle_js_confirm(accept = true) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 147 def handle_js_confirm(accept = true) page.evaluate_script 'window.original_confirm_function = window.confirm' page.evaluate_script "window.confirm = function(msg) { return #{accept}; }" yield ensure page.evaluate_script 'window.confirm = window.original_confirm_function' end |
#native_fill_in(selector, text) ⇒ Object
3 4 5 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 3 def native_fill_in(selector, text) text.to_s.split('').each { |char| find_field(selector).native.send_keys(char) } end |
#page! ⇒ Object
7 8 9 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 7 def page! save_and_open_page end |
#select2(value, options) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 54 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
63 64 65 66 67 68 69 70 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 63 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
40 41 42 43 44 45 46 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 40 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
78 79 80 81 82 83 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 78 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
36 37 38 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 36 def set_select2_field(field, value) page.execute_script %Q{$('#{field}').select2('val', '#{value}')} end |
#spree_accept_alert ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 135 def spree_accept_alert yield rescue Selenium::WebDriver::Error::UnhandledAlertError, Selenium::WebDriver::Error::NoSuchAlertError Selenium::WebDriver::Wait.new(timeout: 5) .until { page.driver.browser.switch_to.alert } .accept end |
#targetted_select2(value, options) ⇒ Object
72 73 74 75 76 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 72 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
48 49 50 51 52 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 48 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
106 107 108 109 110 111 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 106 def wait_for_ajax(delay = Capybara.default_max_wait_time) Timeout.timeout(delay) do active = page.evaluate_script('typeof jQuery !== "undefined" && jQuery.active') active = page.evaluate_script('typeof jQuery !== "undefined" && jQuery.active') until active.nil? || active.zero? 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.
118 119 120 121 122 123 124 125 126 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 118 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
20 21 22 23 24 25 26 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 20 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 |