Module: CapybaraPageObject::ElementClassMethods

Defined in:
lib/capybara_page_object/element_class_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_element_methods(element_name, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/capybara_page_object/element_class_methods.rb', line 4

def self.define_element_methods(element_name, options = {})
  aliases = options.delete(:aliases) || []
  aliases.each {|name| define_element_methods(name, options) }
  element_klass = options.fetch(:element_class, CapybaraPageObject::Element)
  define_method(element_name) do |name, finder_options|
    add_element_accessor_method(name, element_klass, finder_options)
    add_element_query_method(name, finder_options)
    case options[:default]
    when :text
      add_text_accessor_method(name)
    when :value
      add_value_accessor_method(name)
    when :click
      add_click_method(name)
    end
  end
end

Instance Method Details

#checkbox(name, finder_options) ⇒ Object

adds five methods - one to check, another to uncheck, another to return the state of a checkbox, another to return an element representing the checkbox, and a final method to check the checkbox’s existence.

Examples:

checkbox(:active, :name => "is_active")
# will generate
# * 'check_active',
# * 'uncheck_active',
# * 'active_checked?',
# * 'active_element',
# * 'active?'


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/capybara_page_object/element_class_methods.rb', line 77

def checkbox(name, finder_options)
  add_element_accessor_method(name, CapybaraPageObject::Element, finder_options)
  add_element_query_method(name, finder_options)
  element_name = "#{name}_element"
  define_method("check_#{name}") do
    self.send(element_name).set(true)
  end
  define_method("uncheck_#{name}") do
    self.send(element_name).set(false)
  end
  define_method("#{name}_checked?") do
    self.send(element_name).checked? == true ||
    self.send(element_name).checked? == 'checked'
  end
end

#file_field(name, finder_options) ⇒ Object

adds three methods - one to set the file for a file field, another to retrieve the file field element, and another to check it’s existence.

Examples:

file_field(:the_file, :css, '#file_to_upload')
# will generate
# * 'the_file=',
# * 'the_file_element',
# * 'the_file?'


104
105
106
107
108
109
110
111
112
113
# File 'lib/capybara_page_object/element_class_methods.rb', line 104

def file_field(name, finder_options)
  add_element_accessor_method(name, CapybaraPageObject::Element, finder_options)
  add_element_query_method(name, finder_options)
  define_method("#{name}=") do |value|
    self.send("#{name}_element").set(value)
  end
  define_method("#{name}") do
    self.send("#{name}_element").value
  end
end

#radio_button(name, finder_options) ⇒ Object Also known as: radio

adds four methods - one to select, another to return if a radio button is selected, another method to return a PageObject::Elements::RadioButton object representing the radio button element, and another to check the radio button’s existence.

Examples:

radio_button(:north, :css, "#north")
# * will generate
# * 'select_north',
# * 'north_selected?',
# * 'north_element',
# * 'north?' methods


129
130
131
132
133
134
135
136
137
138
# File 'lib/capybara_page_object/element_class_methods.rb', line 129

def radio_button(name, finder_options)
  add_element_accessor_method(name, CapybaraPageObject::Element, finder_options)
  add_element_query_method(name, finder_options)
  define_method("select_#{name}") do
    self.send("#{name}_element").set(true)
  end
  define_method("#{name}_selected?") do
    self.send("#{name}_element").selected?
  end
end

#radio_button_group(name, finder_options) ⇒ Object Also known as: radio_group

adds five methods to help interact with a radio button group - a method to select a radio button in the group by given value/text, a method to return the values of all radio buttons in the group, a method to return if a radio button in the group is selected (will return the text of the selected radio button, if true), a method to return an array of PageObject::Elements::RadioButton objects representing the radio button group, and finally a method to check the existence of the radio button group.

radio_button_group(:color, :css, “input”) will generate

  • ‘select_color’,

  • ‘color_values’,

  • ‘color_selected?’,

  • ‘color_elements’,

  • ‘color?’ methods



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/capybara_page_object/element_class_methods.rb', line 160

def radio_button_group(name, finder_options)
  finder_options = finder_options.dup
  finder_args = extract_finder_args(finder_options)

  define_method("select_#{name}") do |value|
    elements = self.send("#{name}_elements")
    elements.find {|e| e.value == value }.set(true)
  end
  define_method("#{name}_values") do
    self.send("#{name}_elements").map(&:value)
  end
  define_method("#{name}_selected?") do
    elements = self.send("#{name}_elements")
    selected = elements.select {|e| e.checked? }
    selected.first ? selected.first.value : false
  end
  define_method("#{name}_elements") do
    all(*finder_args)
  end
  define_method("#{name}?") do
    assert_selector(*finder_args)
  end
end

#select_list(name, finder_options) ⇒ Object Also known as: select

adds five methods - one to select an item in a drop-down, another to fetch the currently selected item text, another to retrieve the select list element, another to check the drop down’s existence and another to get all the available options to select from.

Examples:

select_list(:state, :css, "#state")
# will generate 'state', 'state=', 'state_element', 'state?', "state_options" methods


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/capybara_page_object/element_class_methods.rb', line 197

def select_list(name, finder_options)
  add_element_accessor_method(name, CapybaraPageObject::SelectListElement, finder_options)
  add_element_query_method(name, finder_options)

  element_name = "#{name}_element"
  define_method(name) do
    self.send(element_name).value
  end
  define_method("selected_#{name}") do
    within(self.send(element_name)) do
      find('option[selected]')
    end
  end
  define_method("#{name}=") do |value|
    element = self.send(element_name)
    within(element) do
      find(:option, value).select_option
    end
  end
  define_method("#{name}_options") do
    element = self.send(element_name)
    within(element) do
      all(:css, 'option')
    end
  end
end

#text_area(name, finder_options) ⇒ Object Also known as: textarea

adds four methods to the page object - one to set text in a text area, another to retrieve text from a text area, another to return the text area element, and another to check the text area’s existence.

Examples:

text_area(:address, :css, "#address")
# will generate
# * 'address',
# * 'address=',
# * 'address_element',
# * 'address?'


238
239
240
241
242
243
# File 'lib/capybara_page_object/element_class_methods.rb', line 238

def text_area(name, finder_options)
  add_element_accessor_method(name, CapybaraPageObject::TextAreaElement, finder_options)
  add_element_query_method(name, finder_options)
  add_value_accessor_method(name)
  add_value_mutator_method(name)
end

#text_field(name, finder_options) ⇒ Object



246
247
248
249
250
251
# File 'lib/capybara_page_object/element_class_methods.rb', line 246

def text_field(name, finder_options)
  add_element_accessor_method(name, CapybaraPageObject::TextFieldElement, finder_options)
  add_element_query_method(name, finder_options)
  add_value_accessor_method(name)
  add_value_mutator_method(name)
end