Module: Kelp::Scoping

Included in:
Checkbox, Dropdown, Field, Navigation, Visibility
Defined in:
lib/kelp/scoping.rb

Overview

This module defines helper methods for restricting the scope of actions or verifications on a webpage.

Instance Method Summary collapse

Instance Method Details

#in_scope(scope) ⇒ Object

Execute a block of code inside a given scope. scope must be a Hash of parameters that describes the context in which to execute the block.

Examples:

in_scope :within => '#footer'
  click_link "Contact Us"
end

Parameters:

  • scope (Hash)

    Context in which to execute the block

Options Hash (scope):

  • :within (String)

    Selector string



54
55
56
57
58
# File 'lib/kelp/scoping.rb', line 54

def in_scope(scope)
  scope_within(scope[:within]) do
    yield
  end
end

#kelp_within(kind, selector = nil) ⇒ Object

Wrapper for Capybara's within, but raises Kelp::InvalidScope instead of Capybara::ElementNotFound



8
9
10
11
12
13
14
15
# File 'lib/kelp/scoping.rb', line 8

def kelp_within(kind, selector=nil)
  begin
    within(kind, selector) { yield }
  rescue Capybara::ElementNotFound
    raise Kelp::InvalidScope,
      "Scope '#{selector || kind}' not found on page"
  end
end

#scope_within(locator) ⇒ Object

Execute a block of code within a given scope. If locator begins with / or ./, then it's assumed to be an XPath selector; otherwise, it's assumed to be a CSS selector.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kelp/scoping.rb', line 21

def scope_within(locator)
  if locator
    # Use the selector_for method if it's defined. This may be provided
    # by features/support/selectors.rb, generated by cucumber-rails.
    if defined? selector_for
      kelp_within(*selector_for(locator)) { yield }
    # Otherwise, fall back on the Capybara locator syntax (CSS or XPath)
    else
      if locator =~ /\.?\//
        kelp_within(:xpath, locator) { yield }
      else
        kelp_within(:css, locator) { yield }
      end
    end
  else
    yield
  end
end