2

I have a site_prism element that points to a select box. Like this:

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

Although I have a way to get the selected option value, with this:

my_page_object.my_select_box.value

I cannot find a nice way to get the selected option text. The only workaround that I have found is this:

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

Is there a better way to do that with SitePrism API? Because the above workaround uses a mix of SitePrism and capybara API, which does not seem to be ideal to me.

p.matsinopoulos
  • 7,655
  • 6
  • 44
  • 92

1 Answers1

4

I've never done this, but one way would probably be to define :my_select_box as a section and then have the selected element accessed under that

class SelectSection < SitePrism::Section
  element :selected, 'option[selected]'
end

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

which should let you access

my_page_object.my_select_box.selected.text

A good question however is why you want to access the text - If it's because you want to verify the text of the selected item against a known text, you're better off actually declaring the element as a select using Capybaras selectors so that you can can use the builtin finder options

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

which then should let you do

expect(my_page_object).to have_my_select_box(selected: 'the text expected to be selected')
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • do you know where I can find documentation about the available 'Capybaras selectors', eg, you mentioned above :select, but what is the selector for a link for example, is it :a, or :link or something else? And when you check the value with SitePrism methods, I had never seen (selected: 'the text ...). Is there more documentation where this 'selected:' or other values, for example for radio buttons, etc is documented. – mickael Dec 01 '16 at 00:28
  • @mickael best place is to look at the Capybara source for the provided selectors - starting at https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L67 – Thomas Walpole Dec 01 '16 at 03:51