ruby - 如何降低使用 selenium 截取的屏幕截图的质量或尺寸?

标签 ruby selenium selenium-webdriver cucumber

我正在使用 Cucumber 并行运行一组测试,然后将它们组合到一个 HTML 报告中。在失败的测试中,会截取我的浏览器的屏幕截图并将其作为 png 嵌入到报告中。对于有很多失败的测试运行,报告可能会增长到 50MB,并且需要很长时间才能加载报告。

有没有办法截取屏幕截图,缩小它的大小,然后才将其嵌入到报告中,这样我就可以减小文件大小?假设图像需要嵌入并且不能作为独立于报告的文件存储。

最佳答案

Selenium 不提供调整屏幕截图大小的方法。不过,您可以轻松覆盖“screenshot_as”方法,使其返回较小的图像。 此示例使用“chunky_png”库将每个屏幕截图的大小调整为原始大小的 80%:

require 'selenium-webdriver'
require 'chunky_png'

module Selenium
  module WebDriver
    module DriverExtensions
      module TakesScreenshot

        def screenshot_as(format)

          # take a screenshot and load it with ChunkyPNG
          img = ChunkyPNG::Image.from_blob(bridge.getScreenshot.unpack("m")[0])

          # reduce the size to 80% of the original size
          img = img.resize((img.width * 0.8).floor, (img.height * 0.8).floor)

          case format
            when :base64
              img.to_blob.pack('A*m')
            when :png
              img.to_blob
            else
              raise Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
          end
        end

      end
    end
  end
end

关于ruby - 如何降低使用 selenium 截取的屏幕截图的质量或尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576459/

相关文章:

ruby - 用 Ruby 创建 COM 服务器?

java - Selenium Java - 新的 Actions(driver)).sendKeys(...) 行为

java - 当单元格值为空时,Poi 的 getCellType() 抛出 NullPointerException

ruby-on-rails - 在 Rails 模型中编写正则表达式查询

ruby-on-rails - 基于 View /喜欢/不喜欢的趋势产品

Python Selenium 在同一 Firefox 窗口中打开 URL

python - 使用 GUI 分发计算机应用程序 (Python)

selenium - 无法使用 css 选择器找到元素

c# - 如何重复失败的测试,以便它仅在 x 次重复后失败?

ruby-on-rails - Ruby on Rails ActiveRecord 数据流