Module: CapybaraExt

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

Instance Method Summary collapse

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


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

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

#eventually_fill_in(field, options = {}) ⇒ Object


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

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

#find_label(text) ⇒ Object


91
92
93
# File 'lib/spree/testing_support/capybara_ext.rb', line 91

def find_label(text)
  first(:xpath, "//label[text()[contains(.,'#{text}')]]")
end

#find_label_by_text(text) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/spree/testing_support/capybara_ext.rb', line 73

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
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
48
# File 'lib/spree/testing_support/capybara_ext.rb', line 41

def select2(value, options)
  label = find_label_by_text(options[:from])

  within label.first(:xpath,".//..") do
    options[:from] = "##{find(".select2-container")["id"]}"
    targetted_select2(value, options)
  end
end

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


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

def select2_no_label value, options={}
  raise "Must pass a hash containing 'from'" if not options.is_a?(Hash) or not options.has_key?(:from)

  placeholder = options[:from]
  minlength = options[:minlength] || 4

  click_link placeholder

  select_select2_result(value)
end

#select2_search(value, options) ⇒ Object


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

def select2_search(value, options)
  label = find_label_by_text(options[:from])
  within label.first(:xpath,".//..") do
    options[:from] = "##{find(".select2-container")["id"]}"
    targetted_select2_search(value, options)
  end
end

#select_select2_result(value) ⇒ Object


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

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


23
24
25
# File 'lib/spree/testing_support/capybara_ext.rb', line 23

def set_select2_field(field, value)
  page.execute_script %Q{$('#{field}').select2('val', '#{value}')}
end

#targetted_select2(value, options) ⇒ Object


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

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


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

def targetted_select2_search(value, options)
  page.execute_script %Q{$('#{options[:from]}').select2('open')}
  page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');"
  select_select2_result(value)
end

#wait_for_ajaxObject


95
96
97
98
99
100
101
102
# File 'lib/spree/testing_support/capybara_ext.rb', line 95

def wait_for_ajax
  counter = 0
  while page.execute_script("return $.active").to_i > 0
    counter += 1
    sleep(0.1)
    raise "AJAX request took longer than 5 seconds." if counter >= 50
  end
end

#within_row(num, &block) ⇒ Object


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

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