Sha256: b05c68ed6a793a7563ecb8bc93916c50decaa3731f77e39058a037070af9f195

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

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

require "teaspoon/driver/base"

module Teaspoon
  module Driver
    class Phantomjs < 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::DriverOptionsError.new(types: "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) }

          unless $?.nil? || $?.success?
            raise Teaspoon::DependencyError.new("Failed to use phantomjs, which exited with status code: #{$?.exitstatus}")
          end
        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 = defined?(::Phantomjs) ? ::Phantomjs.path : which("phantomjs")
          return @executable unless @executable.blank?
          raise Teaspoon::MissingDependencyError.new("Unable to locate phantomjs. Install it or use the phantomjs gem.")
        end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
teaspoon-1.4.0 lib/teaspoon/driver/phantomjs.rb
teaspoon-1.2.2 lib/teaspoon/driver/phantomjs.rb
teaspoon-1.2.1 lib/teaspoon/driver/phantomjs.rb
teaspoon-1.2.0 lib/teaspoon/driver/phantomjs.rb