使用 Watir 保存屏幕截图 [英] Save screenshot with Watir
问题描述
我正在将 Watir 与 Ruby on Rails 结合使用.
I am using Watir with Ruby on Rails.
我需要使用 Watir 保存几个页面的屏幕截图.我已经设法让我想打开的页面显示在浏览器中,但还不能保存屏幕截图.这是我的代码:
I need to save screenshots of couple of pages using Watir. I have managed to get the page that I want to open to show in a browser, but cannot save the screenshot yet. Here's my code:
@browser = Watir::Safari.new
folios_screenshot_path = Rails.root.join('screenshots/')
@page = Page.find(5)
cur_url = root_url + 'pages/' + @page.id.to_s
@browser.goto cur_url
@browser.div(:id => "page").wait_until_present
@browser.driver.save_screenshot(pagess_screenshot_path + '/' + @page.id.to_s + '.png')
@browser.close
在我加载的页面中,有一个 ID 为page"的 div 元素,我试图让 Watir 等到该元素加载到 Watir 浏览器中.但是在我的主浏览器中,我收到错误无法在 10 秒内加载页面,并且屏幕截图也没有保存.知道出了什么问题吗?
In the page that I load, there's a div element with id 'page', and I am trying to make Watir wait till that element is loaded in the Watir browser. But in my main browser, I get the error Unable to load page within 10 seconds, and the screenshot doesn't get saved either. Any idea on what's wrong?
推荐答案
watir gems 有几个:watir(在 windows 上驱动 IE)、safariwatir(在 mac 上驱动 safari)、watir-webdriver(在 safari 上驱动所有流行的浏览器所有流行的操作系统).
There are several watir gems: watir (drives IE on windows), safariwatir (drives safari on mac), watir-webdriver (drives all popular browsers except safari on all popular operating systems).
您正在使用 safariwatir gem,但您正在尝试使用 watir-webdriver 的 driver.save_screenshot
保存屏幕截图.我建议您使用 Firefox 截屏.
You are using safariwatir gem, but you are trying to save screenshot using watir-webdriver's driver.save_screenshot
. I would suggest that you take a screen shot with Firefox.
只需安装 watir-webdriver gem 并进行更改
Just install watir-webdriver gem and change
@browser = Watir::Safari.new
到
@browser = Watir::Browser.new :ff
有关更多信息,请阅读我的 Watir 书的免费版本:
For more information, read free version of my Watir book:
https://github.com/zeljkofilipin/watirbook/downloads
这篇关于使用 Watir 保存屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!