我使用next命令获取选择列表中所选选项的文本:
@sltedStudy=page.select_list_element(:id => 'cntrStudy', :frame => frame).selected_options(&:text)
但是当我尝试比较值时:
expect(page.cell_element(:xpath => "//td[.='Lorem Ipsum']/following-sibling::td[1]", :frame => frame).text).to be == @sltedStudy
它返回:
RSpec::Expectations::ExpectationNotMetError: expected: == ["SLC"]
got: "SLC"
如何在没有[]括号的情况下从选择列表中获取文本值?
答案 0 :(得分:2)
selected_options
方法返回Array
个Option
个对象。呼叫(&:text)
与呼叫相同:
selected_options.collect { |opt| opt.text }
这将返回包含文本的新Array
。所以@sltedStudy
是一个数组。当您将其与cell_element
来电中的文字进行比较时,只需返回String
。在您的匹配器中,您要将Array
与String
进行比较。您可以更改为使用include
匹配器,或者只需将数组的第一个元素视为Surya。
另一方面,我认为你采取的方法实际上是在破坏使用页面对象的整个目的。您似乎在您出现的调用中在页面对象之外公开实现细节。我强烈建议封装元素类型的知识以及页面内部的路径。
答案 1 :(得分:0)
看起来像这一行:
@sltedStudy = page.select_list_element(:id => 'cntrStudy', :frame => frame).selected_options(&:text)
从所选选项中提供一系列文字:["SLC"]
尝试将您的rspec更改为:
expect(page.cell_element(:xpath => "//td[.='Lorem Ipsum']/following-sibling::td[1]", :frame => frame).text).to be == @sltedStudy.first
或者这个:
expect([page.cell_element(:xpath => "//td[.='Lorem Ipsum']/following-sibling::td[1]", :frame => frame).text]).to be == @sltedStudy
答案 2 :(得分:-1)
尝试像这样使用: current_month_element.selected_options [0] .text