简体   繁体   中英

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 .

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

However, you can usefind_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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM