如何降低使用 selenium 截取的屏幕截图的质量或尺寸?
How can I reduce the quality or dimensions of a screenshot taken with selenium?
我是 运行 一组并行测试,使用 Cucumber,然后将它们组合到一个 HTML 报告中。在失败的测试中,会截取我的浏览器的屏幕截图并将其作为 png 嵌入到报告中。对于有很多失败的测试运行,报告可能会增长到 50MB,并且需要很长时间才能加载报告。
有没有办法截屏,缩小它的大小,然后再将它嵌入到报告中,这样我就可以减小文件大小?假设图像需要嵌入并且不能作为独立于报表的文件存储。
Selenium 不提供调整屏幕截图大小的方法。不过,您可以轻松覆盖 "screenshot_as" 方法,使其 return 成为较小的图像。
此示例使用 "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
我是 运行 一组并行测试,使用 Cucumber,然后将它们组合到一个 HTML 报告中。在失败的测试中,会截取我的浏览器的屏幕截图并将其作为 png 嵌入到报告中。对于有很多失败的测试运行,报告可能会增长到 50MB,并且需要很长时间才能加载报告。
有没有办法截屏,缩小它的大小,然后再将它嵌入到报告中,这样我就可以减小文件大小?假设图像需要嵌入并且不能作为独立于报表的文件存储。
Selenium 不提供调整屏幕截图大小的方法。不过,您可以轻松覆盖 "screenshot_as" 方法,使其 return 成为较小的图像。 此示例使用 "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