3

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?

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
rookieRailer
  • 2,313
  • 2
  • 28
  • 48

2 Answers2

4

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

Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
  • I looked into the file safariwatir.rb and found that it does not have the driver method in the Browser class, required for taking screenshots. I have been trying to use watir-webdriver instead, but I just cannot include it in my program. Read this post http://stackoverflow.com/questions/8365555/require-watir-webdriver-error. Thanks for trying to help me out. Please let me know if you can help on this. – rookieRailer Dec 06 '11 at 01:40
  • I took a look at your other question, but I have no idea how Rails works, so I can not help there. – Željko Filipin Dec 06 '11 at 09:28
  • 1
    I was able to fix the problem. I read somewhere that, not being able to include a gem, might be because I have multiple versions of ruby installed in my machine. So I removed everything and reinstalled everything and it works fine. Thanks. – rookieRailer Dec 06 '11 at 14:49
0

Try following browser class, it works for me.

Browser::BROWSER.driver.save_screenshot(screenshot)

(reference)

j0k
  • 22,600
  • 28
  • 79
  • 90
amjad
  • 2,876
  • 7
  • 26
  • 43