【问题标题】:What is the correct way to get selected option text using site_prism?使用 site_prism 获取选定选项文本的正确方法是什么?
【发布时间】:2016-01-08 15:17:40
【问题描述】:

我有一个指向选择框的site_prism 元素。像这样:

class MyPageObject < SitePrism::Page
  element :my_select_box, '#select-box-id'
end

虽然我有办法获取选定的选项值,但是:

my_page_object.my_select_box.value

我找不到获取所选选项文本的好方法。我发现的唯一解决方法是:

my_page_object.my_select_box.find("option[selected]").text

SitePrism API 有没有更好的方法来做到这一点?因为上述解决方法混合使用了 SitePrism 和 capybara API,这对我来说似乎并不理想。

【问题讨论】:

    标签: ruby capybara site-prism


    【解决方案1】:

    我从未这样做过,但一种方法可能是将 :my_select_box 定义为一个部分,然后在该部分下访问所选元素

    class SelectSection < SitePrism::Section
      element :selected, 'option[selected]'
    end
    
    class MyPageObject < SitePrism::Page
      section :my_select_box, SelectSection, '#select-box-id'
    end
    

    应该让你访问

    my_page_object.my_select_box.selected.text
    

    然而,一个很好的问题是您为什么要访问文本 - 如果是因为您想根据已知文本验证所选项目的文本,您最好使用 Capybaras 选择器实际将元素声明为选择,所以您可以使用内置的查找器选项

    class MyPageObject < SitePrism::Page
      element :my_select_box, :select, 'select-box-id' # since it's now using Capybaras :select selector it's no longer a css selector so no # in front
    end
    

    然后应该让你这样做

    expect(my_page_object).to have_my_select_box(selected: 'the text expected to be selected')
    

    【讨论】:

    • 您知道我在哪里可以找到有关可用“Capybaras 选择器”的文档,例如,您在上面提到的 :select,但是例如链接的选择器是什么,它是 :a 还是 :链接还是别的?当您使用 SitePrism 方法检查值时,我从未见过(选择:'文本...)。是否有更多文档记录了此“已选择:”或其他值,例如单选按钮等。
    • @mickael 最好的地方是查看提供的选择器的 Capybara 源代码 - 从github.com/teamcapybara/capybara/blob/master/lib/capybara/…开始
    猜你喜欢
    • 2015-10-17
    • 2010-11-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多