2

我有一个指向选择框的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,这对我来说似乎并不理想。

4

1 回答 1

4

我从来没有这样做过,但一种方法可能是将 :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')
于 2016-01-08T22:50:28.323 回答