Module: Kelp::Field

Includes:
Helper, Scoping
Defined in:
lib/kelp/field.rb

Overview

This module defines helper methods for filling in and verifying the content of fields in a web form.

Instance Method Summary collapse

Methods included from Helper

#listify, #nice_find_field, #rspec_unexpected

Methods included from Scoping

#in_scope, #kelp_within, #scope_within

Instance Method Details

#check_or_select_or_fill(field, value) ⇒ Object

Check a checkbox, select from a dropdown or listbox, or fill in a text field, depending on what kind of form field is available. If value is checked or unchecked, assume field is a checkbox; otherwise, fall back on #select_or_fill.

Raises:

Since:

  • 0.1.9



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kelp/field.rb', line 66

def check_or_select_or_fill(field, value)
  # If value is "checked" or "unchecked", assume
  # field is a checkbox
  begin
    if value == "checked"
      check(field)
    elsif value == "unchecked"
      uncheck(field)
    else
      select_or_fill(field, value)
    end
  rescue Capybara::ElementNotFound
    select_or_fill(field, value)
  end
end

#field_should_be_empty(field, scope = {}) ⇒ Object

Verify that the given field is empty or nil.

Raises:



166
167
168
169
170
171
172
173
174
# File 'lib/kelp/field.rb', line 166

def field_should_be_empty(field, scope={})
  in_scope(scope) do
    _field = nice_find_field(field)
    if !(_field.nil? || _field.value.nil? || _field.value.strip == '')
      raise Kelp::Unexpected,
        "Expected field '#{field}' to be empty, but value is '#{_field.value}'"
    end
  end
end

#field_should_contain(field, value, scope = {}) ⇒ Object

Verify that the given field contains the given value.

Raises:



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/kelp/field.rb', line 211

def field_should_contain(field, value, scope={})
  in_scope(scope) do
    actual = field_value(field)
    # Escape any problematic characters in the expected value
    expect = Regexp.escape(value)
    # Match actual to expected
    if !(actual =~ /#{expect}/)
      raise Kelp::Unexpected,
        "Expected '#{field}' to contain '#{expect}'" + \
        "\nGot '#{actual}'"
    end
  end
end

#field_should_contain_within(selector, field, value) ⇒ Object

Verify a single field within the scope of a given selector. Alias for:

field_should_contain field, value, :within => selector



287
288
289
# File 'lib/kelp/field.rb', line 287

def field_should_contain_within(selector, field, value)
  field_should_contain field, value, :within => selector
end

#field_should_not_contain(field, value, scope = {}) ⇒ Object

Verify that the given field does not contain the given value.

Raises:



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/kelp/field.rb', line 238

def field_should_not_contain(field, value, scope={})
  in_scope(scope) do
    actual = field_value(field)
    # Escape any problematic characters in the expected value
    expect_not = Regexp.escape(value)
    # Match actual to expected
    if (actual =~ /#{expect_not}/)
      raise Kelp::Unexpected,
        "Did not expect '#{field}' to contain '#{expect_not}'" + \
        "\nGot '#{actual}'"
    end
  end
end

#field_value(field) ⇒ String

Return the string value found in the given field. If the field is nil, return the empty string.

Since:

  • 0.2.1



188
189
190
191
192
193
194
195
196
# File 'lib/kelp/field.rb', line 188

def field_value(field)
  element = find_field(field)
  value = (element.tag_name == 'textarea') ? element.text : element.value
  # If field value is an Array, take the first item
  if value.class == Array
    value = value.first
  end
  return value.to_s
end

#fields_should_contain(field_values, scope = {}) ⇒ Object

Verify the values of multiple fields given as a Hash.

Raises:



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/kelp/field.rb', line 263

def fields_should_contain(field_values, scope={})
  in_scope(scope) do
    field_values.each do |field, value|
      _field = find_field(field)
      # For nil/empty, check for nil field or nil value
      if value.nil? or value.strip.empty?
        field_should_be_empty(field)
      # If field is a dropdown
      elsif _field.tag_name == 'select'
        dropdown_should_equal(field, value)
      # Otherwise treat as a text field
      else
        field_should_contain(field, value)
      end
    end
  end
end

#fields_should_contain_within(selector, field_values) ⇒ Object

Verify fields within the scope of a given selector. Alias for:

fields_should_contain field_values, :within => selector



297
298
299
# File 'lib/kelp/field.rb', line 297

def fields_should_contain_within(selector, field_values)
  fields_should_contain field_values, :within => selector
end

#fill_in_field(field, value, scope = {}) ⇒ Object

Fill in a single field within the scope of a given selector. The field may be a text box, dropdown/listbox, or checkbox. See #check_or_select_or_fill for details.

Examples:

fill_in_field "Email", "otto@scratchansniff.wb"
fill_in_field "Send me junk email", "unchecked"

Raises:



104
105
106
107
108
# File 'lib/kelp/field.rb', line 104

def fill_in_field(field, value, scope={})
  in_scope(scope) do
    check_or_select_or_fill(field, value)
  end
end

#fill_in_field_within(selector, field, value) ⇒ Object

Fill in a single fields within the scope of a given selector. Alias for:

fill_in_field field, value, :within => selector



151
152
153
# File 'lib/kelp/field.rb', line 151

def fill_in_field_within(selector, field, value)
  fill_in_field field, value, :within => selector
end

#fill_in_fields(field_values, scope = {}) ⇒ Object

Fill in multiple fields according to values in a Hash. Fields may be text boxes, dropdowns/listboxes, or checkboxes. See #check_or_select_or_fill for details.

Examples:

fill_in_fields "First name" => "Otto", "Last name" => "Scratchansniff"
fill_in_fields "phone" => "303-224-7428", :within => "#home"

Raises:



127
128
129
130
131
132
133
# File 'lib/kelp/field.rb', line 127

def fill_in_fields(field_values, scope={})
  in_scope(scope) do
    field_values.each do |field, value|
      check_or_select_or_fill(field, value)
    end
  end
end

#fill_in_fields_within(selector, fields) ⇒ Object

Fill in multiple fields within the scope of a given selector. Alias for:

fill_in_fields fields, :within => selector



141
142
143
# File 'lib/kelp/field.rb', line 141

def fill_in_fields_within(selector, fields)
  fill_in_fields fields, :within => selector
end

#select_or_fill(field, value) ⇒ Object

Select a value from a dropdown or listbox, or fill in a text field, depending on what kind of form field is available. If field is a dropdown or listbox, select value from that; otherwise, assume field is a text box.

Raises:

Since:

  • 0.1.9



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kelp/field.rb', line 32

def select_or_fill(field, value)
  case field_type(field)
  when :select
    begin
      select(value, :from => field)
    rescue Capybara::ElementNotFound
      raise Kelp::OptionNotFound,
        "Field '#{field}' has no option '#{value}'"
    end
  when :fillable_field
    fill_in(field, :with => value)
  end
end