Sha256: 8a4d839050d0fbde1805c169d46bfefc1e5199218f356ce8b0f42f84af80d2c3

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Watir
  class Browser
    class Scroll
      def initialize(browser)
        @browser = browser
      end

      def to(param = nil)
        if !param.nil? && param.is_a?(Array)
          unless param.is_a?(Array)
            raise ArgumentError, "Unable to scroll to: #{param}."
          end

          execute(
            'window.scrollTo(arguments[0], arguments[1]);',
            Integer(param[0]), Integer(param[1])
          )
        end
        self
      end

      def top
        execute('window.scrollTo(0, 0);')
        self
      end

      def bottom
        execute('window.scrollTo(0, document.body.scrollHeight);')
        self
      end

      def middle
        execute(
          'window.scrollTo(window.outerWidth / 2, window.outerHeight / 2);'
        )
        self
      end

      def by(left, top)
        execute(
          'window.scrollBy(arguments[0], arguments[1]);',
          Integer(left), Integer(top)
        )
        self
      end

      private

      def execute(*args)
        @browser.execute_script(*args)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
browser_scroll-0.5.0 lib/browser_scroll/browser/scroll.rb