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 ⇒ Object
- #within_row(num, &block) ⇒ Object
Instance Method Details
#accept_alert ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 113 def accept_alert if page.driver === Capybara::Selenium::Driver yield page.driver.browser.switch_to.alert.accept else page.evaluate_script('window.confirm = function() { return true; }') yield end end |
#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
23 24 25 26 27 28 29 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 23 def column_text(num) if example.[:js] find("td:nth-child(#{num})").text else all("td")[num-1].text end end |
#dismiss_alert ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 122 def dismiss_alert if page.driver === Capybara::Selenium::Driver yield page.driver.browser.switch_to.alert.dismiss else page.evaluate_script('window.confirm = function() { return false; }') yield # Restore existing default page.evaluate_script('window.confirm = function() { return true; }') end 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, ={}) page.should have_css('#' + field) fill_in field, end |
#find_label(text) ⇒ Object
100 101 102 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 100 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 97 98 |
# 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 if label.nil? raise "Could not find label by text #{text}" end 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 not .is_a?(Hash) or not .has_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 ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 104 def wait_for_ajax 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_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 example.[:js] within("table.index tbody tr:nth-child(#{num})", &block) else within(:xpath, all("table.index tbody tr")[num-1].path, &block) end end |