File: wheel_actions.rb

package info (click to toggle)
ruby-selenium-webdriver 4.38.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,400 kB
  • sloc: ruby: 7,721; javascript: 125; makefile: 6
file content (114 lines) | stat: -rw-r--r-- 4,891 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
  module WebDriver
    module WheelActions
      attr_writer :default_scroll_duration

      #
      # By default this is set to 250ms in the ActionBuilder constructor
      # It can be overridden with default_scroll_duration=
      #

      def default_scroll_duration
        @default_scroll_duration ||= @duration / 1000.0 # convert ms to seconds
      end

      #
      # If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.
      #
      # @example Scroll to element
      #    el = driver.find_element(id: "some_id")
      #    driver.action.scroll_to(element).perform
      #
      # @param [Object] element Which element to scroll into the viewport.
      # @param [Object] device Which device to use to scroll
      # @return [Selenium::WebDriver::WheelActions] A self reference.
      def scroll_to(element, device: nil)
        scroll(origin: element, device: device)
      end

      #
      # Scrolls by provided amounts with the origin in the top left corner of the viewport.
      #
      # @example Scroll viewport by a specified amount
      #    el = driver.find_element(id: "some_id")
      #    driver.action.scroll_by(100, 200).perform
      #
      # @param [Integer] delta_x Distance along X axis to scroll using the wheel. A negative value scrolls left.
      # @param [Integer] delta_y Distance along Y axis to scroll using the wheel. A negative value scrolls up.
      # @return [Selenium::WebDriver::WheelActions] A self reference.
      def scroll_by(delta_x, delta_y, device: nil)
        scroll(delta_x: delta_x, delta_y: delta_y, device: device)
      end

      #
      # Scrolls by provided amount based on a provided origin.
      #
      # The scroll origin is either the center of an element or the upper left of the viewport plus any offsets.
      # If the origin is an element, and the element is not in the viewport, the bottom of the element will first
      #   be scrolled to the bottom of the viewport.
      #
      # @example Scroll from element by a specified amount
      #    el = driver.find_element(id: "some_id")
      #    origin = WheelActions::ScrollOrigin.element(el)
      #    driver.action.scroll_from(origin, 0, 200).perform
      #
      # @example Scroll from element by a specified amount with an offset
      #    el = driver.find_element(id: "some_id")
      #    origin = WheelActions::ScrollOrigin.element(el, 10, 10)
      #    driver.action.scroll_from(origin, 100, 200).perform
      #
      # @example Scroll viewport by a specified amount with an offset
      #    origin = WheelActions::ScrollOrigin.viewport(10, 10)
      #    driver.action.scroll_from(origin, 0, 200).perform
      #
      # @param [ScrollOrigin] scroll_origin Where scroll originates (viewport or element center) plus provided offsets.
      # @param [Integer] delta_x Distance along X axis to scroll using the wheel. A negative value scrolls left.
      # @param [Integer] delta_y Distance along Y axis to scroll using the wheel. A negative value scrolls up.
      # @return [Selenium::WebDriver::WheelActions] A self reference.
      # @raise [Error::MoveTargetOutOfBoundsError] If the origin with offset is outside the viewport.
      def scroll_from(scroll_origin, delta_x, delta_y, device: nil)
        raise TypeError, "#{scroll_origin.inspect} isn't a valid ScrollOrigin" unless scroll_origin.is_a?(ScrollOrigin)

        scroll(x: scroll_origin.x_offset,
               y: scroll_origin.y_offset,
               delta_x: delta_x,
               delta_y: delta_y,
               origin: scroll_origin.origin,
               device: device)
      end

      private

      def scroll(**opts)
        opts[:duration] = default_scroll_duration
        wheel = wheel_input(opts.delete(:device))
        wheel.create_scroll(**opts)
        tick(wheel)
        self
      end

      def wheel_input(name = nil)
        device(name: name, type: Interactions::WHEEL) || add_wheel_input('wheel')
      end
    end # WheelActions
  end # WebDriver
end # Selenium