How to get text of selected option from select list without [ ] brackets

881 views Asked by At

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?

3

There are 3 answers

2
Surya On BEST ANSWER

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
1
Cheezy On

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.

0
shilpa reddy On

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