Sha256: 3b4074ae6d2125eb0a31cd3d33243ea7226869ec01465a8512215c87996f60aa

Contents?: true

Size: 1.95 KB

Versions: 16

Compression:

Stored size: 1.95 KB

Contents

module Watir
  module Scrolling
    def scroll
      Scroll.new(self)
    end
  end

  class Scroll
    def initialize(object)
      @object = object
    end

    # Scrolls by offset.
    # @param [Fixnum] left Horizontal offset
    # @param [Fixnum] top Vertical offset
    #
    def by(left, top)
      @object.browser.execute_script('window.scrollBy(arguments[0], arguments[1]);', Integer(left), Integer(top))
      self
    end

    #
    # Scrolls to specified location.
    # @param [Symbol] param
    #
    def to(param = :top)
      args = @object.is_a?(Watir::Element) ? element_scroll(param) : browser_scroll(param)
      raise ArgumentError, "Don't know how to scroll #{@object} to: #{param}!" if args.nil?

      @object.browser.execute_script(*args)
      self
    end

    private

    def element_scroll(param)
      script = case param
               when :top, :start
                 'arguments[0].scrollIntoView();'
               when :center
                 <<-JS
                   var bodyRect = document.body.getBoundingClientRect();
                   var elementRect = arguments[0].getBoundingClientRect();
                   var left = (elementRect.left - bodyRect.left) - (window.innerWidth / 2);
                   var top = (elementRect.top - bodyRect.top) - (window.innerHeight / 2);
                   window.scrollTo(left, top);
                 JS
               when :bottom, :end
                 'arguments[0].scrollIntoView(false);'
               else
                 return nil
               end
      [script, @object]
    end

    def browser_scroll(param)
      case param
      when :top, :start
        'window.scrollTo(0, 0);'
      when :center
        'window.scrollTo(window.outerWidth / 2, window.outerHeight / 2);'
      when :bottom, :end
        'window.scrollTo(0, document.body.scrollHeight);'
      when Array
        ['window.scrollTo(arguments[0], arguments[1]);', Integer(param[0]), Integer(param[1])]
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
watir-7.0.0 lib/watir/scroll.rb
watir-7.0.0.beta5 lib/watir/scroll.rb
watir-7.0.0.beta4 lib/watir/scroll.rb
watir-7.0.0.beta3 lib/watir/scroll.rb
watir-7.0.0.beta2 lib/watir/scroll.rb
watir-7.0.0.beta1 lib/watir/scroll.rb
watir-6.19.1 lib/watir/scroll.rb
watir-6.19.0 lib/watir/scroll.rb
watir-6.18.0 lib/watir/scroll.rb
watir-6.17.0 lib/watir/scroll.rb
watir-6.16.5 lib/watir/scroll.rb
watir-6.16.4 lib/watir/scroll.rb
watir-6.16.3 lib/watir/scroll.rb
watir-6.16.2 lib/watir/scroll.rb
watir-6.16.1 lib/watir/scroll.rb
watir-6.16.0 lib/watir/scroll.rb