0

I use next command to get a text of selected option in select list:

@sltedStudy=page.select_list_element(:id => 'cntrStudy', :frame => frame).selected_options(&:text)

But when I try to compare values:

expect(page.cell_element(:xpath => "//td[.='Lorem Ipsum']/following-sibling::td[1]", :frame => frame).text).to be == @sltedStudy

it returns:

RSpec::Expectations::ExpectationNotMetError: expected: == ["SLC"]
     got:    "SLC"

How I can get a value of text from select list without [ ] brackets?

Den Silver
  • 199
  • 16

3 Answers3

3

The selected_options method returns an Array of Option objects. Calling (&:text) is the same as calling:

selected_options.collect { |opt| opt.text }

This will return a new Array that contains the text. So @sltedStudy is an array. When you are comparing it to the text in your call to cell_element it is simply returning a String. In your matcher you are comparing an Array to a String. You could change to use the include matcher or you could simply look at the first element of the array as Surya.

On a different note, I think the approach you are taking is actually defeating the entire purpose of using page objects. You seem to be exposing implementation details outside of the page object in the calls you present. I would strongly suggest encapsulating knowledge of the type of elements as well as the path inside of the pages.

Cheezy
  • 789
  • 3
  • 6
  • Oh, hi Jeff :) Thank you for your explanation. I just forgot that selected_options returns an Array. I wonder, can I assign only one value of selected option in case if allowed only one selection? Because in my case I do not need all array of selection options? – Den Silver Dec 30 '14 at 10:04
1

It looks like this line:

@sltedStudy = page.select_list_element(:id => 'cntrStudy', :frame => frame).selected_options(&:text)

gives an array of text from selected options: ["SLC"]

try changing your rspec to this:

expect(page.cell_element(:xpath => "//td[.='Lorem Ipsum']/following-sibling::td[1]", :frame => frame).text).to be == @sltedStudy.first

or this:

expect([page.cell_element(:xpath => "//td[.='Lorem Ipsum']/following-sibling::td[1]", :frame => frame).text]).to be == @sltedStudy
Surya
  • 15,703
  • 3
  • 51
  • 74
  • Array, yes, of course. I forgot that selected_options returns an array of selected options :) Thank you. – Den Silver Dec 29 '14 at 09:51
  • This answer may have worked for the OP, but I would *strongly* suggest NOT using this answer due to the reasons Cheezy points out below. – Jared Darling Jan 05 '15 at 20:36
-1

try using like this: current_month_element.selected_options[0].text