Module: 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
- #find_label(text) ⇒ Object
- #find_label_by_text(text) ⇒ Object
- #page! ⇒ Object
- #select2(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
- #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
21 22 23 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 21 def column_text(num) find("td:nth-child(#{num})").text end |
#eventually_fill_in(field, options = {}) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 10 def eventually_fill_in(field, ={}) Capybara.wait_until do find_field field end fill_in field, end |
#find_label(text) ⇒ Object
79 80 81 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 79 def find_label(text) first(:xpath, "//label[text()[contains(.,'#{text}')]]") end |
#find_label_by_text(text) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 61 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 ? label['for'] : text 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
41 42 43 44 45 46 47 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 41 def select2(value, ) id = find_label_by_text([:from]) # generate select2 id [:from] = "#s2id_#{id}" targetted_select2(value, ) end |
#select2_search(value, options) ⇒ Object
29 30 31 32 33 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 29 def select2_search(value, ) id = find_label_by_text([:from]) [:from] = "#s2id_#{id}" targetted_select2_search(value, ) end |
#select_select2_result(value) ⇒ Object
55 56 57 58 59 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 55 def select_select2_result(value) #p %Q{$("div.select2-result-label:contains('#{value}')").mouseup()} sleep(1) page.execute_script(%Q{$("div.select2-result-label:contains('#{value}')").mouseup()}) end |
#set_select2_field(field, value) ⇒ Object
25 26 27 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 25 def set_select2_field(field, value) page.execute_script %Q{$('#{field}').select2('val', '#{value}')} end |
#targetted_select2(value, options) ⇒ Object
49 50 51 52 53 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 49 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
35 36 37 38 39 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 35 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 |
#within_row(num, &block) ⇒ Object
17 18 19 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 17 def within_row(num, &block) within("table.index tbody tr:nth-child(#{num})", &block) end |