[英]watir webdriver using a variable
我在浏览器参考中使用变量时遇到问题。 我试图传递诸如ul,ol等字段类型。如果我输入ol,代码可以工作,但我想使用标记为'field'的传递变量。 我收到了“未定义的字段方法”错误。
我也试过使用#{field},但这也不起作用。 错误是该字段未定义。
def CheckNewPageNoUl(browser, field, text1, output)
browser.a(:id => 'submitbtn').hover
browser.a(:id => 'submitbtn').click
output.puts("text1 #{text1}")
browser.body(:id => 'page').wait_until_present
if browser.table.field.exists?
output.puts(" #{text1} was found in CheckNewPageNoUl")
end
end
field = "ol"
text1 = "<ol>"
CheckText.CheckNewPageNoUl(b, field, text1, outputt)
要将字符串转换为方法调用,请使用Object#send
,它可以使用两个参数:
一些例子:
field = 'ol'
browser.send(field).exists?
#=> Translates to browser.ol.exists?
specifiers = {:text => 'text', :class => 'class'}
browser.send(field, specifiers).exists?
#=> Translates to browser.ol(:text => 'text', :class => 'class').exists?
对于您的代码,您可能希望:
if browser.table.send(field).exists?
output.puts(" #{text1} was found in CheckNewPageNoUl")
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.