Sha256: 2b7be8fa663b5af37805b69d158840c5529a96f9744feb5b1b6e06e3b7c4abd7

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

# :nocov:
begin
  require "selenium-webdriver"
rescue LoadError
  Teaspoon.abort("Could not find Selenium Webdriver. Install the selenium-webdriver gem.")
end
# :nocov:

require "teaspoon/driver/base"

module Teaspoon
  module Driver
    class Selenium < Base
      def initialize(options = nil)
        options ||= {}
        case options
        when Hash then @options = options
        when String then @options = JSON.parse(options)
        else raise Teaspoon::DriverOptionsError.new(types: "hash or json string")
        end
      rescue JSON::ParserError
        raise Teaspoon::DriverOptionsError.new(types: "hash or json string")
      end

      def run_specs(runner, url)
        driver = ::Selenium::WebDriver.for(
          driver_options[:client_driver],
          **driver_options[:selenium_options].to_hash.to_options
        )
        driver.navigate.to(url)

        ::Selenium::WebDriver::Wait.new(driver_options).until do
          done = driver.execute_script("return window.Teaspoon && window.Teaspoon.finished")
          driver.execute_script("return window.Teaspoon && window.Teaspoon.getMessages() || []").each do |line|
            runner.process("#{line}\n")
          end
          done
        end
      ensure
        driver.quit if driver
      end

      protected

        def driver_options
          @driver_options ||= HashWithIndifferentAccess.new(
            client_driver: :firefox,
            timeout: Teaspoon.configuration.driver_timeout.to_i,
            interval: 0.01,
            message: "Timed out",
            selenium_options: {}
          ).merge(@options)
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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