Module: CapybaraPageObject::ClassMethods

Defined in:
lib/capybara_page_object/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_click_method(name) ⇒ Object



222
223
224
225
226
# File 'lib/capybara_page_object/class_methods.rb', line 222

def add_click_method(name)
  define_method(name) do
    self.send("#{name}_element").click
  end
end

#add_element_accessor_method(name, element_klass, finder_options) ⇒ Object

end



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/capybara_page_object/class_methods.rb', line 168

def add_element_accessor_method(name, element_klass, finder_options)
  finder_options = finder_options.dup
  finder_args = extract_finder_args(finder_options)
  element_klass ||= CapybaraPageObject::Element
  define_method("#{name}_element") do
    e = find(*finder_args)
    element_klass.new(e, self)
  end
  define_method("#{name}_selector") do |options={}|
    [finder_args.first,
     finder_args[1],
     finder_args[2].merge(options)]
  end
end

#add_element_query_method(name, finder_options) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/capybara_page_object/class_methods.rb', line 183

def add_element_query_method(name, finder_options)
  finder_options = finder_options.dup
  finder_args = extract_finder_args(finder_options)
  define_method("has_#{name}?") do |options = {}|
    args = self.send("#{name}_selector", options)
    has_selector?(*args)
  end
  define_method("has_no_#{name}?") do |options = {}|
    args = self.send("#{name}_selector", options)
    has_no_selector?(*args)
  end
  define_method("assert_has_#{name}") do |options = {}|
    args = self.send("#{name}_selector", options)
    assert_selector(*args)
  end
  define_method("assert_has_no_#{name}") do |options = {}|
    args = self.send("#{name}_selector", options)
    assert_no_selector(*args)
  end
end

#add_text_accessor_method(name) ⇒ Object



210
211
212
213
214
# File 'lib/capybara_page_object/class_methods.rb', line 210

def add_text_accessor_method(name)
  define_method(name) do
    self.send("#{name}_element").text
  end
end

#add_value_accessor_method(name) ⇒ Object



204
205
206
207
208
# File 'lib/capybara_page_object/class_methods.rb', line 204

def add_value_accessor_method(name)
  define_method(name) do
    self.send("#{name}_element").value
  end
end

#add_value_mutator_method(name) ⇒ Object



216
217
218
219
220
# File 'lib/capybara_page_object/class_methods.rb', line 216

def add_value_mutator_method(name)
  define_method("#{name}=") do |value|
    self.send("#{name}_element").set(value)
  end
end

#expected_element(element_name) ⇒ boolean

Creates a method that provides a way to initialize a page based upon an expected element. This is useful for pages that load dynamic content.

Examples:

Specify a text box named :address expected on the page within 10 seconds

expected_element(:address, 10)
page.has_expected_element?

Parameters:

  • the (Symbol)

    name given to the element in the declaration

  • timeout (optional, Integer)

    default value is 5 seconds

Returns:

  • (boolean)


67
68
69
70
71
72
73
# File 'lib/capybara_page_object/class_methods.rb', line 67

def expected_element(element_name)
  define_method("has_expected_element?") do
    args = self.send("#{element_name}_selector")
    page.assert_selector(*args)
    true
  end
end

#expected_element_visible(element_name, timeout = Capybara.default_wait_time) ⇒ boolean

Creates a method that provides a way to initialize a page based upon an expected element to become visible. This is useful for pages that load dynamic content and might have hidden elements that are not shown.

Examples:

Specify a text box named :address expected on the page within 10 seconds

expected_element_visible(:address, 10)
page.has_expected_element_visible?

Parameters:

  • the (Symbol)

    name given to the element in the declaration

  • timeout (optional, Integer) (defaults to: Capybara.default_wait_time)

    default value is 5 seconds

  • also (optional, boolean)

    check that element to be visible if set to true

Returns:

  • (boolean)


87
88
89
90
91
# File 'lib/capybara_page_object/class_methods.rb', line 87

def expected_element_visible(element_name, timeout=Capybara.default_wait_time)
  define_method("has_expected_element_visible?") do
    expect(self.send("#{element_name}_element")).to be_visible
  end
end

#expected_title(expected_title) ⇒ boolean

Creates a method that compares the expected_title of a page against the actual.

Examples:

Specify ‘Google’ as the expected title of a page

expected_title "Google"
page.has_expected_title?

Parameters:

  • expected_title (String)

    the literal expected title for the page

  • expected_title (Regexp)

    the expected title pattern for the page

Returns:

  • (boolean)

Raises:

  • An exception if expected_title does not match actual title



50
51
52
53
54
# File 'lib/capybara_page_object/class_methods.rb', line 50

def expected_title(expected_title)
  define_method("has_expected_title?") do
    page.has_title?(expected_title)
  end
end

#extract_finder_args(finder_options = {}) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/capybara_page_object/class_methods.rb', line 228

def extract_finder_args(finder_options={})
  finder_type, finder_param = extract_finder_type!(finder_options, :class, :css, :id, :xpath)
  finder_options.delete(finder_type)
  case finder_type
    when :class
      [:css, ".{finder_param}", finder_options]
    when :id
      [:css, "##{finder_param}", finder_options]
    when :css
      [finder_type, finder_param, finder_options]
    when :xpath
      [finder_type, finder_param, finder_options]
    else
      raise "oops"
  end
end

#extract_finder_type!(options, *types) ⇒ Object



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

def extract_finder_type!(options, *types)
  found_keys = options.select{|k,v| types.include?(k)}
  unless found_keys.length == 1
    raise "Incorrect finder type specified '#{found_keys.keys.join(', ')}' - only one of #{types.join(', ')} can be specified at a time."
  end
  found_keys.to_a.first
end

#page_url(url) ⇒ Object Also known as: page_path

Specify the url for the page. A call to this method will generate a ‘goto’ method to take you to the page.

Parameters:

  • the (String)

    url for the page.

  • a (Symbol)

    method name to call to get the url



28
29
30
31
32
33
34
35
36
# File 'lib/capybara_page_object/class_methods.rb', line 28

def page_url(url)
  define_method("goto") do
    visit self.page_url_value
  end

  define_method('page_url_value') do
    url
  end
end

#paramsObject

Return the params that exist on this page class



17
18
19
# File 'lib/capybara_page_object/class_methods.rb', line 17

def params
  @params ||= {}
end

#params=(the_params) ⇒ Object

Set some values that can be used within the class. This is typically used to provide values that help build dynamic urls in the page_url method

Parameters:

  • the (Hash)

    value to set the params



10
11
12
# File 'lib/capybara_page_object/class_methods.rb', line 10

def params=(the_params)
  @params = the_params
end