Module: CapybaraExt

Defined in:
lib/spree/testing_support/capybara_ext.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) click_icon(type)



5
6
7
# File 'lib/spree/testing_support/capybara_ext.rb', line 5

def click_icon(type)
  find(".fa-#{type}").click
end

- (Object) column_text(num)



18
19
20
# File 'lib/spree/testing_support/capybara_ext.rb', line 18

def column_text(num)
  find("td:nth-of-type(#{num})").text
end

- (Object) eventually_fill_in(field, options = {})



9
10
11
12
# File 'lib/spree/testing_support/capybara_ext.rb', line 9

def eventually_fill_in(field, options = {})
  expect(page).to have_css('#' + field)
  fill_in field, options
end

- (Object) fill_in_quantity(table_column, selector, quantity)



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spree/testing_support/capybara_ext.rb', line 22

def fill_in_quantity(table_column, selector, quantity)
  Spree::Deprecation.warn <<-WARN.strip_heredoc
    fill_in_quantity is deprecated. Instead use:
      within(#{table_column.inspect}) do
        fill_in #{selector.inspect}, with: #{quantity.inspect}
      end
  WARN
  within(table_column) do
    fill_in selector, with: quantity
  end
end

- (Object) find_label_by_text(text)



99
100
101
102
103
104
105
106
# File 'lib/spree/testing_support/capybara_ext.rb', line 99

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

- (Object) page!



2
3
# File 'lib/spree/testing_support/capybara_ext.rb', line 2

def page!
end

- (Object) select2(value, options)



67
68
69
70
71
72
73
74
# File 'lib/spree/testing_support/capybara_ext.rb', line 67

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

- (Object) select2_no_label(value, options = {})



76
77
78
79
80
81
82
83
84
# File 'lib/spree/testing_support/capybara_ext.rb', line 76

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

- (Object) select2_search(value, options)



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spree/testing_support/capybara_ext.rb', line 34

def select2_search(value, options)
  options = {
    search: value, # by default search for the value
    select: true
  }.merge(options)
  label = find_label_by_text(options[:from])
  within label.first(:xpath, ".//..") do
    options[:from] = "##{find('.select2-container')['id']}"
  end
  select2_search_without_selection(options[:search], from: options[:from])
  select_select2_result(value) if options[:select]
end

- (Object) select2_search_without_selection(value, options)



47
48
49
50
51
52
53
# File 'lib/spree/testing_support/capybara_ext.rb', line 47

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

- (Object) select_select2_result(value)



92
93
94
95
96
97
# File 'lib/spree/testing_support/capybara_ext.rb', line 92

def select_select2_result(value)
  # results are in a div appended to the end of the document
  within_entire_page do
    page.find("div.select2-result-label", text: /#{Regexp.escape(value)}/i, match: :prefer_exact).click
  end
end

- (Object) targetted_select2(value, options)



86
87
88
89
90
# File 'lib/spree/testing_support/capybara_ext.rb', line 86

def targetted_select2(value, options)
  # find select2 element and click it
  find(options[:from]).find('a').click
  select_select2_result(value)
end

- (Object) targetted_select2_search(value, options)



55
56
57
58
# File 'lib/spree/testing_support/capybara_ext.rb', line 55

def targetted_select2_search(value, options)
  select2_search_without_selection(value, from: options[:from])
  select_select2_result(value)
end

- (Object) wait_for_ajax



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/spree/testing_support/capybara_ext.rb', line 108

def wait_for_ajax

  Spree::Deprecation.warn <<-WARN.squish, caller
    wait_for_ajax has been deprecated.
    Please refer to the capybara documentation on how to properly wait for asyncronous behavior:
    https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends
  WARN

  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

- (Object) within_entire_page(&block)

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.



63
64
65
# File 'lib/spree/testing_support/capybara_ext.rb', line 63

def within_entire_page(&block)
  within(:xpath, '//body', &block)
end

- (Object) within_row(num, &block)



14
15
16
# File 'lib/spree/testing_support/capybara_ext.rb', line 14

def within_row(num, &block)
  within("table.index tbody tr:nth-of-type(#{num})", &block)
end