Class: Watir::Scroll

Inherits:
Object
  • Object
show all
Defined in:
lib/watir/scroll.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Scroll

Returns a new instance of Scroll.



11
12
13
14
15
# File 'lib/watir/scroll.rb', line 11

def initialize(object)
  @driver = object.browser.wd
  @object = object
  @origin = nil
end

Instance Method Details

#by(left, top) ⇒ Object

Scrolls by offset.

Parameters:

  • left (Fixnum)

    Horizontal offset

  • top (Fixnum)

    Vertical offset



21
22
23
24
25
26
27
28
# File 'lib/watir/scroll.rb', line 21

def by(left, top)
  if @origin
    @driver.action.scroll_from(@origin, left, top).perform
  else
    @object.browser.execute_script('window.scrollBy(arguments[0], arguments[1]);', Integer(left), Integer(top))
  end
  self
end

#from(x_offset = 0, y_offset = 0) ⇒ Object

Sets an origin point for a future scroll

Parameters:

  • x_offset (Integer) (defaults to: 0)
  • y_offset (Integer) (defaults to: 0)


46
47
48
49
50
51
52
53
# File 'lib/watir/scroll.rb', line 46

def from(x_offset = 0, y_offset = 0)
  @origin = if @object.is_a?(Watir::Element)
              Selenium::WebDriver::WheelActions::ScrollOrigin.element(@object.we, x_offset, y_offset)
            else
              Selenium::WebDriver::WheelActions::ScrollOrigin.viewport(x_offset, y_offset)
            end
  self
end

#to(param = :top) ⇒ Object

Scrolls to specified location.

Parameters:

  • param (Symbol) (defaults to: :top)

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
# File 'lib/watir/scroll.rb', line 33

def to(param = :top)
  return scroll_to_element if param == :viewport

  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