Module: CapybaraExt

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

Instance Method Summary collapse

Instance Method Details

#accept_alertObject



113
114
115
116
# File 'lib/spree/testing_support/capybara_ext.rb', line 113

def accept_alert
  page.evaluate_script('window.confirm = function() { return true; }')
  yield
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 RSpec.current_example.[:js]
    find("td:nth-child(#{num})").text
  else
    all("td")[num-1].text
  end
end

#dismiss_alertObject



118
119
120
121
122
123
# File 'lib/spree/testing_support/capybara_ext.rb', line 118

def dismiss_alert
  page.evaluate_script('window.confirm = function() { return false; }')
  yield
  # Restore existing default
  page.evaluate_script('window.confirm = function() { return true; }')
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={})
  expect(page).to have_css('#' + field)
  fill_in field, options
end

#fill_in_quantity(table_column, selector, quantity) ⇒ Object



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

def fill_in_quantity(table_column, selector, quantity)
  within(table_column) do
    fill_in selector, :with => quantity
  end
end

#find_label_by_text(text) ⇒ Object



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

def find_label_by_text(text)
  find(:xpath, "//label[text()[contains(.,'#{text}')]]")
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



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

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



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

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



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

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

#select2_search_without_selection(value, options) ⇒ Object



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

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

#select_select2_result(value) ⇒ Object



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

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: %r{#{Regexp.escape(value)}}i).click
  end
end

#targetted_select2(value, options) ⇒ Object



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

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



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

def targetted_select2_search(value, options)
  find("#{options[:from]}:not(.select2-container-disabled)").click

  within_entire_page do
    find("input.select2-input").set(value)
  end

  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_entire_page(&block) ⇒ Object

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

#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 RSpec.current_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