Module: Kelp::Navigation

Includes:
Scoping, XPaths
Defined in:
lib/kelp/navigation.rb

Overview

This module defines helper methods for navigating a webpage, including clicking links and pressing buttons, and for verifying the current page's path and query parameters.

Instance Method Summary collapse

Methods included from XPaths

#xpath_row_containing, #xpath_sanitize

Methods included from Scoping

#in_scope, #kelp_within, #scope_within

Instance Method Details

Click a link in a table row containing the given text.

Examples:

click_link_in_row "Edit", "Pinky"

Parameters:

  • link (String)

    Content of the link to click

  • text (String)

    Other content that must be in the same row



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kelp/navigation.rb', line 71

def click_link_in_row(link, text)
  begin
    row = find(:xpath, xpath_row_containing([link, text]))
  rescue Capybara::ElementNotFound
    raise Kelp::MissingRow,
      "No table row found containing '#{link}' and '#{text}'"
  end
  begin
    row.click_link(link)
  rescue Capybara::ElementNotFound
    raise Kelp::MissingLink,
      "No link with title, id or text '#{link}' found in the same row as '#{text}'"
  end
end

#follow(link, scope = {}) ⇒ Object

Follow a link on the page.

Examples:

follow "Login"
follow "Contact Us", :within => "#footer"

Parameters:

  • link (String)

    Capybara locator expression (id, name, or link text)

  • scope (Hash) (defaults to: {})

    Scoping keywords as understood by Scoping#in_scope



27
28
29
30
31
32
33
34
35
36
# File 'lib/kelp/navigation.rb', line 27

def follow(link, scope={})
  in_scope(scope) do
    begin
      click_link(link)
    rescue Capybara::ElementNotFound
      raise Kelp::MissingLink,
        "No link with title, id or text '#{link}' found"
    end
  end
end

#go_to_page(page_name_or_path) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/kelp/navigation.rb', line 87

def go_to_page(page_name_or_path)
  if defined? path_to
    visit path_to(page_name_or_path)
  else
    visit page_name_or_path
  end
end

#press(button, scope = {}) ⇒ Object

Press a button on the page.

Examples:

press "Cancel"
press "Submit", :within => "#survey"

Parameters:

  • button (String)

    Capybara locator expression (id, name, or button text)

  • scope (Hash) (defaults to: {})

    Scoping keywords as understood by Scoping#in_scope



49
50
51
52
53
54
55
56
57
58
# File 'lib/kelp/navigation.rb', line 49

def press(button, scope={})
  in_scope(scope) do
    begin
      click_button(button)
    rescue Capybara::ElementNotFound
      raise Kelp::MissingButton,
        "No button with value, id or text '#{button}' found"
    end
  end
end

#should_be_on_page(page_name_or_path) ⇒ Object

Verify that the current page matches the path of page_name_or_path. The cucumber-generated path_to function will be used, if it's defined, to translate human-readable names into URL paths. Otherwise, assume a raw, absolute path string.

Examples:

should_be_on_page 'home page'
should_be_on_page '/admin/login'

Parameters:

  • page_name_or_path (String)

    Human-readable page name (mapped to a pathname by your path_to function), or an absolute path beginning with /.

Raises:

Since:

  • 0.1.9



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kelp/navigation.rb', line 114

def should_be_on_page(page_name_or_path)
  # Use the path_to translator function if it's defined
  # (normally in features/support/paths.rb)
  if defined? path_to
    expect_path = path_to(page_name_or_path)
  # Otherwise, expect a raw path string
  else
    expect_path = page_name_or_path
  end
  actual_path = URI.parse(current_url).path
  if actual_path != expect_path
    raise Kelp::Unexpected,
      "Expected to be on page: '#{expect_path}'" + \
      "\nActually on page: '#{actual_path}'"
  end
end

#should_have_query(params) ⇒ Object

Verify that the current page has the given query parameters.

Parameters:

  • params (Hash)

    Key => value parameters, as they would appear in the URL

Raises:

  • (Kelp::Unexpected)

    If actual query parameters don't match expected parameters

Since:

  • 0.1.9



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/kelp/navigation.rb', line 142

def should_have_query(params)
  query = URI.parse(current_url).query
  actual_params = query ? CGI.parse(query) : {}
  expected_params = {}
  params.each_pair do |k,v|
    expected_params[k] = v.split(',')
  end
  if actual_params != expected_params
    raise Kelp::Unexpected,
      "Expected query params: '#{expected_params.inspect}'" + \
      "\nActual query params: '#{actual_params.inspect}'"
  end
end