Module: Cucumber::Salad::WidgetMacros

Included in:
Cucumber::Salad::Widgets::Widget
Defined in:
lib/cucumber/salad/widget_macros.rb

Instance Method Summary collapse

Instance Method Details

#action(name, selector) ⇒ Object

Defines a new action.

This is a shortcut to help defining a widget and a method that clicks on that widget. You can then send a widget instance the message given by name.

Examples:

# Consider the widget will encapsulate the following HTML
#
# <div id="profile">
#  <a href="/profiles/1/edit" rel="edit">Edit</a>
# </div>
class PirateProfile < Salad::Widget
  root "#profile"

  # Declare the action
  action :edit, '[rel = edit]'
end

# Click the link
widget(:pirate_profile).edit

Parameters:

  • name

    the name of the action

  • selector

    the selector for the widget that will be clicked



28
29
30
31
32
33
34
35
36
# File 'lib/cucumber/salad/widget_macros.rb', line 28

def action(name, selector)
  widget name, selector

  define_method name do
    widget(name).click

    self
  end
end

#widget(name, selector, parent = Widgets::Widget) { ... } ⇒ Object

Declares a new sub-widget.

Sub-widgets are accessible inside the container widget using the widget message.

Parameters:

  • name

    the name of the sub-widget

  • selector

    the sub-widget selector

  • parent (Class) (defaults to: Widgets::Widget)

    the parent class of the new sub-widget

Yields:

  • A block allowing you to further customize the widget behavior.

See Also:



50
51
52
53
54
55
56
57
58
# File 'lib/cucumber/salad/widget_macros.rb', line 50

def widget(name, selector, parent = Widgets::Widget, &block)
  type = Class.new(parent) {
    root selector

    instance_eval(&block) if block
  }

  const_set(Salad::WidgetName.new(name).to_sym, type)
end

#widget_delegator(name, widget_message, method_name = nil) ⇒ Object

Creates a delegator for one sub-widget message.

Since widgets are accessed through Cucumber::Salad::WidgetContainer#widget, we can’t use Forwardable to delegate messages to widgets.

Parameters:

  • name

    the name of the receiver sub-widget

  • widget_message

    the name of the message to be sent to the sub-widget

  • method_name (defaults to: nil)

    the name of the delegator. If nil the method will have the same name as the message it will send.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cucumber/salad/widget_macros.rb', line 69

def widget_delegator(name, widget_message, method_name = nil)
  method_name = method_name || widget_message

  class_eval <<-RUBY
    def #{method_name}(*args)
      if args.size == 1
        widget(:#{name}).#{widget_message} args.first
      else
        widget(:#{name}).#{widget_message} *args
      end
    end
  RUBY
end