Module: CapybaraExt

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

Instance Method Summary collapse

Instance Method Details

#click_icon(type) ⇒ Object



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

def click_icon(type)
  first(".icon-#{type}").click
end

#column_text(num) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/spree/testing_support/capybara_ext.rb', line 27

def column_text(num)
  if RSpec.current_example.[:js]
    find("td:nth-child(#{num})").text
  else
    all('td')[num - 1].text
  end
end

#delayed_fill_in(selector, text) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/spree/testing_support/capybara_ext.rb', line 3

def delayed_fill_in(selector, text)
  field = find_field(selector)
  text.to_s.split('').each do |char|
    sleep 0.05
    field.send_keys(char)
  end
end

#disable_html5_validationObject



98
99
100
# File 'lib/spree/testing_support/capybara_ext.rb', line 98

def disable_html5_validation
  page.execute_script('for(var f=document.forms,i=f.length;i--;)f[i].setAttribute("novalidate",i)')
end

#page!Object



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

def page!
  save_and_open_page
end

#select2(value, options) ⇒ Object



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

def select2(value, options)
  options[:from] = select2_from_label(options[:from])
  targetted_select2(value, options)
end

#select2_from_label(from) ⇒ Object



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

def select2_from_label(from)
  label = find(:label, from, class: '!select2-offscreen')
  within label.first(:xpath, './/..') do
    "##{find('.select2-container')['id']}"
  end
end

#select2_no_label(value, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/spree/testing_support/capybara_ext.rb', line 60

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(value, options) ⇒ Object



35
36
37
38
# File 'lib/spree/testing_support/capybara_ext.rb', line 35

def select2_search(value, options)
  options[:from] = select2_from_label(options[:from])
  targetted_select2_search(value, options)
end

#select_select2_result(value) ⇒ Object



75
76
77
78
# 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
  page.document.find('div.select2-result-label', match: :first, text: %r{#{Regexp.escape(value)}}i).click
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, options)
  # find select2 element and click it
  find(options[:from]).find('a').click
  select_select2_result(value)
end

#targetted_select2_search(value, options) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/spree/testing_support/capybara_ext.rb', line 40

def targetted_select2_search(value, options)
  select2_el = find(:css, options[:from])
  select2_el.click
  page.document.find('.select2-search input.select2-input,
                      .select2-search-field input.select2-input.select2-focused').send_keys(value)
  select_select2_result(value)
end

#wait_for_ajax(delay = Capybara.default_max_wait_time) ⇒ Object

arg delay in seconds



81
82
83
84
85
86
# File 'lib/spree/testing_support/capybara_ext.rb', line 81

def wait_for_ajax(delay = Capybara.default_max_wait_time)
  Timeout.timeout(delay) do
    active = page.evaluate_script('typeof jQuery !== "undefined" && jQuery.active')
    active = page.evaluate_script('typeof jQuery !== "undefined" && jQuery.active') until active.nil? || active.zero?
  end
end

#wait_for_condition(delay = Capybara.default_max_wait_time) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/spree/testing_support/capybara_ext.rb', line 88

def wait_for_condition(delay = Capybara.default_max_wait_time)
  counter = 0
  delay_threshold = delay * 10
  until yield
    counter += 1
    sleep(0.1)
    raise "Could not achieve condition within #{delay} seconds." if counter >= delay_threshold
  end
end

#within_row(num, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/spree/testing_support/capybara_ext.rb', line 19

def within_row(num, &block)
  if RSpec.current_example.[:js]
    within("table.table tbody tr:nth-child(#{num})", match: :first, &block)
  else
    within(all('table.table tbody tr')[num - 1], &block)
  end
end