Module: Kelp::Field
Overview
This module defines helper methods for filling in and verifying the content of fields in a web form.
Instance Method Summary (collapse)
-
- (Object) check_or_select_or_fill(field, value)
Check a checkbox, select from a dropdown or listbox, or fill in a text field, depending on what kind of form field is available.
-
- (Object) field_should_be_empty(field, scope = {})
Verify that the given field is empty or nil.
-
- (Object) field_should_contain(field, value, scope = {})
Verify that the given field contains the given value.
-
- (Object) field_should_contain_within(selector, field, value)
Verify a single field within the scope of a given selector.
-
- (Object) field_should_not_contain(field, value, scope = {})
Verify that the given field does not contain the given value.
-
- (String) field_value(field)
Return the string value found in the given field.
-
- (Object) fields_should_contain(field_values, scope = {})
Verify the values of multiple fields given as a Hash.
-
- (Object) fields_should_contain_within(selector, field_values)
Verify fields within the scope of a given selector.
-
- (Object) fill_in_field(field, value, scope = {})
Fill in a single field within the scope of a given selector.
-
- (Object) fill_in_field_within(selector, field, value)
Fill in a single fields within the scope of a given selector.
-
- (Object) fill_in_fields(field_values, scope = {})
Fill in multiple fields according to values in a
Hash. -
- (Object) fill_in_fields_within(selector, fields)
Fill in multiple fields within the scope of a given selector.
-
- (Object) select_or_fill(field, value)
Select a value from a dropdown or listbox, or fill in a text field, depending on what kind of form field is available.
Methods included from Helper
#listify, #nice_find_field, #rspec_unexpected
Methods included from Scoping
#in_scope, #kelp_within, #scope_within
Instance Method Details
- (Object) check_or_select_or_fill(field, value)
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.
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 |
- (Object) field_should_be_empty(field, scope = {})
Verify that the given field is empty or nil.
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 == '') raise Kelp::Unexpected, "Expected field '#{field}' to be empty, but value is '#{_field.value}'" end end end |
- (Object) field_should_contain(field, value, scope = {})
Verify that the given field contains the given value.
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 |
- (Object) field_should_contain_within(selector, field, value)
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 |
- (Object) field_should_not_contain(field, value, scope = {})
Verify that the given field does not contain the given value.
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 |
- (String) field_value(field)
Return the string value found in the given field.
If the field is nil, return the empty string.
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 |
- (Object) fields_should_contain(field_values, scope = {})
Verify the values of multiple fields given as a Hash.
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.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 |
- (Object) fields_should_contain_within(selector, field_values)
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 |
- (Object) fill_in_field(field, value, scope = {})
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.
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 |
- (Object) fill_in_field_within(selector, field, value)
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 |
- (Object) fill_in_fields(field_values, scope = {})
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.
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 |
- (Object) fill_in_fields_within(selector, fields)
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 |
- (Object) select_or_fill(field, value)
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.
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 |