Module: Kelp::Dropdown

Includes:
Helper, Scoping
Defined in:
lib/kelp/dropdown.rb

Overview

This module defines methods for working with dropdown fields in a web form, including verifying their visible content as well as the value attribute of selected options.

Instance Method Summary (collapse)

Methods included from Helper

#listify, #nice_find_field, #rspec_unexpected

Methods included from Scoping

#in_scope, #kelp_within, #scope_within

Instance Method Details

Verify that the selected option in a dropdown has the given value. Note that this is the visible content of the dropdown (the content of the

Parameters:

  • dropdown (String)

    Capybara locator for the dropdown (the select element)

  • value (String)

    Value you expect to see as the currently-selected option

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

    Scoping keywords as understood by Scoping#in_scope

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kelp/dropdown.rb', line 28

def dropdown_should_equal(dropdown, value, scope={})
  in_scope(scope) do
    field = nice_find_field(dropdown)
    # See if there's a 'selected' option
    begin
      selected = field.find(:xpath, ".//option[@selected='selected']")
    # If not, find the option matching the field's 'value' attribute
    rescue Capybara::ElementNotFound
      # FIXME: Find a way to support multiple-selection dropdowns
      if field.value.class == Array
        raise NotImplementedError, "Multiple-selection dropdowns are not supported"
      end
      field_value = xpath_sanitize(field.value)
      selected = field.find(:xpath, ".//option[@value=#{field_value}]")
    end
    if selected.text != value
      raise Kelp::Unexpected,
        "Expected '#{dropdown}' dropdown to equal '#{value}'\nGot '#{selected.text}' instead"
    end
  end
end

Verify that a given dropdown includes all of the given strings. This looks for the visible values in the dropdown, not the 'value' attribute of each option.

Examples:

dropdown_should_include "Weekday", "Monday"
dropdown_should_include "Size", ["Small", "Medium", "Large"]

Parameters:

  • dropdown (String)

    Capybara locator for the dropdown (the select element)

  • values (Array)

    Visible contents you expect to be able to select from the dropdown

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

    Scoping keywords as understood by Scoping#in_scope

Raises:



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

def dropdown_should_include(dropdown, values, scope={})
  in_scope(scope) do
    # If values is a String, convert it to an Array
    values = [values] if values.class == String
    field = nice_find_field(dropdown)
    # Select all expected values that don't appear in the dropdown
    unexpected = values.select do |value|
      !page.has_xpath?(".//option[text()=#{xpath_sanitize(value)}]")
    end
    if !unexpected.empty?
      raise Kelp::Unexpected,
        "Expected '#{dropdown}' dropdown to include: #{values.inspect}" + \
        "\nMissing: #{unexpected.inspect}"
    end
  end
end

Verify that a given dropdown does not include any of the given strings. This looks for the visible values in the dropdown, not the 'value' attribute of each option.

Parameters:

  • dropdown (String)

    Capybara locator for the dropdown (the select element)

  • values (Array)

    Visible contents you do not want to see in the dropdown

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

    Scoping keywords as understood by Scoping#in_scope

Raises:

Since:

  • 0.1.2



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/kelp/dropdown.rb', line 103

def dropdown_should_not_include(dropdown, values, scope={})
  in_scope(scope) do
    # If values is a String, convert it to an Array
    values = [values] if values.class == String
    field = nice_find_field(dropdown)
    # Select all not-expected values that do appear in the dropdown
    unexpected = values.select do |value|
      page.has_xpath?(".//option[text()=#{xpath_sanitize(value)}]")
    end
    if !unexpected.empty?
      raise Kelp::Unexpected,
        "Expected '#{dropdown}' dropdown to not include: #{values.inspect}" + \
        "\nDid include: #{unexpected.inspect}"
    end
  end
end

Verify that a dropdown currently has the option with the given value attribute selected. Note that this differs from #dropdown_should_equal, in that it looks at the actual value attribute of the selected option, rather than its visible contents.

Parameters:

  • dropdown (String)

    Capybara locator for the dropdown (the select element)

  • value (String)

    Expected value attribute of the selected option

Raises:

  • (Kelp::Unexpected)

    If the given dropdown's 'value' attribute is not equal to value



134
135
136
137
138
139
140
141
# File 'lib/kelp/dropdown.rb', line 134

def dropdown_value_should_equal(dropdown, value)
  field = find_field(dropdown)
  if field.value != value
    raise Kelp::Unexpected,
      "Expected '#{dropdown}' dropdown's value to equal '#{value}'" + \
      "\nGot '#{field.value}' instead"
  end
end