nil的未定义方法`contains_text':NilClass(NoMethodError)

时间:2012-07-10 08:37:08

标签: ruby watir

我在RadRails IDE中使用Watir。我创建了一个类,用于检查我的网页上是否存在某些文本:

class Text

  def initialize(text, object)
    @text   = text
    @object = object

    if $ie.contains_text(@text)
      puts "Test for" + @object + "failed"
      puts $ie.link(:text => /Exception:/)

      h = $ie.link(:text => /Exception:/)
      r.addtoReport(testReport, "check" + @object, "FAILED", h.text)
    else
      puts("Test for" + @object + "passed")

      r.addtoReport(
          testReport, 
          "check" + @object, 
          "PASSED", 
          "Test for" + @object + "passed"
      )
    end
  end

end

但是当我尝试在我的测试中执行代码时,我收到以下错误 -

BPM/Company.rb:38:in `initialize': undefined method `contains_text' for nil:NilClass (NoMethodError)
from BPM/Company.rb:79:in `new'
from BPM/Company.rb:79:in `<main>'

我通过这个命令调用该类 -

Text.new("Menu", "login")

代码更改 -

def initialize(text, object, ie)
    @text   = text
    @object = object
    @ie=ie
    if @ie.contains_text(@text)
      puts "Test for" + @object + "failed"
      puts @ie.link(:text => /Exception:/)

我通过这个命令调用该类 -

Text_pos.new("Terms", "login",$ie)

在描述类之前,即初始化ie=Watir::IE.new

1 个答案:

答案 0 :(得分:1)

$ ie来自哪里? - 看来你没有初始化ie:

ie = Watir::IE.new