【问题标题】:Full-page screenshot with URL in ruby ​and watir带有 ruby​​ 和 watir 中的 URL 的整页屏幕截图
【发布时间】:2018-12-06 13:06:34
【问题描述】:

使用下面的代码,我只截取了屏幕的一部分,但我想截取完整的屏幕截图,其中还显示了页面的 URL。

有可能吗?

AfterStep do
  encoded_img = @browser.driver.screenshot_as(:base64)
    embed("data:image/png;base64,#{encoded_img}",'image/png')
end

【问题讨论】:

  • 最近发布了一个watir-screenshot-stich gem 来解决类似的问题。你可以看看这是否适合你。
  • 这个想法很好,但它仍然对我不起作用,我已经阅读了文档并将其应用到我的代码中,但它仍然在
  • 我想你想看看从操作系统的角度截图:例如对于 Windows github.com/jarmo/win32screenshot 浏览器截图仅限于其视口。
  • Hacky 解决方法,但您可以使用 browser.screenshot.savebrowser.url 作为参数。诚然,您必须做一些体操才能剥离 http:// 并将 / 转换为另一个分隔符。 YMMV。

标签: ruby selenium-webdriver automated-tests watir


【解决方案1】:

Watir 没有此规定来捕获屏幕截图和 URL。但是我们可以使用 win32ole 来做到这一点。

require 'win32/screenshot'

Win32::Screenshot::Take.of(:desktop).write(image_path)

在我使用 URL 捕获全屏截图的情况下,我执行以下操作

# Code to capture the full-page screenshot with a URL
require 'watir'
require 'win32ole'
require 'win32/screenshot'

# Launch a browser and navigate to the page
browser = Watir::Browser.new :ie
browser.goto "https://www.google.com"

win_title = browser.title #Fetch the Title

# Use AutoIt to get the focus of the browser to front
WIN32OLE.new("AutoItX3.Control").ControlFocus(win_title, "", "")

# Capture the screen shot of the desktop
sleep 2 # Hold for 2s
image_path = "image_path#{rand(10000)}.png"
Win32::Screenshot::Take.of(:desktop).write(image_path)

【讨论】:

  • 你不想require 'win32/screenshot'吗?
  • 我试过这样用,什么是base 64捕获然后放到html状态报告中,但是没有成功。 encoded_img = Win32 :: Screenshot :: Take.of (:foreground) .write (encoded_img) embed ("data: image / png; base64, # {encoded_img}", 'image / png')
  • 我没有图像格式转换方面的知识。无论我为您提供什么解决方案,都只会提供一个“.png”格式的输出文件,因为您的要求是捕获带有 URL 的整页屏幕截图。无论是在 selenium 中还是在 watir 中,我们都没有这样的支持来捕获浏览器窗口的屏幕截图。如果您想直接作为base64嵌入到页面中,最好按照以下步骤操作: 1. 在临时位置捕获图像。 2.将其转换为base64 3.嵌入到页面中 4.如果需要,从该位置删除图像文件。
  • 请查看以下网址:stackoverflow.com/questions/1547008/…
【解决方案2】:

在我使用 URL 捕获全屏截图的情况下,我执行以下操作

#Code to capture the Full-Page screenshot with URL require 'watir' require 'win32ole' require 'win32/screenshot'

#Launch Browser and navigate to page browser = Watir::Browser.new :ie browser.goto "https://www.google.com"

#Fetch the Title win_title = browser.title

#Use AutoIt to get the focus of the browser to front WIN32OLE.new("AutoItX3.Control").ControlFocus(win_title, "", "")

#Capture the screen shot of the desktop sleep 2 # Hold for 2s image_path = "image_path#{rand(10000)}.png" Win32::Screenshot::Take.of(:desktop).write(image_path)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多