使用变量的watir webdriver

时间:2013-02-01 04:15:07

标签: ruby webdriver watir

我在浏览器参考中使用变量时遇到问题。我试图传递诸如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)

1 个答案:

答案 0 :(得分:3)

要将字符串转换为方法调用,请使用Object#send,它可以使用两个参数:

  1. 方法名称(字符串或符号)
  2. 方法的参数(可选)
  3. 一些例子:

    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
    
相关问题