2009-11-30 45 views
3

我试图捕捉使用selenium-client和rspec的测试失败截图。我运行这个命令:使用rspec的Selenium屏幕截图

$ spec my_spec.rb \ 
--require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter' \ 
--format=Selenium::RSpec::SeleniumTestReportFormatter:./report.html 

它会在所有东西都通过时正确创建报告,因为不需要任何屏幕截图。然而,当测试失败,我得到这个消息,报告中有空白屏幕截图:

WARNING: Could not capture HTML snapshot: execution expired 
WARNING: Could not capture page screenshot: execution expired 
WARNING: Could not capture system screenshot: execution expired 
Problem while capturing system stateexecution expired 

是什么原因造成“执行过期”的错误?我是否错过了我的规格中的重要内容?这里是my_spec.rb的代码:

require 'rubygems' 
gem "rspec", "=1.2.8" 
gem "selenium-client" 
require "selenium/client" 
require "selenium/rspec/spec_helper" 

describe "Databases" do 
    attr_reader :selenium_driver 
    alias :page :selenium_driver 

    before(:all) do 
     @selenium_driver = Selenium::Client::Driver.new \ 
      :host => "192.168.0.10", 
      :port => 4444, 
      :browser => "*firefox", 
      :url => "http://192.168.0.11/", 
      :timeout_in_seconds => 10 
    end 

    before(:each) do 
    @selenium_driver.start_new_browser_session 
    end 

    # The system capture need to happen BEFORE closing the Selenium session 
    append_after(:each) do 
    @selenium_driver.close_current_browser_session 
    end 

    it "backed up" do 
    page.open "/SQLDBDetails.aspx" 
    page.click "btnBackup", :wait_for => :page 
    page.text?("Pending Backup").should be_true 
    end 
end 
+0

这种更为玩弄后,我发现截图的工作,如果我删除“@ selenium_driver.close_current_browser_session”来自append_after(:each)。但是,这让我有一堆浏览器窗口被打开,因为没有会话被释放。我尝试在之前(:each)之前释放它们,而不是之后(:each),但当会话在之前(:each)释放时,屏幕截图仍然不起作用。 –

回答

0

为了得到工作,我不得不国防部事情上的错误截图一点点。

我提出下列代码出spec_helper(我发现在C:\红宝石\ lib中\红宝石\宝石\硒 - 客户1.2.18 \ lib中\硒\ rspec的\ spec_helper.rb):的

if actual_failure? 
     Selenium::RSpec::SeleniumTestReportFormatter.capture_system_state(selenium_driver, self) 
    end 

并将其放入我测试的setup/teardown(在行@ selenium_driver.close_current_browser_session之前)的append_after(:each)部分。

希望这会有所帮助!

0

为什么不把截图放在after函数中,但在关闭浏览器之前?

+0

根据文档,SeleniumTestReportFormatter应该处理失败时的屏幕截图。它(几乎)。每当测试失败时,它都会尝试截图。问题是浏览器会话发布后需要截图。 在after函数中手动截取屏幕截图将是一个可接受的解决方法,但我找不到有关如何使用selenium-client和rspec执行此操作的任何文档。你知道该怎么做吗? –

+0

对不起,我对SeleniumTestReportFormatter不熟悉。我是一个Java人:P但是,应该有一个“带截图”命令。 Java客户端当然有。它将屏幕截图作为base64编码的字符串返回,您必须将其解码并写入文件。 –

1

我遇到了这个问题,并能够通过设置驱动程序超时来解决它。 这可能会导致驱动程序运行到之前结束浏览器会话:after_each 您正在使用10秒,我运行罚款:timeout_in_seconds => 2000

+0

我的问题没有通过更改timeout_in_seconds来解决。事实上,timeout_in_seconds是第一个经常遇到失败测试的东西。在此示例中,如果页面在特定时间内未加载“Pending Backup”文本,则测试将失败。此时,测试失败,因此SeleniumTestReportFormatter应该抓取截图。 如果我将timeout_in_seconds命令增加到2000秒,这只会让测试在失败之前花费2000秒,此时我会得到相同的错误,无法获取屏幕截图。 –

0

它看起来像在那里丢失"

it "backed up" do 
    page.open "/SQLDBDetails.aspx 
+1

我不确定我是否理解你的答案。也许你可以编辑它使其更清晰? –