Sha256: 2adeed10eb2bedf10eaba46601394b3bc48511438e341d4fcc42310c16cb7733

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require 'capybara'
require 'capybara/poltergeist'

CAPYBARA_ERRORS = [
  Capybara::ElementNotFound
]

module ClickSession
  class WebRunner
    include Capybara::DSL

    attr_reader :processor

    def initialize(processor)
      Capybara.default_driver = ClickSession.configuration.driver_client
      Capybara.javascript_driver = ClickSession.configuration.driver_client
      Capybara.run_server = false
      page = Capybara::Session.new(ClickSession.configuration.driver_client)
      @processor = processor
    end

    def run(model)
      raise NotImplementedError("You need to override the #steps method by sub classing WebRunner")
    end

    def reset
      clear_cookies
    end

    def save_screenshot(file_name_identity = "")
      @file_name = build_file_name_for(file_name_identity)
      page.save_screenshot(screenshot_save_path, full: true)

      S3FileUploader.new.upload_file(@file_name)
    end

    def point_of_no_return
      processor.stop_processing
    end

    private
    def clear_cookies
      browser = Capybara.current_session.driver.browser

      if browser.respond_to?(:manage) and browser.manage.respond_to?(:delete_all_cookies)
        browser.manage.delete_all_cookies
      else
        page.driver.cookies.keys.each do |cookie|
          page.driver.remove_cookie(cookie)
        end
      end
    end

    def screenshot_save_path
      path_for(@file_name)
    end

    def build_file_name_for(file_name_identity)
      readable_date = Time.now.strftime("%F_%T")
      "screenshot-#{file_name_identity}-#{readable_date}.png"
    end

    def path_for(file_name)
      "#{Rails.root}/tmp/#{file_name}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
click_session-0.1.1 lib/click_session/web_runner.rb
click_session-0.1.0 lib/click_session/web_runner.rb