Module: Spree::TestingSupport::CapybaraExt
- Defined in:
- lib/spree/testing_support/capybara_ext.rb
Instance Method Summary collapse
- #click_icon(type) ⇒ Object
- #column_text(num) ⇒ Object
- #dialog(parent: "body", **options) ⇒ Object
- #eventually_fill_in(field, options = {}) ⇒ Object
- #fill_in_with_force(locator, with:) ⇒ Object
- #find_label_by_text(text) ⇒ Object
- #select2(value, options) ⇒ Object
- #select2_no_label(value, options = {}) ⇒ Object
- #select2_search(value, options) ⇒ Object
- #select2_search_without_selection(value, options) ⇒ Object
- #select_select2_result(value) ⇒ Object
- #targetted_select2(value, options) ⇒ Object
- #targetted_select2_search(value, options) ⇒ Object
- #turbo_frame_modal ⇒ Object
-
#within_entire_page(&block) ⇒ Object
Executes the given block within the context of the entire capybara document.
- #within_row(num, &block) ⇒ Object
Instance Method Details
#click_icon(type) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 6 def click_icon(type) el = find(".fa-#{type}", visible: :all) begin el.click rescue Selenium::WebDriver::Error::ElementClickInterceptedError # When a floating element (eg. tooltips/overlays) intercepts the click, # scroll the target into view and dispatch a click via JS to keep tests stable. page.execute_script('arguments[0].scrollIntoView({block: "center"});', el.native) page.execute_script("arguments[0].click();", el.native) end end |
#column_text(num) ⇒ Object
42 43 44 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 42 def column_text(num) find("td:nth-of-type(#{num})").text end |
#dialog(parent: "body", **options) ⇒ Object
122 123 124 125 126 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 122 def dialog(parent: "body", **) within(parent) do find("dialog", visible: :all, **) end end |
#eventually_fill_in(field, options = {}) ⇒ Object
18 19 20 21 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 18 def eventually_fill_in(field, = {}) expect(page).to have_css("#" + field) fill_in field, end |
#fill_in_with_force(locator, with:) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 23 def fill_in_with_force(locator, with:) if .current_driver == .javascript_driver field_id = find_field(locator)[:id] page.execute_script " var field = document.getElementById('\#{field_id}');\n field.value = '\#{with}';\n\n var event = new Event('change', { bubbles: true });\n field.dispatchEvent(event);\n JS\n else\n fill_in locator, with:\n end\nend\n" |
#find_label_by_text(text) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 113 def find_label_by_text(text) # This used to find the label by it's text using an xpath query, so we use # a case insensitive search to avoid breakage with existing usage. # We need to select labels which are not .select2-offscreen, as select2 # makes a duplicate label with the same text, and we want to be sure to # find the original. find("label:not(.select2-offscreen)", text: /#{Regexp.escape(text)}/i, match: :one) end |
#select2(value, options) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 79 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
88 89 90 91 92 93 94 95 96 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 88 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
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 46 def select2_search(value, ) = { search: value, # by default search for the value select: true }.merge() label = find_label_by_text([:from]) within label.first(:xpath, ".//..") do [:from] = "##{find(".select2-container")["id"]}" end select2_search_without_selection([:search], from: [:from]) select_select2_result(value) if [:select] end |
#select2_search_without_selection(value, options) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 59 def select2_search_without_selection(value, ) find("#{options[:from]}:not(.select2-container-disabled):not(.select2-offscreen)").click within_entire_page do find("input.select2-input.select2-focused").set(value) end end |
#select_select2_result(value) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 104 def select_select2_result(value) # results are in a div appended to the end of the document within_entire_page do expect(page).to have_selector(".select2-result-label", visible: true) find("div.select2-result-label", text: /#{Regexp.escape(value)}/i, match: :prefer_exact).click expect(page).not_to have_selector(".select2-result-label") end end |
#targetted_select2(value, options) ⇒ Object
98 99 100 101 102 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 98 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
67 68 69 70 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 67 def targetted_select2_search(value, ) select2_search_without_selection(value, from: [:from]) select_select2_result(value) end |
#turbo_frame_modal ⇒ Object
128 129 130 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 128 def turbo_frame_modal dialog(parent: find("turbo-frame", visible: :all)) end |
#within_entire_page(&block) ⇒ Object
Executes the given block within the context of the entire capybara document. Can be used to 'escape' from within the context of another within block.
75 76 77 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 75 def within_entire_page(&block) within(:xpath, "//body", &block) end |
#within_row(num, &block) ⇒ Object
38 39 40 |
# File 'lib/spree/testing_support/capybara_ext.rb', line 38 def within_row(num, &block) within("table.index tbody tr:nth-of-type(#{num})", &block) end |