Problem with screenshot.save method

27 views
Skip to first unread message

Luis Espla

unread,
May 17, 2013, 6:13:58 AM5/17/13
to watir-general
Hello, I use "@browser.screenshot.save file" for when I have a problem, but when I use it and I have a Timeout Exception it doesn't work well, because it was waiting for page load totally(This page never load because It has a problem). How can I do for It doesn't wait and capture a snapshoot?
Best Regards


Chuck van der Linden

unread,
May 17, 2013, 2:32:21 PM5/17/13
to watir-...@googlegroups.com
On Friday, May 17, 2013 3:13:58 AM UTC-7, LuisE wrote:
Hello, I use "@browser.screenshot.save file" for when I have a problem, but when I use it and I have a Timeout Exception it doesn't work well, because it was waiting for page load totally(This page never load because It has a problem). How can I do for It doesn't wait and capture a snapshoot?
Best Regards

If you are trying to do this inline in your code then wrap the thing you expect to sometimes fail with exception handling, and have the screenshot as part of the actions to take when it fails

What I'd do is something along these lines

 begin
  #code you think might fail due to timeout
 rescue
  #code to take screenshot
  raise "timeout trying to load page xxxx" #presumes we still want the test to fail at this point
 end

See here for a pretty decent into to ruby exception handling http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/  

A better way however is if you are using a framework like cucumber, which allows you to define things to happen after every test.  You can put code there that takes specific actions if the test failed, such as taking a screenshot and embedding it in the html report generated by the framework   EG

  #I use cucumber, this is from my env.rb file
After do |scenario|
  if scenario.failed?
    screenshot = "./FAILED_#{scenario.name.gsub(' ','_').gsub(/[^0-9A-Za-z_]/, '')}.png"
    @browser.driver.save_screenshot(screenshot)
    encoded_img = @browser.driver.screenshot_as(:base64)
    embed("data:image/png;base64,#{encoded_img}",'image/png')
  end
end

 
Reply all
Reply to author
Forward
0 new messages