如何使用 Hound select 从 select 下拉列表中选择一个选项?

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

我有以下下拉列表:

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

我正在使用 HoundElixir 中编写集成测试,我想在提交表单之前 select 上面列表中的一个元素。我可以用 Hound 做吗?

我在 Hound documentation 中找不到任何关于下拉列表的信息。

目前没有专用于 select 从下拉列表中获取元素的 Hound 函数。

不过,你可以用find_element/3 to find the element that corresponds to the option value you want to select, then feed this element to click/1到select吧:

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

More information on this GitHub issue


实现示例

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