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
- #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
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 |
#disable_html5_validation ⇒ Object
138 139 140 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 138 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
125 126 127 128 129 130 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 125 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
96 97 98 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 96 def find_label(text) first(:xpath, "//label[text()[contains(.,'#{text}')]]") end |
#find_label_by_text(text) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 80 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 |
# 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] 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
73 74 75 76 77 78 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 73 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 |
#spree_accept_alert ⇒ Object
132 133 134 135 136 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 132 def spree_accept_alert yield rescue Selenium::WebDriver::Error::UnhandledAlertError page.driver.browser.switch_to.alert.accept end |
#targetted_select2(value, options) ⇒ Object
67 68 69 70 71 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 67 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
101 102 103 104 105 106 107 108 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 101 def wait_for_ajax(delay = Capybara.default_max_wait_time) Timeout.timeout(delay) do active = page.evaluate_script('typeof jQuery !== "undefined" && jQuery.active') until active.zero? active = page.evaluate_script('typeof jQuery !== "undefined" && jQuery.active') end 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.
115 116 117 118 119 120 121 122 123 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 115 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 |