Save screenshot with Watir

2020-06-25 04:18发布

I am using Watir with Ruby on Rails.

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

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?

2条回答
劳资没心,怎么记你
2楼-- · 2020-06-25 05:03

Try following browser class, it works for me.

Browser::BROWSER.driver.save_screenshot(screenshot)

(reference)

查看更多
放我归山
3楼-- · 2020-06-25 05:05

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).

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.

Just install watir-webdriver gem and change

@browser = Watir::Safari.new

to

@browser = Watir::Browser.new :ff

For more information, read free version of my Watir book:

https://github.com/zeljkofilipin/watirbook/downloads

查看更多
登录 后发表回答