【问题标题】:Selenium screenshots using rspec使用 rspec 的 Selenium 屏幕截图
【发布时间】:2009-11-30 17:25:21
【问题描述】:

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

$ 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

【问题讨论】:

  • 在玩了更多之后,我发现如果我从 append_after(:each) 中删除 '@selenium_driver.close_current_browser_session',屏幕截图会起作用。然而,这让我打开了一堆浏览器窗口,因为没有一个会话被释放。我尝试在 before(:each) 而不是 after(:each) 中释放它们,但是在 before(:each) 中释放会话时屏幕截图仍然不起作用。

标签: ruby selenium rspec selenium-rc


【解决方案1】:

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

【讨论】:

  • 更改 timeout_in_seconds 并不能解决我的问题。实际上,timeout_in_seconds 通常是首先捕获失败测试的原因。在此示例中,如果页面在特定时间内未加载“待备份”文本,则测试失败。此时,测试失败,所以 SeleniumTestReportFormatter 应该截取屏幕截图。如果我将 timeout_in_seconds 命令增加到 2000 秒,这只会使测试在失败前花费 2000 秒,此时我会收到关于无法抓取屏幕截图的相同错误。
【解决方案2】:

为了获得错误工作的屏幕截图,我不得不稍微修改一下。

我将以下代码移出 spec_helper(我在 C:\Ruby\lib\ruby\gems\selenium-client-1.2.18\lib\selenium\rspec\spec_helper.rb 中找到):

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

并将其放入我的测试设置/拆卸的 append_after(:each) do 部分(在@selenium_driver.close_current_browser_session 行之前)。

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    为什么不在after函数中截屏,而是在关闭浏览器之前截屏呢?

    【讨论】:

    • 根据文档,SeleniumTestReportFormatter 应该负责处理失败的屏幕截图。它确实(几乎)。每当测试失败时,它都会尝试截取屏幕截图。问题是它在浏览器会话释放后截取屏幕截图。在 after 函数中手动截取屏幕截图是一种可以接受的解决方法,但我找不到任何关于如何使用 selenium-client 和 rspec 进行截图的文档。你知道怎么做吗?
    • 对不起,我不熟悉 SeleniumTestReportFormatter。我是 Java 人 :P 但是,应该有一个“截屏”命令。 Java客户端中肯定有。它将屏幕截图作为 base64 编码字符串返回,您必须将其解码并写入文件。
    【解决方案4】:

    不确定这是否有帮助,https://github.com/mattheworiordan/capybara-screenshot,虽然它是针对 Capybara 而不是 Selenium

    【讨论】:

      【解决方案5】:

      那里好像少了一个"

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

      【讨论】:

      • 我不确定我是否理解您的回答。也许您可以对其进行编辑以使其更清晰?
      猜你喜欢
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多