我刚刚开始考虑如何使用"test/unit"
. 我复制了 Selenium IDE 生成的代码并将其粘贴到我的 Ruby 测试方法中。
但是当使用 Ruby.exe 运行它时,由于某种原因它会抛出一个错误:
Finished tests in 31.835891s, 0.0314 tests/s, 0.0942 assertions/s.
1) Error:
test_method(MyTestClass):
NameError: uninitialized constant Test::Unit::AssertionFailedError
teste-noticia.rb:30:in `rescue in verify'
teste-noticia.rb:29:in `verify'
teste-noticia.rb:42:in `test_method'
1 tests, 3 assertions, 0 failures, 1 errors, 0 skips
任何人都可以帮助我如何正确断言所需的字符串?欢迎任何好的做法;-)。
这是代码:
# encoding: utf-8
require "selenium-webdriver"
require "test/unit"
class MyTestClass < Test::Unit::TestCase
def setup
@driver = Selenium::WebDriver.for :firefox
@base_url = "http://www.yoursite.com"
@driver.manage.timeouts.implicit_wait = 30
@verification_errors = []
@wait = Selenium::WebDriver::Wait.new :timeout => 10
end
def teardown
@driver.quit
assert_equal [], @verification_errors
end
def element_present?(how, what)
@driver.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
@verification_errors << ex
end
#your test methods go here
def test_method
@driver.get(@base_url + "/my-desired-path")
verify { assert_equal "Obama wins and will move U.S. forward", @driver.find_element(:css, "h1").text }
end
end
编辑
我的本地宝石:
C:\Users\wmj>gem list
*** LOCAL GEMS ***
addressable (2.3.2)
bigdecimal (1.1.0)
childprocess (0.3.6)
ffi (1.1.5 x86-mingw32)
io-console (0.3)
json (1.5.4)
libwebsocket (0.1.5)
minitest (2.5.1)
multi_json (1.3.7)
rake (0.9.2.2)
rdoc (3.9.4)
rubyzip (0.9.9)
selenium-webdriver (2.26.0)
test-unit (2.5.2)