testingelixirintegration-testinghound

How to select an option from a select drop-down list with Hound?


I have the following drop-down list:

<select id="cities" name="cities">
  <option value="paris">Paris</option>
  <option value="london">London</option>
  <option value="rome">Rome</option>
</select>

I am writing integration tests in Elixir with Hound and I'd like to select an element from the list above before submitting my form. Can I do that with Hound?

I couldn't find anything about drop-down lists in the Hound documentation.


Solution

  • Currently there is no Hound function dedicated to selecting an element from a drop-down list.

    However, you can use find_element/3 to find the element that corresponds to the option value you want to select, then feed this element to click/1 to select it:

    find_element(:css, "#cities option[value='london']") |> click()
    

    More information on this GitHub issue


    Implementation example

    defmodule CustomHelpers.Hound do
      use Hound.Helpers
    
      def select_drop_down(drop_down_id, option) do
        find_element(:css, "##{drop_down_id} option[value='#{option}']") |> click()
      end
    
      def select_drop_down_within(element, drop_down, option) do
        find_within_element(element, :css, "##{drop_down_id} option[value='#{option}']") |> click()
      end
    end