The Issue

There are multiple ways to go about selecting from a dropdown list; some will work better than others depending on a variety of factors. Let’s take a look at an efficient way to select from a dropdown using Ruby & Watir-Webdriver.

The Answer

A common solution is the select command, allowing us to select directly select through a list in one line of code. This will appear as follows:

Alternatively, you can click on the list itself in order to pull up the dropdown, and then attempt to click your desired option after. This would appear as such:

This allows you to interact with the selected dropdown. To validate that we see the correct options, we can use if logic:

You can apply these assertions as many times as you like to cover just a few options, or validate every selection from a list. These are particularly useful when the list options will change based on an outside variable.

The Code

Lets look at two examples: using the select command to select from a dropdown, and using click on the list, and selecting a different option by using click on the option. We’ll use the Watir Webdriver Demo page: bit.ly/watir-webdriver-demo.

The Result

We can save the file (dropdown1.rb) and run it from the command line. Here is a breakdown of the result:

  • Browser opens
  • Browser navigates to the Watir Webdriver Demo URL
  • Selects Ruby from the dropdown by using click the dropdown, then use click on Ruby
  • Selects Java directly from dropdown using select_list

The Takeaway

You’re now set to take dropdown lists from two different approaches. Practice using both approaches to see how they interact with dropdowns in your test automation.