Module: CapybaraExtensions::Matchers

Includes:
Locators
Defined in:
lib/capybara-extensions/matchers.rb

Instance Method Summary collapse

Methods included from Locators

#image_locator, #meta_tag_locator

Instance Method Details

#has_field_value?(locator, text) ⇒ Boolean

Checks that the value of a field matches a given value. Typically, you’ll want to scope this to a form.

Parameters:

  • locator (String)

    the label, name, or id of the field.

  • text (String)

    the text to match against the field value.

Returns:

  • (Boolean)

    true if the field value matches.



32
33
34
35
36
37
38
# File 'lib/capybara-extensions/matchers.rb', line 32

def has_field_value?(locator, text)
  if find_field(locator).value == text
    true
  else
    raise Capybara::ExpectationNotMet, "expected to find field #{locator} with a value of #{text}."
  end
end

#has_image?(options = {}) ⇒ Boolean

Checks that the current node has an image with the given src or alt.

Parameters:

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

    must pass a hash containing src and/or alt to match against. You may pass a Regexp with src.

Returns:

  • (Boolean)

    true if the image matches.



11
12
13
14
# File 'lib/capybara-extensions/matchers.rb', line 11

def has_image?(options = {})
  raise "Must pass a hash containing 'src' or 'alt'" unless options.is_a?(Hash) && (options.has_key?(:src) || options.has_key?(:alt))
  has_selector?(:xpath, "//img#{image_locator(options)}")
end

#has_meta_tag?(name, content) ⇒ Boolean

Checks the that the content of a meta tag matches a given value.

Parameters:

  • name (String)

    the name attribute of the meta tag.

  • content (String)

    the value of the content attribute to match against.

Returns:

  • (Boolean)

    true if the meta tag content matches the name.



60
61
62
# File 'lib/capybara-extensions/matchers.rb', line 60

def has_meta_tag?(name, content)
  has_selector?(:xpath, "/html/head/meta#{meta_tag_locator(name, content)}", visible: false)
end

#has_no_field_value?(locator, text) ⇒ Boolean

Checks that the value of a field does not match a given value. Typically, you’ll want to scope this to a form.

Parameters:

  • locator (String)

    the label, name, or id of the field.

  • text (String)

    the text to match against the field value.

Returns:

  • (Boolean)

    true if the field value does not match.



46
47
48
49
50
51
52
# File 'lib/capybara-extensions/matchers.rb', line 46

def has_no_field_value?(locator, text)
  if find_field(locator).value != text
    true
  else
    raise Capybara::ExpectationNotMet, "expected to not find field #{locator} with a value of #{text}."
  end
end

#has_no_image?(options = {}) ⇒ Boolean

Checks that the current node does not have an image with the given src or alt.

Parameters:

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

    must pass a hash containing src and/or alt to match against.

Returns:

  • (Boolean)

    true if the image does not match.



21
22
23
24
# File 'lib/capybara-extensions/matchers.rb', line 21

def has_no_image?(options = {})
  raise "Must pass a hash with 'alt' or 'src'" unless options.is_a?(Hash) and (options.has_key?(:alt) or options.has_key?(:src))
  has_no_selector?(:xpath, "//img#{image_locator(options)}")
end

#has_no_meta_tag?(name, content) ⇒ Boolean

Checks the that the content of a meta tag does not match a given value.

Parameters:

  • name (String)

    the name attribute of the meta tag.

  • content (String)

    the value of the content attribute to match against.

Returns:

  • (Boolean)

    true if the meta tag content does not match the name.



70
71
72
# File 'lib/capybara-extensions/matchers.rb', line 70

def has_no_meta_tag?(name, content)
  has_no_selector?(:xpath, "/html/head/meta#{meta_tag_locator(name, content)}", visible: false)
end