Sha256: 58818c51a3edf7b2279b73fa1d619916cc9562e7914e0ddf5e4dfa2cf2c1c2cb

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'kommando'

module Superbot
  module Capybara
    class Runner
      def self.run(script)
        new.run(script)
      end

      def run(script)
        create_runner
        runner.in.writeln({ eval: script }.to_json)
      end

      def rerun(script)
        runner.in.writeln({ eval: script }.to_json)
      end

      def kill_session
        runner&.kill
      rescue Timeout::Error
        p # do nothing
      ensure
        @runner = nil
      end

      attr_accessor :script, :runner, :finished

      private

      def create_runner
        return if runner

        gem 'superbot-capybara'

        @runner = Kommando.new "sb-capybara"

        @finished = false

        runner.out.every(/{"type":"ok".*\n/) do
          puts "Test succeed!"
          @finished = true
        end

        runner.out.every(/{"type":"error".*\n/) do
          parsed_error = JSON.parse(runner.out.lines.last, symbolize_names: true)
          puts "Test failed: #{parsed_error[:message]}"
          @finished = true
          if parsed_error[:class].start_with?('Selenium::WebDriver::Error')
            kill_session
            puts "", "ERROR: Seems like browser session has been closed, try to run test again to create new session"
          end
        end

        runner.run_async
      rescue Gem::LoadError
        abort "superbot-capybara not installed"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
superbot-0.1.4.rc1 lib/superbot/capybara/runner.rb