Sha256: 07b7c33b0a35f9647702a7ee3fe2c0955775dcb93e39dd3205111ae3010265a7

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

begin
  require "phantomjs"
rescue LoadError
  # if we can't load phantomjs, assume the cli is installed and in the path
end

module Teaspoon
  module Drivers
    class PhantomjsDriver < Base
      include Teaspoon::Utility

      def initialize(options = nil)
        options ||= []
        case options
        when Array then @options = options
        when String then @options = options.split(" ")
        when Hash then @options = options.map { |k, v| "--#{k}=#{v}" }
        else raise Teaspoon::UnknownDriverOptions, "Unknown driver options -- supply a string, array or hash"
        end
      end

      def run_specs(runner, url)
        run(*driver_options(url)) do |line|
          runner.process(line) if line && line.strip != ""
        end
      end

      protected

      def run(*args, &block)
        IO.popen([executable, *args].join(" ")) { |io| io.each(&block) }
      end

      def driver_options(url)
        [
          @options,
          escape_quotes(script),
          escape_quotes(url),
          Teaspoon.configuration.driver_timeout
        ].flatten.compact
      end

      def escape_quotes(string)
        %{"#{string.gsub('"', '\"')}"}
      end

      def executable
        return @executable if @executable
        @executable = which("phantomjs")
        @executable = Phantomjs.path if @executable.blank? && defined?(::Phantomjs)
        return @executable unless @executable.blank?
        raise Teaspoon::MissingDependency, "Could not find PhantomJS. Install phantomjs or try the phantomjs gem."
      end

      def script
        File.expand_path("../phantomjs/runner.js", __FILE__)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
teaspoon-0.9.1 lib/teaspoon/drivers/phantomjs_driver.rb
teaspoon-0.9.0 lib/teaspoon/drivers/phantomjs_driver.rb