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