Sha256: af43963591b7ab33eb3a178cb2ad833b8a37f176cb9c7cee6b1133f406dc4f74

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

require 'tempfile'
require 'time'
require 'capybara'

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
          tempfile = Tempfile.new(['gnawrnip', '.png'])
          session.save_screenshot(tempfile.path)
          tempfile
        rescue Capybara::NotSupportedByDriverError => e
          raise e
        rescue => e
          raise e if (Time.now - start_time) >= wait_second
          sleep(0.3)
          retry
        end
      end

      private

      def session
        Capybara.current_session
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gnawrnip-0.1.3 lib/gnawrnip/screenshot.rb
gnawrnip-0.1.2 lib/gnawrnip/screenshot.rb
gnawrnip-0.1.1 lib/gnawrnip/screenshot.rb