红宝石和瓦蒂尔URL的全页面截图

时间:2018-06-27 15:05:46

标签: ruby selenium-webdriver automated-tests watir

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

有可能吗?

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

1 个答案:

答案 0 :(得分:1)

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

require 'win32/screenshot'

Win32 ::屏幕截图:: Take.of(:foreground).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)