Module: Watir::Container

Defined in:
lib/watir-formhandler/container.rb

Constant Summary collapse

FORM_FIELDS =
[Input, Select, TextArea]

Instance Method Summary collapse

Instance Method Details

#field(label, start_node: nil, placeholder: false, id: false) ⇒ Watir::Element

Searches for the specified label and returns the form field belonging to it, identified by the ‘for’ attribute of the label. Alternatively, you may pass a Watir::Label.

Parameters:

  • label (String, Watir::Label)

    the label for which to find the form field.

  • start_node (Watir::Element) (defaults to: nil)

    the node where to start searching for the label.

  • placeholder (Boolean) (defaults to: false)

    whether to handle label as Watir::Label or as placeholder attribute for an input field.

  • id (Boolean) (defaults to: false)

    assumes the given label is an HTML ID and searches for it.

Returns:

  • (Watir::Element)

    form field of the given label. If the field is no form field, it will be assumed to be an OptionGroup.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/watir-formhandler/container.rb', line 17

def field(label, start_node: nil, placeholder: false, id: false)
  start_node ||= self

  if placeholder
    start_node.element(placeholder: label).to_subtype
  elsif id
    start_node.element(id: label).to_subtype
  else
    field_label = label.respond_to?(:for) ? label : start_node.label(text: label)
    determine_field(start_node, field_label)
  end
end

#fill_in(label, value, start_node: nil, placeholder: false, id: false) ⇒ Object

Fills in the given value(s) to the passed attribute. It therefore accepts the same parameters as the #field method.

Parameters:

  • label (String, Watir::Label)

    the label for which to find the form field.

  • value (String, Boolean, Array)

    to be set.

  • start_node (Watir::Element) (defaults to: nil)

    the node where to start searching for the label.

  • placeholder (Boolean) (defaults to: false)

    whether to handle label as Watir::Label or as placeholder attribute for an input field.

  • id (Boolean) (defaults to: false)

    assumes the given label is an HTML ID and searches for it.



39
40
41
42
43
44
45
# File 'lib/watir-formhandler/container.rb', line 39

def fill_in(label, value, start_node: nil, placeholder: false, id: false)
  field(label,
        start_node:     start_node,
        placeholder:    placeholder,
        id:             id
  ).set(value)
end

#option_group(*args) ⇒ OptionGroup

Returns an OptionGroup

Returns:



67
68
69
70
71
72
73
74
# File 'lib/watir-formhandler/container.rb', line 67

def option_group(*args)
  selector = if args.first.respond_to?(:elements)
               args.first
             else
               extract_selector(args)
             end
  OptionGroup.new(self, selector)
end

#value_of(label, start_node: nil, placeholder: false, id: false) ⇒ String, ...

Returns the current value of the specified form field. It therefore accepts the same parameters as the #field method.

Parameters:

  • label (String, Watir::Label)

    the label for which to find the form field.

  • start_node (Watir::Element) (defaults to: nil)

    the node where to start searching for the label.

  • placeholder (Boolean) (defaults to: false)

    whether to handle label as Watir::Label or as placeholder attribute for an input field.

  • id (Boolean) (defaults to: false)

    assumes the given label is an HTML ID and searches for it.

Returns:

  • (String, Boolean, Array)

    current value of the field.



56
57
58
59
60
61
62
# File 'lib/watir-formhandler/container.rb', line 56

def value_of(label, start_node: nil, placeholder: false, id: false)
  field(label,
        start_node:     start_node,
        placeholder:    placeholder,
        id:             id
  ).field_value
end