3
<select class="business_group" multiple="multiple" name="SelectedBusinessGroups">
  <option value="Partners">Partners</option>
  <option value="Press">Press</option>
  <option value="ProductToolbox">Product Toolbox</option>
  <option selected="selected" value="Promotional">Promotional</option>
  <option value="Sponsors">Sponsors</option>
</select>

As the attribute named :selected mean that option is clicked. I want to check whether "Promotional" is selected or not from the list of options. How can I do that?

I tried

assert @browser.option(:text => "Promotional").attribute_value("selected").exists? == true

but it's not working.

1
  • 1
    That is a standard HTML selection list, using the standard element types for such, so you can use the watir methods for selection lists, which includes a way to ask an option if it is selected or not. See @JustinKo's answer Commented Apr 23, 2013 at 18:21

1 Answer 1

4

You have a couple of options to check the selected option.

Using Option#selected?

Options have a built-in method for telling you if they are selected - see Option#selected?.

@browser.option(:text => "Promotional").selected?
#=> true

@browser.option(:text => "Press").selected?
#=> false

Using Select#selected?

Select lists have a built-in method for check if an options is selected - Select#selected?. Note that this only checks the options based on their text.

ddl = @browser.select(:class => 'business_group')

ddl.selected?('Promotional')
#=> true

ddl.selected?('Press')
#=> false

Using Select#selected_options

The Select#selected_options method will return a collection of options that are selected. You iterate over this collection to see if the option you want is included. This allows you to check an option by more than its text.

selected = @browser.select(:class => 'business_group').selected_options
selected.any?{ |o| o.text == 'Promotional' }
#=> true

Using Element#attribute_value

The attribute_value method will return the attribute value as a string if the attribute exists. If the attribute does not exist, nil is returned.

#Compare the attribute value
@browser.option(:text => "Promotional").attribute_value("selected") == 'true'
#=> true

#Compare the attribute value presence
@browser.option(:text => "Promotional").attribute_value("selected").nil? == false
#=> true
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.