Module: CapybaraExt

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

Instance Method Summary collapse

Instance Method Details

#accept_alertObject



113
114
115
116
117
118
119
120
121
# File 'lib/spree/testing_support/capybara_ext.rb', line 113

def accept_alert
  if page.driver === Capybara::Selenium::Driver
    yield
    page.driver.browser.switch_to.alert.accept
  else
    page.evaluate_script('window.confirm = function() { return true; }')
    yield
  end
end

#click_icon(type) ⇒ Object



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

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

#column_text(num) ⇒ Object



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

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

#dismiss_alertObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/spree/testing_support/capybara_ext.rb', line 122

def dismiss_alert
  if page.driver === Capybara::Selenium::Driver
    yield
    page.driver.browser.switch_to.alert.dismiss
  else
    page.evaluate_script('window.confirm = function() { return false; }')
    yield

    # Restore existing default
    page.evaluate_script('window.confirm = function() { return true; }')
  end
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



100
101
102
# File 'lib/spree/testing_support/capybara_ext.rb', line 100

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

#find_label_by_text(text) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/spree/testing_support/capybara_ext.rb', line 82

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



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

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 = {}) ⇒ Object



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

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



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

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

#select_select2_result(value) ⇒ Object



75
76
77
78
79
80
# 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
  within(:xpath, '//body') do
    page.find("div.select2-result-label", text: %r{#{Regexp.escape(value)}}i).click
  end
end

#set_select2_field(field, value) ⇒ Object



31
32
33
# File 'lib/spree/testing_support/capybara_ext.rb', line 31

def set_select2_field(field, value)
  page.execute_script %Q{$('#{field}').select2('val', '#{value}')}
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



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

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



104
105
106
107
108
109
110
111
# File 'lib/spree/testing_support/capybara_ext.rb', line 104

def wait_for_ajax
  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

#within_row(num, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/spree/testing_support/capybara_ext.rb', line 15

def within_row(num, &block)
  if example.[:js]
    within("table.index tbody tr:nth-child(#{num})", &block)
  else
    within(:xpath, all("table.index tbody tr")[num-1].path, &block)
  end
end