【问题标题】:How to select an option from a select drop-down list with Hound?如何使用 Hound 从选择下拉列表中选择一个选项?
【发布时间】:2018-09-26 11:00:39
【问题描述】:

我有以下下拉列表:

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

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

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

【问题讨论】:

    标签: testing elixir integration-testing hound


    【解决方案1】:

    目前没有专门用于从下拉列表中选择元素的 Hound 函数。

    但是,您可以使用find_element/3 找到与您要选择的选项值对应的元素,然后将此元素提供给click/1 以选择它:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-11
      • 2017-12-03
      • 1970-01-01
      • 2023-03-28
      • 2017-10-17
      • 2017-05-19
      相关资源
      最近更新 更多