Class: Cucumber::Salad::Widgets::FieldGroup

Inherits:
Widget
  • Object
show all
Defined in:
lib/cucumber/salad/widgets/field_group.rb

Overview

TODO:

Explain how to use locators when defining fields, including what happens when locators are omitted.

A group of form fields.

Direct Known Subclasses

Form

Defined Under Namespace

Classes: CheckBox, Field, Select, TextField

Instance Attribute Summary

Attributes inherited from Widget

#root

Field definition macros collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Widget

find_in, #has_action?, #initialize, #inspect, present_in?, #reload, root, selector, #to_s

Methods included from Cucumber::Salad::WidgetMacros

#action, #widget, #widget_delegator

Methods included from Cucumber::Salad::WidgetContainer

#has_widget?, #widget

Constructor Details

This class inherits a constructor from Cucumber::Salad::Widgets::Widget

Class Method Details

.check_box(name, locator = nil) ⇒ Object

TODO:

Handle checkbox access when the field is disabled (raise an exception?)

Creates a new checkbox accessor.

Adds the following methods to the widget:

<name>

Gets the current checkbox state, as a boolean. Returns true if the corresponding check box is checked, false otherwise.

<name>=

Sets the current checkbox state. Pass true to check the checkbox, false otherwise.

Examples:

# Given the following HTML:
#
# <form>
#   <p>
#     <label for="checked-box">
#     <input type="checkbox" value="1" id="checked-box" checked>
#   </p>
#   <p>
#     <label for="unchecked-box">
#     <input type="checkbox" value="1" id="unchecked-box">
#   </p>
# </form>
class MyFieldGroup < Cucumber::Salad::Widgets::FieldGroup
  root 'form'

  check_box :checked_box, 'checked-box'
  check_box :unchecked_box, 'unchecked-box'
end

form = widget(:my_field_group)

form.checked_box          #=> true
form.unchecked_box        #=> false

form.unchecked_box = true
form.unchecked_box        #=> true

Parameters:

  • name

    the name of the checkbox accessor.

  • locator (defaults to: nil)

    the locator for the checkbox. If nil the locator will be derived from name.



74
75
76
# File 'lib/cucumber/salad/widgets/field_group.rb', line 74

def self.check_box(name, locator = nil)
  field name, locator || name_to_locator(name), CheckBox
end

.default_locator(type = nil, &block) ⇒ Object



11
12
13
14
15
# File 'lib/cucumber/salad/widgets/field_group.rb', line 11

def self.default_locator(type = nil, &block)
  alias_method :name_to_locator, type if type

  define_method :name_to_locator, &block if block
end

.field(name, locator, type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines a new field.

Parameters:

  • name

    the name of the field accessor.

  • locator

    the field locator.

  • type

    the field class name.

Raises:

  • (TypeError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cucumber/salad/widgets/field_group.rb', line 85

def self.field(name, locator, type)
  raise TypeError, "can't convert `#{name}' to Symbol" \
    unless name.respond_to?(:to_sym)

  field_names << name.to_sym

  widget name, locator, type do
    define_method :label do
      name.to_s.gsub(/_/, ' ').capitalize
    end
  end

  define_method "#{name}=" do |val|
    widget(name).set val
  end

  define_method name do
    widget(name).get
  end
end

.field_namesSet

The names of all the fields that belong to this field group.

Field names are automatically added to this group as long as you use the field definition macros.

Returns:

  • (Set)

    the field names.

See Also:



25
26
27
# File 'lib/cucumber/salad/widgets/field_group.rb', line 25

def self.field_names
  @field_names ||= Set.new
end

.select(name, locator = nil) ⇒ Object

TODO:

Handle select access when the field is disabled (raise an exception?)

TODO:

Raise an exception when an option doesn’t exist.

TODO:

Allow passing the option value to set an option.

TODO:

Ensure an option with no text returns the empty string.

TODO:

What to do when nil is passed to the writer?

Creates a new select accessor.

Adds the following methods to the widget:

<name>

Gets the current selected option. Returns the label of the selected option, or nil, if no option is selected.

<name>=

Selects an option on the current select. Pass the label of the option you want to select.

Examples:

# Given the following HTML:
#
# <form>
#   <p>
#     <label for="selected">
#     <select id="selected">
#       <option selected>Selected option</option>
#       <option>Another option</option>
#     </select>
#   </p>
#   <p>
#     <label for="deselected">
#     <select id="deselected">
#       <option>Deselected option</option>
#       <option>Another option</option>
#     </select>
#   </p>
# </form>
class MyFieldGroup < Cucumber::Salad::Widgets::FieldGroup
  root 'form'

  select :selected, 'selected'
  select :deselected, 'deselected'
end

form = widget(:my_field_group)

form.selected                         #=> "Selected option"
form.deselected                       #=> nil

form.deselected = "Deselected option"
form.unchecked_box                    #=> "Deselected option"

Parameters:

  • name

    the name of the select accessor.

  • locator (defaults to: nil)

    the locator for the select. If nil the locator will be derived from name.



159
160
161
# File 'lib/cucumber/salad/widgets/field_group.rb', line 159

def self.select(name, locator = nil)
  field name, locator || name_to_locator(name), Select
end

.text_field(name, locator = nil) ⇒ Object

TODO:

Handle text field access when the field is disabled (raise an exception?)

Creates a new text field accessor.

Adds the following methods to the widget:

<name>

Returns the current text field value, or nil if no value has been set.

<name>=

Sets the current text field value.

Examples:

# Given the following HTML:
#
# <form>
#   <p>
#     <label for="text-field">
#     <input type="text" value="Content" id="text-field">
#   </p>
#   <p>
#     <label for="empty-field">
#     <input type="text" id="empty-field">
#   </p>
# </form>
class MyFieldGroup < Cucumber::Salad::Widgets::FieldGroup
  root 'form'

  text_field :filled_field, 'text-field'
  text_field :empty_field, 'empty-field'
end

form = widget(:my_field_group)

form.filled_field                #=> "Content"
form.empty_field                 #=> nil

form.empty_field = "Not anymore"
form.empty_field                 #=> "Not anymore"

Parameters:

  • name

    the name of the text field accessor.

  • locator (defaults to: nil)

    the locator for the text field. If nil the locator will be derived from name.



205
206
207
# File 'lib/cucumber/salad/widgets/field_group.rb', line 205

def self.text_field(name, locator = nil)
  field name, locator || name_to_locator(name), TextField
end

Instance Method Details

#fieldsObject

Returns This field group’s field widgets.

Returns:

  • This field group’s field widgets.



212
213
214
# File 'lib/cucumber/salad/widgets/field_group.rb', line 212

def fields
  self.class.field_names.map { |name| widget(name) }
end

#set(attributes) ⇒ Object

Sets the given form attributes.

Parameters:

  • attributes (Hash)

    the attributes and values we want to set.

Returns:

  • the current widget.



221
222
223
224
225
226
227
# File 'lib/cucumber/salad/widgets/field_group.rb', line 221

def set(attributes)
  attributes.each do |k, v|
    send "#{k}=", v
  end

  self
end

#to_tableArray<Array>

Converts the current field group into a table suitable for diff’ing with Cucumber::Ast::Table.

Field labels are determined by the widget name.

Field values correspond to the return value of each field’s to_s.

Returns:

  • (Array<Array>)

    the table.



237
238
239
240
241
242
# File 'lib/cucumber/salad/widgets/field_group.rb', line 237

def to_table
  headers = fields.map { |field| field.label.downcase }
  body    = fields.map { |field| field.to_s.downcase }

  [headers, body]
end