Sha256: c16f33e76acd26d7d2a95bfd50773b6d8348ea3cc8dd9743cbf624dd7a88f7c6

Contents?: true

Size: 1.65 KB

Versions: 40

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'aranha/selenium/session'

module Ehbrs
  module Google
    module Translate
      class Session
        START_URL = 'https://translate.google.com.br/#view=home&op=docs&sl=en&tl=pt'
        CLOSE_POPUP_CSS = '.dismiss-button'
        FILE_INPUT_XPATH = '//*[@id = "tlid-file-input"]'
        SUBMIT_XPATH = '//form[@action="//translate.googleusercontent.com/translate_f"]' \
          '//input[@type="submit"]'
        RESULT_XPATHS = [
          '//script[contains(@src, "https://translate.google.com/translate_a")]',
          '/html/body/pre'
        ].freeze

        attr_reader :sub

        def initialize
          @sub = ::Aranha::Selenium::Session.new
        end

        def translate(source_document_path)
          go_to_upload_form
          input_file(source_document_path)
          close_changes_alert_popup
          click_on_translate_button
          wait_for_load_translation
          sub.current_source
        end

        private

        def go_to_upload_form
          sub.navigate.to START_URL
          sub.wait_for_element(xpath: FILE_INPUT_XPATH)
        end

        def input_file(source_document_path)
          sub.find_element(xpath: FILE_INPUT_XPATH).send_keys(source_document_path)
        end

        def click_on_translate_button
          sub.wait_for_click(xpath: SUBMIT_XPATH)
        end

        def close_changes_alert_popup
          sub.wait_for_click(css: CLOSE_POPUP_CSS)
        end

        def wait_for_load_translation
          sub.wait.until do
            RESULT_XPATHS.any? { |xpath| sub.find_elements(xpath: xpath).size.positive? }
          end
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
ehbrs-tools-0.37.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.36.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.35.1 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.35.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.34.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.33.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.32.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.31.1 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.31.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.30.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.29.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.28.3 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.28.2 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.28.1 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.28.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.27.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.26.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.25.1 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.25.0 lib/ehbrs/google/translate/session.rb
ehbrs-tools-0.24.0 lib/ehbrs/google/translate/session.rb