Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ruby测试单元中使用verify_Ruby_Unit Testing_Selenium_Webdriver - Fatal编程技术网

如何在ruby测试单元中使用verify

如何在ruby测试单元中使用verify,ruby,unit-testing,selenium,webdriver,Ruby,Unit Testing,Selenium,Webdriver,所以我总是用perl编写脚本 但是尝试使用Ruby,所以这个问题听起来可能很愚蠢,但我会尝试一下 我正在用ruby Test::Unit编写Selenium Webdriver脚本 下面是一些示例代码 def test_runit @driver.get(@base_url + "/") @driver.find_element(:id, "gbqfq").clear @driver.find_element(:id, "gbqfq").send_keys "selen

所以我总是用perl编写脚本 但是尝试使用Ruby,所以这个问题听起来可能很愚蠢,但我会尝试一下

我正在用ruby Test::Unit编写Selenium Webdriver脚本 下面是一些示例代码

 def test_runit
    @driver.get(@base_url + "/")
    @driver.find_element(:id, "gbqfq").clear
    @driver.find_element(:id, "gbqfq").send_keys "selenium"
    @driver.find_element(:id, "gbqfb").click
    @driver.find_element(:link, "Selenium - Web Browser Automation").click
    assert(@driver.find_element(:tag_name => "body").text.include?("administration tasks"),"the assert works")
    @driver.find_element(:link, "Support").click
  end
有输出

 ruby runit.rb 
Loaded suite runit
Started
.
Finished in 9.732149 seconds.

1 tests, 2 assertions, 0 failures, 0 errors
测试时,我需要检查页面上是否有文本。 使用
assert
效果很好。 但是如果失败了,测试就到此结束,不再继续

在perl中,我可以为您提供一些类似于
验证
的东西,它基本上会将某些东西标记为失败并继续进行

我希望得到这样的结果

ruby runit.rb 
Loaded suite runit
Started
.
Finished in 9.732149 seconds.

1 tests, 1 assertion, 1 failure, 0 errors
但verify不适用于ruby测试单元,或者可能是我做错了

有人能给我指出一些示例代码吗?
谢谢

我没有听说Ruby有类似于perl的“verify”函数,因为“assert”表示如果为false,程序会立即停止

我建议您将代码重新实现为,并给它一个“零防护”:

明白了!! 人们需要使用救援。 谢谢你的回复

begin
    assert(@driver.find_element(:tag_name => "body").text.include?("text to check"),"the assert works")
rescue Test::Unit::AssertionFailedError
    @verification_errors << $!
end
开始
assert(@driver.find_元素(:tag_name=>“body”).text.include?(“要检查的文本”),“assert有效”)
救援测试::单元::断言失败错误

@验证错误是的,确实如此。你需要营救指挥部

但是,您可以在测试助手中轻松创建类似于此的验证方法

 def verify
   $verification_errors = [] if $verification_errors.nil?
   assert yield
 rescue Test::Unit::AssertionFailedError => afe
   $verification_errors << afe
   puts "ERROR: #{afe.message}"
 end
测试结束时,在拆卸过程中,您需要检查是否存在任何验证错误:

 assert $verification_errors.empty? or $verification_errors.nil?

问题是body元素始终存在,并且检查文本“administrationtasks”。但除了程序停止问题仍然没有解决,因为断言如果失败将立即停止脚本。要稍微改变问题的过程,如何更新测试结果中的“失败”方面?一旦“断言”失败,测试用例将停止,这是单元测试的方式。我想:)。顺便说一句,你可以看看“确保”。
 verify{ @driver.find_element(:tag_name => "body").text.include?("foobar") }
 assert $verification_errors.empty? or $verification_errors.nil?