【问题标题】:how to export results when running selenium ruby webdriver scripts to output files from command prompt ruby window运行 selenium ruby​​ webdriver 脚本以从命令提示符 ruby​​ 窗口输出文件时如何导出结果
【发布时间】:2012-12-02 15:03:39
【问题描述】:

目前,我在“使用 Ruby 启动命令提示符”终端中使用 rake gem 一次性运行我的测试套件(由 Selenium Ruby Webdriver 编写)中的所有 selenium 脚本。

为此,我必须创建一个名为“rakefile.rb”的文件,其内容如下,并在我的终端中调用“rake”:(我已经根据我之前帖子中的一个人的指导了解了这些知识) .

task :default do
    FileList['file*.rb'].each { |file| ruby file }
end

但是,如果有一个脚本在执行时失败,运行将被终止。

请任何人帮助指导我如何修改“rakefile.rb”,以便如果有一个脚本失败,那么系统将忽略它并继续运行我的测试套件中的下一个脚本?

另外,您能否建议我一种在将脚本运行到一个输出文件时将所有结果写入的方法?或者每个脚本的结果都放在每个输出文件中,并且输出文件将显示脚本列表失败。任何帮助表示赞赏。非常感谢。

【问题讨论】:

    标签: ruby selenium webdriver rake


    【解决方案1】:

    您可以使用beginrescue 来捕获测试脚本中的任何故障。

    有点像

    begin
     raise "Ruby test script failed"
    rescue
     puts "Error handled"
    end
    

    在你的情况下是这样的

    task :default do
        FileList['file*.rb'].each { |file| 
        begin
          ruby file
        rescue
          puts "Test script failed because of #{$!}"
        end 
        }
    end
    

    在写入一个类似于

    的文件时
    task :default do
        $stdout = File.new('console.out', 'w')
        $stdout.sync = true
        FileList['*.rb'].each { |file| 
        begin
          ruby file
        rescue
          puts "test script failed because of #{$!}"
        end 
        }
    end
    

    这样做是覆盖 $stdout 以重定向控制台输出。

    【讨论】:

    • 非常感谢您,根据您的指南,只需再添加一次日志消息,现在我可以运行我的测试套件中的所有测试脚本,然后显示失败的测试列表完成执行脚本,“rakefile.rb”文件现在是: task :default do $stdout = File.new('console.out', 'w') $stdout.sync = true FileList['test*.rb'] .each { |文件|开始 ruby​​ 文件救援 puts "以下测试报告了意外行为:" puts "#{file} \n" end } end
    • 但是,您能否指导我更多地修改“rakefile.rb”以便能够将执行每个失败测试的内容导出到每个输出文件?这意味着我希望执行每个我的脚本的内容将被写入输出文件而不是显示在我的 Ruby 终端上(例如:当我运行测试脚本“test_GI-1.rb”时,执行这个的内容脚本将被写入输出文件“test_GI-1.out”,而不是显示在我的终端中。再次感谢您的出色指导。
    • 类似ruby example.rb > test.txt
    • 是的,我知道像你提到的 ruby​​ test.rb >> test.txt 这样的方式,当我在 Ruby 终端中键入它时,它按预期工作。但是,当我将这个东西包含在 rakefile.rb 中时,它根本不起作用。我将为您和某人打开一个新帖子,以更轻松地帮助我。再次感谢。
    【解决方案2】:

    我在一个单元框架中运行我的所有测试。我自己使用测试单元,但你也可以使用 rspec。这也使您能够将断言添加到代码中,然后由单元框架报告。如果一个测试失败或出错,您可以继续下一个测试。

    我的 rakefile 的简化版本如下所示

    require 'rake/testtask'
    
    #this will run all tests in directory with no dependencies 
    Rake::TestTask.new do |t|
      t.libs << "test"
      t.test_files = FileList['FAL*.rb']
      t.verbose = true
    end
    
    #or you could run individual files like this
    
    task :FAL001 do 
      ruby "FAL001.rb"
    end
    

    每个测试用例都是这样的

    require "test-unit"
    gem "test-unit"
    require "selenium-webdriver"
    
    class FAL001 < Test::Unit::TestCase
      def testFAL001 #methods that begin with test are automatically run 
        #selenium code goes here 
        assert_true(1 == 1)
      end
      def test002 
        #another test goes here 
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2021-03-29
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多