Sha256: 4e734b8a1b257a2063c33db4fa287153a096e6079d18550ab0f90795d3e969a9

Contents?: true

Size: 1.83 KB

Versions: 41

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require 'aranha/selenium/driver_factory'

module Aranha
  module Selenium
    class Session < ::SimpleDelegator
      attr_reader :downloads, :wait

      def initialize(options = {})
        @downloads = Downloads.new
        @wait = ::Selenium::WebDriver::Wait.new(timeout: 15)
        super(
          ::Aranha::Selenium::DriverFactory.create_driver(
            options.merge(download_dir: @downloads.dir)
          )
        )
      end

      def find_or_not_element(find_element_args)
        r = find_elements(find_element_args)
        r.any? ? r.first : nil
      end

      def wait_for_click(find_element_args)
        wait.until do
          element = find_element(find_element_args)
          element ? element_click(element) : nil
        end
      end

      def wait_for_element(find_element_args)
        wait.until { find_element(find_element_args) }
      end

      def wait_for_download
        initial_downloads = downloads.current
        yield
        new_downloads = []
        wait.until do
          new_downloads = downloads.current - initial_downloads
          new_downloads.any?
        end
        new_downloads.first
      end

      def current_source
        element = find_element(xpath: '/html[1]')
        raise 'Root element not found' unless element

        s = element.attribute('innerHTML')
        "<html>\n#{s}\n</html>\n"
      end

      class Downloads
        attr_reader :dir

        def initialize
          @dir = ::Dir.mktmpdir
        end

        def current
          Dir.glob("#{dir}/**/*")
        end
      end

      private

      def element_click(element)
        element.click
        element
      rescue ::Selenium::WebDriver::Error::ElementClickInterceptedError,
             ::Selenium::WebDriver::Error::ElementNotInteractableError
        nil
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 3 rubygems

Version Path
ehbrs-tools-0.28.3 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.28.2 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.28.1 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.28.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.27.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.26.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.25.1 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.25.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.24.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
aranha-selenium-0.2.2 lib/aranha/selenium/session.rb
aranha-selenium-0.2.1 lib/aranha/selenium/session.rb
ehbrs-tools-0.23.1 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.23.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.22.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.21.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
aranha-selenium-0.2.0 lib/aranha/selenium/session.rb
ehbrs-tools-0.20.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.19.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.18.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb
ehbrs-tools-0.17.0 vendor/aranha-selenium/lib/aranha/selenium/session.rb