Ruby 使用变量的watir webdriver
在浏览器引用中使用变量时遇到问题。我正在尝试传递字段类型,如ul、ol等。如果我键入ol,代码会工作,但我希望使用标记为“field”的pass变量。我收到一个“字段的未定义方法”错误 我也尝试过使用#{field},但也不起作用。错误是未定义字段Ruby 使用变量的watir webdriver,ruby,webdriver,watir,Ruby,Webdriver,Watir,在浏览器引用中使用变量时遇到问题。我正在尝试传递字段类型,如ul、ol等。如果我键入ol,代码会工作,但我希望使用标记为“field”的pass变量。我收到一个“字段的未定义方法”错误 我也尝试过使用#{field},但也不起作用。错误是未定义字段 def CheckNewPageNoUl(browser, field, text1, output) browser.a(:id => 'submitbtn').hover browser.a(:id => 'submitbt
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)
def CheckNewPageNoUl(浏览器、字段、文本1、输出)
browser.a(:id=>'submitbtn')。悬停
browser.a(:id=>submitbtn')。单击
put(“text1{text1}”)
body(:id=>'page')。请等待\u出现
是否存在browser.table.field?
put(“#{text1}在CheckNewPageNoUl中找到”)
结束
结束
field=“ol”
text1=“”
CheckText.CheckNewPageNoUl(b,字段,文本1,输出)
要将字符串转换为方法调用,请使用,它可以接受两个参数:
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
在代码示例中,
字段
变量是一个字符串。浏览器对象需要一个类为Watir::OList
(或Watir classic中的Watir::Ol
)的方法。我知道该字段是字符串,但为什么不在代码行中将其转换为“Ol”,以便将其解释为“browser.table.Ol.exists?”我希望对ul、Ol、li使用该方法,通过传递一个变量,而不是生成多个方法。谢谢你的帮助。