Module: WatirRobot::Select

Included in:
KeywordLibrary
Defined in:
lib/watir_robot/keywords/select.rb

Overview

Functionality related to select list form elements

Instance Method Summary collapse

Instance Method Details

#clear_items_from_list(loc) ⇒ Object

Note:

The underlying Watir-WebDriver code will raise an error if the

Clear all select options from a drop-down select list

select list is not a multiple select list

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element



51
52
53
# File 'lib/watir_robot/keywords/select.rb', line 51

def clear_items_from_list(loc)
  @browser.select(parse_location(loc)).clear
end

#item_should_be_selected(list_loc, item_loc) ⇒ Object

Ensure that an item is selected

Parameters:

  • list_loc (String)

    attribute/value pairs that match a select list

  • item_loc (String)

    attribute/value pairs that match an item in a select list

Raises:



64
65
66
67
# File 'lib/watir_robot/keywords/select.rb', line 64

def item_should_be_selected(list_loc, item_loc)
  raise(Exception::SelectListSelectionError, "The item described by #{item_loc} in the select list described by #{list_loc} is not selected.") unless
    @browser.select(parse_location(list_loc)).option(parse_location(item_loc)).selected?
end

#item_should_not_be_selected(list_loc, item_loc) ⇒ Object

Ensure that an item is not selected

Parameters:

  • list_loc (String)

    attribute/value pairs that match a select list

  • item_loc (String)

    attribute/value pairs that match an item in a select list

Raises:



75
76
77
78
# File 'lib/watir_robot/keywords/select.rb', line 75

def item_should_not_be_selected(list_loc, item_loc)
  raise(Exception::SelectListSelectionError, "The item described by #{item_loc} in the select list described by #{list_loc} is selected erroneously.") if
    @browser.select(parse_location(list_loc)).option(parse_location(item_loc)).selected?
end

#select_all_items_from_list(loc) ⇒ Object

Select all items from a drop-down select list

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element



36
37
38
39
40
41
# File 'lib/watir_robot/keywords/select.rb', line 36

def select_all_items_from_list(loc)
  select_list = @browser.select(parse_location(loc))
  select_list.options.each do |item|
    select_list.select_value(item.value) # Using value because it's more likely to be programmatically ensured unique
  end
end

#select_item_from_list(list_loc, item_loc) ⇒ Object

Select a single item from a drop-down select list

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

  • item (String)

    the label, text or value of an item to select

  • by_value (String)

    boolean, whether or not to select an element by visible text or by value attribute (as string because arg comes from Robot Framework)



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/watir_robot/keywords/select.rb', line 17

def select_item_from_list(list_loc, item_loc)
  # Give user option to select by value rather than by text of option or label
  # Since the text is immediately visible, I assume people will default to that
  if item_loc[0..4].downcase == 'text='
    item_loc = item_loc[5..item_loc.length]
    @browser.select(parse_location(list_loc)).select(item_loc)
  elsif item_loc[0..5].downcase == 'value='
    item_loc = item_loc[6..item_loc.length]
    @browser.select(parse_location(list_loc)).select_value(item_loc)
  else
    raise(ArgumentError, "The only attributes allowed are 'text' and 'value'")
  end
end