Module: Capextensions

Defined in:
lib/mundo_pepino/capybara/extensions.rb

Constant Summary collapse

DATE_TIME_SUFFIXES =
{
        :year   => '1i',
        :month  => '2i',
        :day    => '3i',
        :hour   => '4i',
        :minute => '5i'
}

Instance Method Summary collapse

Instance Method Details



11
12
13
14
15
# File 'lib/mundo_pepino/capybara/extensions.rb', line 11

def click_link_within(selector, link_text)
  within(:css, selector) do
    click_link link_text
  end
end

#contain(content) ⇒ Object



125
126
127
# File 'lib/mundo_pepino/capybara/extensions.rb', line 125

def contain(content)
  have_text(content)
end

#have_selector(path, options = {}) ⇒ Object



118
119
120
121
122
123
# File 'lib/mundo_pepino/capybara/extensions.rb', line 118

def have_selector(path, options = {})
  # content key to text key
  content = options.delete(:content)
  options[:text] = content unless content.nil?
  have_css(path, options)
end

#have_tag(*args, &block) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/mundo_pepino/capybara/extensions.rb', line 109

def have_tag(*args, &block)
  text = args[1]
  unless text.blank?
    have_css(args.first, :text => text)
  else
    have_css(args.first)
  end
end

#response(*args, &block) ⇒ Object

Maybe this methos should be defined in the other order, capybara is the default?



105
106
107
# File 'lib/mundo_pepino/capybara/extensions.rb', line 105

def response(*args, &block)
  page(*args, &block)
end

#select_date(date_to_select, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mundo_pepino/capybara/extensions.rb', line 17

def select_date(date_to_select, options={})
  date = date_to_select.is_a?(Date) || date_to_select.is_a?(Time) ?
          date_to_select : Date.parse(date_to_select)

  if options[:id_prefix].blank?
    if options[:from].blank?
      source = locate(:xpath, Capybara::XPath.append("//select[contains(@id, '_#{DATE_TIME_SUFFIXES[:year]}')]"))
      id_prefix = source.node.attributes["id"].value[/(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/, 1]
    else
      msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
      begin
        label = locate(:xpath, Capybara::XPath.append("//label[text()='#{options[:from]}']"), msg)
        id_prefix = label.node.attributes["for"].value
      rescue Capybara::ElementNotFound
        begin
          previous_exception = $!
          label = locate(:xpath, Capybara::XPath.append("//label[text()='#{options[:from].capitalize}']"), msg)
          id_prefix = label.node.attributes["for"].value
        rescue
          raise "#{previous_exception}\nand\n#{$!}"
        end
      end
    end
  end

  id_prefix ||= options[:id_prefix]

  select date.year.to_s, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:year]}"
  select date.strftime('%B'), :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:month]}"
  select date.day.to_s, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:day]}"
end

#select_datetime(time_to_select, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mundo_pepino/capybara/extensions.rb', line 79

def select_datetime(time_to_select, options={})
  time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)

  if options[:from]
    msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
    begin
      label = locate(:xpath, Capybara::XPath.append("//label[text()='#{options[:from]}']"), msg)
      id_prefix = label.node.attributes["for"].value
    rescue Capybara::ElementNotFound
      begin
        previous_exception = $!
        label = locate(:xpath, Capybara::XPath.append("//label[text()='#{options[:from].capitalize}']"), msg)
        id_prefix = label.node.attributes["for"].value
      rescue
        raise "#{previous_exception}\nand\n#{$!}"
      end
    end
  end

  options[:id_prefix] ||= (options[:from] ? id_prefix : nil)

  select_date time, options
  select_time time, options
end

#select_time(time_to_select, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mundo_pepino/capybara/extensions.rb', line 49

def select_time(time_to_select, options ={})
  time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)

  if options[:id_prefix].blank?
    if options[:from].blank?
      source = locate(:xpath, Capybara::XPath.append("//select[contains(@id, '_#{DATE_TIME_SUFFIXES[:hour]}')]"))
      id_prefix = source.node.attributes["id"].value[/(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/, 1]
    else
      msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
      begin
        label = locate(:xpath, Capybara::XPath.append("//label[text()='#{options[:from]}']"), msg)
        id_prefix = label.node.attributes["for"].value
      rescue Capybara::ElementNotFound
        begin
          previous_exception = $!
          label = locate(:xpath, Capybara::XPath.append("//label[text()='#{options[:from].capitalize}']"), msg)
          id_prefix = label.node.attributes["for"].value
        rescue
          raise "#{previous_exception}\nand\n#{$!}"
        end
      end
    end
  end

  id_prefix ||= options[:id_prefix]

  select time.hour.to_s.rjust(2, '0'), :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:hour]}"
  select time.min.to_s.rjust(2, '0'), :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:minute]}"
end