Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何降低使用selenium拍摄的屏幕截图的质量或尺寸?_Ruby_Selenium_Selenium Webdriver_Cucumber - Fatal编程技术网

Ruby 如何降低使用selenium拍摄的屏幕截图的质量或尺寸?

Ruby 如何降低使用selenium拍摄的屏幕截图的质量或尺寸?,ruby,selenium,selenium-webdriver,cucumber,Ruby,Selenium,Selenium Webdriver,Cucumber,我正在使用Cucumber并行运行一套测试,然后将它们组合到一个HTML报告中。在失败的测试中,我的浏览器截图被截取并作为png嵌入报告中。对于有很多失败的测试运行,报告可能会增长到50MB,并且需要很长时间才能加载报告 有没有一种方法可以截图,缩小它的大小,然后将它嵌入到报告中,这样我就可以减小文件的大小?假设图像需要嵌入,并且不能作为独立于报告的文件存储。Selenium没有提供调整屏幕截图大小的方法。不过,您可以轻松地覆盖“screenshot_as”方法,使其返回较小的图像。 本例使用“

我正在使用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

您对可以使用/安装的工具有任何限制吗?您是否尝试过调整浏览器的窗口大小,以使屏幕截图更小?不确定是否将选项传递给Selenium,但您可以在下载后使用Imagemagick或其Ruby bindings RMagick来调整大小。仅使用Selenium无法做到这一点,而不是通过减小帧大小。更可能的情况是,您只想缩小生成的图像。谢谢您的评论。我将研究RMagick,我还研究了ad tinypng.com,它也有一颗红宝石。