Sha256: 230da5d867ba25ee4654602d00b4f89f33288fbb84609c61a44f1118e7c9c34f

Contents?: true

Size: 1009 Bytes

Versions: 1

Compression:

Stored size: 1009 Bytes

Contents

# frozen_string_literal: true

module Superbot
  module Capybara
    class Convert
      def initialize(json)
        @json = JSON.parse(json, symbolize_names: true)
      end

      def self.call(json)
        new(json).call
      end

      def call
        converted_json&.tap { |script| Superbot::Capybara::Runner.run(script) }
      end

      attr_accessor :json

      private

      def convert_action(action)
        case action[:type]
        when 'visit'      then "visit '#{action[:url]}'"
        when 'click'      then "click_on '#{action[:selector]}'"
        when 'scroll'     then
          "page.execute_script('window.scrollBy(0,' + (page.execute_script('return document.body.scrollHeight') * #{action[:amountPercent]} / 100).to_s + ')')"
        when 'resolution' then "page.driver.browser.manage.window.resize_to(#{action[:resolution].join(',')})"
        end
      end

      def converted_json
        json.map { |action| convert_action(action) }.join('; ')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
superbot-0.1.2 lib/superbot/capybara/convert.rb