Module: CapybaraExt

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

Instance Method Summary collapse

Instance Method Details

#accept_alertObject



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

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



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

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

#dismiss_alertObject



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

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



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

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



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

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: %r{#{Regexp.escape(text)}}i, match: :one)
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



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

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



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

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



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

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



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

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

#select_select2_result(value) ⇒ Object



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

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, match: :prefer_exact).click
  end
end

#targetted_select2(value, options) ⇒ Object



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

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



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

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

#wait_for_ajaxObject



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

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.



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

def within_entire_page(&block)
  within(:xpath, '//body', &block)
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-of-type(#{num})", &block)
end