module Spree::TestingSupport::CapybaraExt

Public Instance Methods

click_icon(type) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 6
def click_icon(type)
  find(".fa-#{type}").click
end
column_text(num) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 34
def column_text(num)
  find("td:nth-of-type(#{num})").text
end
eventually_fill_in(field, options = {}) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 10
def eventually_fill_in(field, options = {})
  expect(page).to have_css('#' + field)
  fill_in field, options
end
fill_in_with_force(locator, with:) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 15
      def fill_in_with_force(locator, with:)
        if Capybara.current_driver == Capybara.javascript_driver
          field_id = find_field(locator)[:id]
          page.execute_script <<-JS
            var field = document.getElementById('#{field_id}');
            field.value = '#{with}';

            var event = new Event('change', { bubbles: true });
            field.dispatchEvent(event);
          JS
        else
          fill_in locator, with: with
        end
      end
find_label_by_text(text) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 105
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) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 71
def select2(value, options)
  label = find_label_by_text(options[:from])

  within label.first(:xpath, ".//..") do
    options[:from] = "##{find('.select2-container')['id']}"
  end
  targetted_select2(value, options)
end
select2_no_label(value, options = {}) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 80
def select2_no_label(value, options = {})
  raise "Must pass a hash containing 'from'" if !options.is_a?(Hash) || !options.key?(:from)

  placeholder = options[:from]

  click_link placeholder

  select_select2_result(value)
end
select2_search_without_selection(value, options) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 51
def select2_search_without_selection(value, options)
  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) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 96
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) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 90
def targetted_select2(value, options)
  # find select2 element and click it
  find(options[:from]).find('a').click
  select_select2_result(value)
end
within_entire_page(&block) click to toggle source

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.

# File lib/spree/testing_support/capybara_ext.rb, line 67
def within_entire_page(&block)
  within(:xpath, '//body', &block)
end
within_row(num, &block) click to toggle source
# File lib/spree/testing_support/capybara_ext.rb, line 30
def within_row(num, &block)
  within("table.index tbody tr:nth-of-type(#{num})", &block)
end