Question

I have a page object that has a list item of locations.

select_list(:locations, :id => 'locations)

And I would like to have a list of locations and then select one of them. Something like:

def select_item_different_than d_item    
 list_items = :locations.items #This is wrong, but you get the point
 list_items.each do |item|
   if item != d_item
     item.select
     return
   end
 end
end

Thanks a lot :)

Was it helpful?

Solution

The select list elements have an options method that returns an array of its option elements. You could iterate over the array and compare them to the d_item.

The method could be:

def select_item_different_than d_item    
  list_items = locations_element.options
  list_items.each do |item|
    if item.text != d_item
      item.click
      return
    end
  end
end 

Note that the following changes were required:

  1. The select list element is retrieved by locations_element instead of :locations.
  2. The list of options is retrieved by options instead of items.
  3. In the if statement item != d_item was changed to item.text != d_item. My assumption is that you want to compare the option's text and d_item is a string.
  4. Option elements do not have a select method. Instead, use the click method.

Personally, I think the method might be more clear as:

def select_item_different_than d_item    
  locations_element
    .options
    .find{ |option| option.text != d_item }
    .click
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top