class Cucumber::Salad::Widgets::Widget

Attributes

root[RW]

@return The root node of the current widget

Public Class Methods

find_in(node, options = {}) click to toggle source

Finds a single instance of the current widget in node.

@param node the node we want to search in

@return a new instance of the current widget class.

@raise [Capybara::ElementNotFoundError] if the widget can’t be found

# File lib/cucumber/salad/widgets/widget.rb, line 27
def self.find_in(node, options = {})
  new(options.merge(root: node.find(selector)))
end
new(settings = {}) click to toggle source
# File lib/cucumber/salad/widgets/widget.rb, line 48
def initialize(settings = {})
  self.root = settings.fetch(:root)
end
present_in?(parent_node) click to toggle source

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

@param parent_node [Capybara::Node] the node we want to search in

@return true if a widget instance is found, false otherwise.

# File lib/cucumber/salad/widgets/widget.rb, line 16
def self.present_in?(parent_node)
  parent_node.has_selector?(selector)
end
root(selector) click to toggle source

Sets this widget’s default selector.

@param selector [String] a CSS or XPath query

# File lib/cucumber/salad/widgets/widget.rb, line 34
def self.root(selector)
  @selector = selector
end
selector() click to toggle source

@return The selector specified with root.

# File lib/cucumber/salad/widgets/widget.rb, line 39
def self.selector
  @selector
end

Public Instance Methods

has_action?(name) click to toggle source

Determines if the widget underlying an action exists.

@param name the name of the action

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

@return [Boolean] true if the action widget is found, false

otherwise.
# 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
inspect() click to toggle source
# 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, &test) click to toggle source

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.*

@param wait_time [Numeric] how long we should wait for changes, in

seconds.

@yield A block that determines what it means for a widget to be

reloaded.

@yieldreturn [Boolean] true if the widget is considered to be

reloaded, +false+ otherwise.

@return the current widget

# 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_s() click to toggle source
# File lib/cucumber/salad/widgets/widget.rb, line 112
def to_s
  node_text(root)
end

Protected Instance Methods

node_text(node) click to toggle source
# File lib/cucumber/salad/widgets/widget.rb, line 120
def node_text(node)
  NodeText.new(node)
end

Private Instance Methods

page() click to toggle source
# File lib/cucumber/salad/widgets/widget.rb, line 128
def page
  Capybara.current_session
end