how to export results when running selenium ruby w

2019-02-16 02:32发布

问题:

Currently, I'm running all selenium scripts in my test suite (written by Selenium Ruby Webdriver) at one time by using rake gem in "Start Command Prompt with Ruby" terminal.

To do this I have to create a file with name "rakefile.rb" with below content and just call "rake" in my terminal: (I have known this knowledge based on the guide of a person in my previous posts).

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

However, running will be terminated if there is one script got failure when executing.

Anybody please help guide me how to modify "rakefile.rb" so that if there is one script failed, then system will ignore it and continue to run the next script in my test suite ?

Also, could you please suggest me a way to write all results when running scripts to one output file ?, or the result of each script is put in each output file and a output file will display the list of scripts failed. Any help is appreciated. Thanks so much.

回答1:

I run all my tests inside a unit framework. I use test-unit myself but you could also use rspec. This also gives you the ability to add assertions to your code and then have it be reported by the unit framework. If one test fails or errors you can move on to the next test.

a simplified version of my rakefile looks like this

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

and every test case looks like this

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


回答2:

You could use a begin and rescue to catch any failures in your test scripts.

Something like

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

Which in your case would be something like

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

and as of writing to a file that would be something like

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

What that does is override $stdout to redirect the console output.