
[英]How to get a a selected option from HTML <select> to a POST request in Sinatra
[英]How to get text of selected option from select list without [ ] brackets
我使用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"
如何从不带[]括号的选择列表中获取文本值?
selected_options
方法返回一个由Option
对象组成的Array
。 调用(&:text)
与调用相同:
selected_options.collect { |opt| opt.text }
这将返回一个包含文本的新Array
。 所以@sltedStudy
是一个数组。 当您将其与cell_element
调用中的文本进行比较时,它只是返回String
。 在匹配器中,您正在将Array
与String
进行比较。 您可以更改为使用include
匹配器,也可以简单地将数组的第一个元素视为Surya。
另一方面,我认为您所采用的方法实际上正在击败使用页面对象的全部目的。 您似乎在您提出的调用中在页面对象之外公开了实现细节。 我强烈建议封装有关元素类型以及页面内部路径的知识。
看起来像这行:
@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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.