Sha256: cca29a5cf9b6b861c997604a18235cfa000762db06ab0079057672290c613fd6

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'tempfile'
require 'time'
require 'capybara'
require 'gnawrnip/developer'

module Gnawrnip
  class Screenshot
    class << self

      #
      # Screenshot of current capybara session
      #
      # When browser is still loading page, raise follow exception (maybe...)
      #
      #   Selenium::WebDriver::Error::UnknownError:
      #     Could not take screenshot of current page - TypeError: c is null
      #
      # So, to retry during +wait_second+ seconds.
      #
      # @example
      #   image = Gnawrnip::Screenshot.take
      #
      #
      # @param   [Fixnum]  wait_second  Second to repeat the retry
      # @return  [Tempfile]  Image file of screenshot
      #
      def take(wait_second = Capybara.default_wait_time)
        start_time = Time.now

        begin
          shot
        rescue Capybara::NotSupportedByDriverError => e
          raise e
        rescue => e
          if (Time.now - start_time) < wait_second
            sleep(0.3)
            retry
          end

          $stderr.puts "WARNING: Timeout!! Can't take screenshot"
          $stderr.puts "  #{e}"

          nil
        end
      end

      private

        def session
          Capybara.current_session
        end

        #
        # @return [Gnawrnip::Image]
        #
        def shot
          tempfile = Tempfile.new(['gnawrnip', '.png'])
          session.save_screenshot(tempfile.path)
          Developer.new.develop(tempfile)
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gnawrnip-0.2.5 lib/gnawrnip/screenshot.rb