Method: Webstract::ScreenCapture#capture

Defined in:
lib/webstract/screen_capture.rb

#capture(url, path, opts = {}) ⇒ Object

Captures a screenshot of url saving it to path.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/webstract/screen_capture.rb', line 37

def capture(url, path, opts = {})
  begin
    # Default settings
    @width   = opts.fetch(:width, 120)        if opts[:width]
    @height  = opts.fetch(:height, 90)        if opts[:width]

    # Reset session before visiting url
    Capybara.reset_sessions! unless @session_started
    @session_started = false

    # Open page
    visit(url)

    # Timeout
    sleep opts[:timeout] if opts[:timeout]

    # Check response code
    if page.driver.status_code.to_i == 200 || page.driver.status_code.to_i / 100 == 3
      page.driver.save_screenshot(path, :full => true)
    else
      raise Webstract::Errors::PageError.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}")
    end
  rescue Capybara::Poltergeist::BrowserError, Capybara::Poltergeist::DeadClient, Capybara::Poltergeist::TimeoutError, Errno::EPIPE => e
    # TODO: Handle Errno::EPIPE and Errno::ECONNRESET
    raise Webstract::Errors::CaptureError.new("Capybara error: #{e.message.inspect}")
  end
end