Watir-webdriver:获取所有失败场景的截图图像[for Test :: Unit]

时间:2013-10-20 08:50:32

标签: watir watir-webdriver

我希望我的测试能够为所有失败的场景保存屏幕截图。

我发现了一篇关于这个问题的好文章“Watir-webdriver: get same screenshot images for all failed scenarios”,我把Justin建议的代码放在我的env.rb中并​​且效果不好。我不确定这是因为我没有使用黄瓜?

鉴于我有一个典型的测试案例:

require "rubygems"
require "test/unit"
require "watir-webdriver"
require "page-object"
require "./home_page"


class LogInTest < Test::Unit::TestCase
  # Called before every test method runs. Can be used
  # to set up fixture information.
  def setup
    @browser ||= Watir::Browser.new :firefox
  end

  # Called after every test method runs. Can be used to tear
  # down fixture information.

  def teardown
    @browser.close
  end

  # Fake test
  def test_fail
    @home_page = HomePage.new(@browser)
    @home_page.visit
    @log_in_page = @home_page.go_to_log_in
    @all_deals = @log_in_page.log_in("test_user","test_pass")
    assert @browser.title.include? "- hello world"
  end
end

如何制作通用方法,将失败测试的所有屏幕截图保存到目标文件夹?

非常感谢你的时间。

1 个答案:

答案 0 :(得分:0)

你提出的问题是正确的,因为它是为Cucumber编写的。但是,同样可以适用于Test :: Unit。

尝试将teardown切换为:

def teardown
  #Save image if test fails
  unless passed?
    #Where to save the image and the file name
    screenshot_file = "screenshot-#{Time.now.strftime('%Y%m%d-%H%M%S')}.png"

    #Save the image
    @browser.driver.save_screenshot(screenshot_file)
  end

  #Close the browser
  @browser.close
end