以下代码行从下拉列表中正确选择“Joe Bloggs”:
browser.select_list(:id => "ListOwnerID").option(:text => /Joe Bloggs/).select # edited: added '.select'
如何将所有者的名称作为变量“list_owner”传递?
类似的东西:
def set_list_owner(list_owner)
browser.select_list(:id => "ListOwnerID").option(:text => /list_owner/).select
end
用法:
set_list_owner("Joe Bloggs")
答案 0 :(得分:3)
您可以使用Regexp::new
:
re_string = '\d'
Regexp.new(re_string) =~ 'abc 123'
# => 4
替代Cary Swoveland建议(正则表达式插值):
/#{re_string}/
def set_list_owner(list_owner)
browser.select_list(:id => "ListOwnerID").option(:text => Regexp.new(list_owner))
end
set_list_owner("Joe Bloggs")
如果你想要字面上匹配字符串,而不是解释为正则表达式,请使用Regexp::escape
:
Regexp.new(Regexp.escape(list_owner))