Ruby Watir 在运行类之外找不到断言方法?

发布于 2024-10-14 00:55:26 字数 1110 浏览 6 评论 0原文

我有一个想要在许多测试用例中使用的类:

require 'rubygems'
require 'test/unit'
require 'watir'

class Tests < Test::Unit::TestCase
  def self.Run(browser)
    #  make sure Summary of Changes exists
    assert( browser.table(:class, "summary_table_class").exists? )
    # make sure Snapshot of Change Areas exists
    assert( browser.image(:xpath, "//div[@id='report_chart_div']/img").exists?  )
    # make sure Integrated Changes table exists
    assert( browser.table(:id, 'change_table_html').exists? )
  end
end

但是,当在我的一个测试用例中运行时:

require 'rubygems'
require 'test/unit'
require 'watir'
require 'configuration'
require 'Tests'

class TwoSCMCrossBranch < Test::Unit::TestCase
  def test_two_scm_cross_branch
    test_site = Constants.whatsInUrl
    puts " Step 1: go to the test site: " + test_site
    ie = Watir::IE.start(test_site)

    Tests.Run(ie)

  end
end

我收到错误:

NoMethodError: undefined method `assert' for Tests:Class
    C:/p4/dev/webToolKit/test/webapps/WhatsIn/ruby-tests/Tests.rb:8:in `Run'

缺少什么?谢谢!

I have a class that I want to use in many test cases:

require 'rubygems'
require 'test/unit'
require 'watir'

class Tests < Test::Unit::TestCase
  def self.Run(browser)
    #  make sure Summary of Changes exists
    assert( browser.table(:class, "summary_table_class").exists? )
    # make sure Snapshot of Change Areas exists
    assert( browser.image(:xpath, "//div[@id='report_chart_div']/img").exists?  )
    # make sure Integrated Changes table exists
    assert( browser.table(:id, 'change_table_html').exists? )
  end
end

However, when run in one of my test cases:

require 'rubygems'
require 'test/unit'
require 'watir'
require 'configuration'
require 'Tests'

class TwoSCMCrossBranch < Test::Unit::TestCase
  def test_two_scm_cross_branch
    test_site = Constants.whatsInUrl
    puts " Step 1: go to the test site: " + test_site
    ie = Watir::IE.start(test_site)

    Tests.Run(ie)

  end
end

I get the error:

NoMethodError: undefined method `assert' for Tests:Class
    C:/p4/dev/webToolKit/test/webapps/WhatsIn/ruby-tests/Tests.rb:8:in `Run'

What's missing? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两人的回忆 2024-10-21 00:55:26

assert() 是 TestCase 上的实例方法,因此仅适用于测试实例。您在类方法中调用它,因此 Ruby 正在测试中寻找不存在的类方法。

更好的方法是将 Tests 设为模块,将 Run 方法设为实例方法:

module Tests
  def Run(browser)
    ...
  end
end

然后将 Tests 模块包含在测试类中:

class TwoSCMCrossBranch < Test::Unit::TestCase
  include Tests

  def test_two_scm_cross_branch
    test_site = Constants.whatsInUrl
    puts " Step 1: go to the test site: " + test_site
    ie = Watir::IE.start(test_site)

    Run(ie)
  end
end

这将使 Run 方法可用于测试,并且 Run() 将找到 assert()测试类中的方法。

assert() is an instance method on TestCase so would only be available to instances of Tests. You are calling it inside a class method so Ruby is looking for a class method in Tests which doesn't exist.

A better way to do this is to make Tests a module and the Run method an instance method:

module Tests
  def Run(browser)
    ...
  end
end

Then include the Tests module in your test class:

class TwoSCMCrossBranch < Test::Unit::TestCase
  include Tests

  def test_two_scm_cross_branch
    test_site = Constants.whatsInUrl
    puts " Step 1: go to the test site: " + test_site
    ie = Watir::IE.start(test_site)

    Run(ie)
  end
end

which will make the Run method available to the test and Run() will find the assert() method in the test class.

魂归处 2024-10-21 00:55:26

可能值得尝试将断言全部删除,然后仅使用.exists?

It might be worth a try to remove the asserts all together, and just use .exists?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文