简体   繁体   English

使用变量的watir webdriver

[英]watir webdriver using a variable

I am having trouble using a variable in browser reference. 我在浏览器参考中使用变量时遇到问题。 I am trying to pass the field type such as ul, ol etc. The code works if I type ol, but I would like to use the pass variable labeled 'field'. 我试图传递诸如ul,ol等字段类型。如果我输入ol,代码可以工作,但我想使用标记为'field'的传递变量。 I recieve an "undefined method for field" error. 我收到了“未定义的字段方法”错误。

I have tried also using #{field}, but that does not work either. 我也试过使用#{field},但这也不起作用。 The error is that field is not defined. 错误是该字段未定义。

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)

To translate a string into a method call, use Object#send , which can take two parameters: 要将字符串转换为方法调用,请使用Object#send ,它可以使用两个参数:

  1. The method name (as string or symbol) 方法名称(作为字符串或符号)
  2. Arguments for the method (optional) 方法的参数(可选)

Some examples: 一些例子:

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?

For your code, you would want to have: 对于您的代码,您可能希望:

if browser.table.send(field).exists?
  output.puts(" #{text1} was found in CheckNewPageNoUl")
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM