Module: Kelp::Visibility

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

Overview

This module defines methods for verifying the visibility (or invisibility) of elements on a web page.

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

#page_contains?(text_or_regexp) ⇒ Boolean

Return true if the current page contains the given text or regular expression, or false if it does not.

Raises:

  • (ArgumentError)

    If the given argument isn't a String or Regexp

Since:

  • 0.2.0


26
27
28
29
30
31
32
33
34
# File 'lib/kelp/visibility.rb', line 26

def page_contains?(text_or_regexp)
  if text_or_regexp.class == String
    return page.has_content?(text_or_regexp)
  elsif text_or_regexp.class == Regexp
    return page.has_xpath?('.//*', :text => text_or_regexp)
  else
    raise ArgumentError, "Expected String or Regexp, got #{text_or_regexp.class}"
  end
end

#page_should_contain(text_or_regexp) ⇒ Object

Deprecated.

Use #should_see instead

Ensure that the current page content includes a String or Regexp.


214
215
216
217
218
219
# File 'lib/kelp/visibility.rb', line 214

def page_should_contain(text_or_regexp)
  warn "WARNING: page_should_contain is deprecated. Use should_see instead."
  if !page_contains?(text_or_regexp)
    raise Kelp::Unexpected
  end
end

#page_should_contain_regexp(regexp) ⇒ Object

Deprecated.

Use #should_see instead

Ensure that the current page content matches a Regexp.


262
263
264
265
266
267
# File 'lib/kelp/visibility.rb', line 262

def page_should_contain_regexp(regexp)
  warn "WARNING: page_should_contain_regexp is deprecated. Use should_see instead."
  if !page.has_xpath?('.//*', :text => regexp)
    raise Kelp::Unexpected
  end
end

#page_should_contain_text(text) ⇒ Object

Deprecated.

Use #should_see instead

Ensure that the current page content includes a String.


246
247
248
249
250
251
# File 'lib/kelp/visibility.rb', line 246

def page_should_contain_text(text)
  warn "WARNING: page_should_contain_text is deprecated. Use should_see instead."
  if !page.has_content?(text)
    raise Kelp::Unexpected
  end
end

#page_should_not_contain(text_or_regexp) ⇒ Object

Deprecated.

Use #should_not_see instead

Ensure that the current page content does not include a String or Regexp.


230
231
232
233
234
235
# File 'lib/kelp/visibility.rb', line 230

def page_should_not_contain(text_or_regexp)
  warn "WARNING: page_should_not_contain is deprecated. Use should_not_see instead."
  if page_contains?(text_or_regexp)
    raise Kelp::Unexpected
  end
end

#page_should_not_contain_regexp(regexp) ⇒ Object

Deprecated.

Use #should_not_see instead

Ensure that the current page content does not match a Regexp.


294
295
296
297
298
299
# File 'lib/kelp/visibility.rb', line 294

def page_should_not_contain_regexp(regexp)
  warn "WARNING: page_should_not_contain_regexp is deprecated. Use should_not_see instead."
  if page.has_xpath?('.//*', :text => regexp)
    raise Kelp::Unexpected
  end
end

#page_should_not_contain_text(text) ⇒ Object

Deprecated.

Use #should_not_see instead

Ensure that the current page content does not include a String.


278
279
280
281
282
283
# File 'lib/kelp/visibility.rb', line 278

def page_should_not_contain_text(text)
  warn "WARNING: page_should_not_contain_text is deprecated. Use should_not_see instead."
  if page.has_content?(text)
    raise Kelp::Unexpected
  end
end

#should_not_see(texts, scope = {}) ⇒ Object

Verify the absence of content on the page. Passes when none of the given items are found on the page, and fails if any of them are found.

Raises:

  • (Kelp::Unexpected)

    If any of the expected text strings are seen in the given scope


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kelp/visibility.rb', line 80

def should_not_see(texts, scope={})
  in_scope(scope) do
    texts = [texts] if (texts.class == String || texts.class == Regexp)
    # Select all unexpected values that do appear on the page
    unexpected = texts.select do |text|
      page_contains?(text)
    end
    if !unexpected.empty?
      raise Kelp::Unexpected,
        "Expected not to see: #{texts.inspect}\nDid see: #{unexpected.inspect}"
    end
  end
end

#should_not_see_button(button_text, scope = {}) ⇒ Object

Verify that a button with the given text does not appear on the page. Works for <input...> as well as <button...> elements.

Examples:

should_not_see_button "Delete"
should_not_see_button "Save changes", :within => "#read_only"

Raises:

  • (Kelp::Unexpected)

    If a button with the given text does exist in the given scope

Since:

  • 0.1.9


190
191
192
193
194
195
196
197
# File 'lib/kelp/visibility.rb', line 190

def should_not_see_button(button_text, scope={})
  in_scope(scope) do
    xpath = XPath::HTML.button(button_text)[~ XPath.attr(:disabled)]
    if page.has_xpath?(xpath)
      raise Kelp::Unexpected, "Did not expect to see button '#{button_text}', but button exists."
    end
  end
end

#should_not_see_in_same_row(texts, scope = {}) ⇒ Object

Verify that all items do not appear in the same table row. Passes if there is no tr containing all the given texts, and fails if there exists such a tr.

Examples:

should_not_see_in_same_row ["Pinky", "Maurice LaMarche"]
should_not_see_in_same_row ["Brain", "Rob Paulsen"]

Raises:


137
138
139
140
141
142
143
# File 'lib/kelp/visibility.rb', line 137

def should_not_see_in_same_row(texts, scope={})
  in_scope(scope) do
    if page.has_xpath?(xpath_row_containing(texts))
      raise Kelp::Unexpected, "Did not expect, but did see: #{texts.inspect} in the same row"
    end
  end
end

#should_see(texts, scope = {}) ⇒ Object

Verify the presence of content on the page. Passes when all the given items are found on the page, and fails if any of them are not found.

Examples:

should_see "Animaniacs"
should_see ["Yakko", "Wakko", "Dot"]
should_see "Baloney", :within => "#slacks"
should_see /(Animaney|Totally Insaney|Pinky and the Brainy)/

Raises:

  • (Kelp::Unexpected)

    If any of the expected text strings are not seen in the given scope


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kelp/visibility.rb', line 54

def should_see(texts, scope={})
  in_scope(scope) do
    texts = [texts] if (texts.class == String || texts.class == Regexp)
    # Select all expected values that don't appear on the page
    unexpected = texts.select do |text|
      !page_contains?(text)
    end
    if !unexpected.empty?
      raise Kelp::Unexpected,
        "Expected to see: #{texts.inspect}\nDid not see: #{unexpected.inspect}"
    end
  end
end

#should_see_button(button_text, scope = {}) ⇒ Object

Verify that a button with the given text appears on the page. Works for <input...> as well as <button...> elements.

Examples:

should_see_button "Submit"
should_see_button "Save changes", :within => "#preferences"

Raises:

  • (Kelp::Unexpected)

    If no button with the given text exists in the given scope

Since:

  • 0.1.9


163
164
165
166
167
168
169
170
# File 'lib/kelp/visibility.rb', line 163

def should_see_button(button_text, scope={})
  in_scope(scope) do
    xpath = XPath::HTML.button(button_text)
    if !page.has_xpath?(xpath)
      raise Kelp::Unexpected, "Expected to see button '#{button_text}', but button does not exist."
    end
  end
end

#should_see_in_same_row(texts, scope = {}) ⇒ Object

Verify that all items appear in the same table row. Passes if a tr element exists containing all the given texts, and fails if no such tr exists. The texts may be in any order in the row.

Examples:

should_see_in_same_row ["Yakko", "Rob Paulsen"]
should_see_in_same_row ["Wakko", "Jess Harnell"]
should_see_in_same_row ["Dot", "Tress MacNeille"]

Raises:


112
113
114
115
116
117
118
# File 'lib/kelp/visibility.rb', line 112

def should_see_in_same_row(texts, scope={})
  in_scope(scope) do
    if !page.has_xpath?(xpath_row_containing(texts))
      raise Kelp::Unexpected, "Expected, but did not see: #{texts.inspect} in the same row"
    end
  end
end