Class: Cucumber::Salad::Widgets::Widget

Inherits:
Object
  • Object
show all
Extended by:
Cucumber::Salad::WidgetMacros, Forwardable
Includes:
Cucumber::Salad::WidgetContainer
Defined in:
lib/cucumber/salad/widgets/widget.rb

Defined Under Namespace

Classes: Reload

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cucumber::Salad::WidgetMacros

action, widget, widget_delegator

Methods included from Cucumber::Salad::WidgetContainer

#has_widget?, #widget

Constructor Details

#initialize(settings = {}) ⇒ Widget

Returns a new instance of Widget.



48
49
50
# File 'lib/cucumber/salad/widgets/widget.rb', line 48

def initialize(settings = {})
  self.root = settings.fetch(:root)
end

Instance Attribute Details

#rootObject

Returns The root node of the current widget.

Returns:

  • The root node of the current widget



44
45
46
# File 'lib/cucumber/salad/widgets/widget.rb', line 44

def root
  @root
end

Class Method Details

.find_in(node, options = {}) ⇒ Object

Finds a single instance of the current widget in node.

Parameters:

  • node

    the node we want to search in

Returns:

  • a new instance of the current widget class.

Raises:

  • (Capybara::ElementNotFoundError)

    if the widget can’t be found



27
28
29
# File 'lib/cucumber/salad/widgets/widget.rb', line 27

def self.find_in(node, options = {})
  new(options.merge(root: node.find(selector)))
end

.present_in?(parent_node) ⇒ Boolean

Determines if an instance of this widget class exists in parent_node.

Parameters:

  • parent_node (Capybara::Node)

    the node we want to search in

Returns:

  • (Boolean)

    true if a widget instance is found, false otherwise.



16
17
18
# File 'lib/cucumber/salad/widgets/widget.rb', line 16

def self.present_in?(parent_node)
  parent_node.has_selector?(selector)
end

.root(selector) ⇒ Object

Sets this widget’s default selector.

Parameters:

  • selector (String)

    a CSS or XPath query



34
35
36
# File 'lib/cucumber/salad/widgets/widget.rb', line 34

def self.root(selector)
  @selector = selector
end

.selectorObject

Returns The selector specified with root.

Returns:

  • The selector specified with root.



39
40
41
# File 'lib/cucumber/salad/widgets/widget.rb', line 39

def self.selector
  @selector
end

Instance Method Details

#has_action?(name) ⇒ Boolean

Determines if the widget underlying an action exists.

Parameters:

  • name

    the name of the action

Returns:

  • (Boolean)

    true if the action widget is found, false otherwise.

Raises:

  • Missing if an action with name can’t be found.



60
61
62
63
64
# File 'lib/cucumber/salad/widgets/widget.rb', line 60

def has_action?(name)
  raise Missing, "couldn't find `#{name}' action" unless respond_to?(name)

  has_widget?(name)
end

#inspectObject



66
67
68
69
70
71
# File 'lib/cucumber/salad/widgets/widget.rb', line 66

def inspect
  xml = Nokogiri::HTML(page.body).at(root.path).to_xml

  "<!-- #{self.class.name}: -->\n" <<
   Nokogiri::XML(xml, &:noblanks).to_xhtml
end

#reload(wait_time = Capybara.default_wait_time) { ... } ⇒ Object

Reloads the widget, waiting for its contents to change (by default), or until wait_time expires.

Call this method to make sure a widget has enough time to update itself.

You can pass a block to this method to control what it means for the widget to be reloaded.

*Note: does not account for multiple changes to the widget yet.*

Parameters:

  • wait_time (Numeric) (defaults to: Capybara.default_wait_time)

    how long we should wait for changes, in seconds.

Yields:

  • A block that determines what it means for a widget to be reloaded.

Yield Returns:

  • (Boolean)

    true if the widget is considered to be reloaded, false otherwise.

Returns:

  • the current widget



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cucumber/salad/widgets/widget.rb', line 95

def reload(wait_time = Capybara.default_wait_time, &test)
  unless test
    old_inspect = inspect
    test        = ->{ old_inspect != inspect }
  end

  root.synchronize(wait_time) do
    raise Reload unless test.()
  end

  self
rescue Reload
  # raised on timeout

  self
end

#to_sObject



112
113
114
# File 'lib/cucumber/salad/widgets/widget.rb', line 112

def to_s
  node_text(root)
end