如何从没有[]括号的选择列表中获取所选选项的文本

时间:2014-12-29 09:23:43

标签: ruby watir watir-webdriver page-object-gem rspec-expectations

我使用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"

如何在没有[]括号的情况下从选择列表中获取文本值?

3 个答案:

答案 0 :(得分:2)

selected_options方法返回ArrayOption个对象。呼叫(&:text)与呼叫相同:

selected_options.collect { |opt| opt.text }

这将返回包含文本的新Array。所以@sltedStudy是一个数组。当您将其与cell_element来电中的文字进行比较时,只需返回String。在您的匹配器中,您要将ArrayString进行比较。您可以更改为使用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

相关问题