Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/interactions/wheel_actions.rb
1990 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
module WheelActions
23
attr_writer :default_scroll_duration
24
25
#
26
# By default this is set to 250ms in the ActionBuilder constructor
27
# It can be overridden with default_scroll_duration=
28
#
29
30
def default_scroll_duration
31
@default_scroll_duration ||= @duration / 1000.0 # convert ms to seconds
32
end
33
34
#
35
# If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.
36
#
37
# @example Scroll to element
38
# el = driver.find_element(id: "some_id")
39
# driver.action.scroll_to(element).perform
40
#
41
# @param [Object] element Which element to scroll into the viewport.
42
# @param [Object] device Which device to use to scroll
43
# @return [Selenium::WebDriver::WheelActions] A self reference.
44
def scroll_to(element, device: nil)
45
scroll(origin: element, device: device)
46
end
47
48
#
49
# Scrolls by provided amounts with the origin in the top left corner of the viewport.
50
#
51
# @example Scroll viewport by a specified amount
52
# el = driver.find_element(id: "some_id")
53
# driver.action.scroll_by(100, 200).perform
54
#
55
# @param [Integer] delta_x Distance along X axis to scroll using the wheel. A negative value scrolls left.
56
# @param [Integer] delta_y Distance along Y axis to scroll using the wheel. A negative value scrolls up.
57
# @return [Selenium::WebDriver::WheelActions] A self reference.
58
def scroll_by(delta_x, delta_y, device: nil)
59
scroll(delta_x: delta_x, delta_y: delta_y, device: device)
60
end
61
62
#
63
# Scrolls by provided amount based on a provided origin.
64
#
65
# The scroll origin is either the center of an element or the upper left of the viewport plus any offsets.
66
# If the origin is an element, and the element is not in the viewport, the bottom of the element will first
67
# be scrolled to the bottom of the viewport.
68
#
69
# @example Scroll from element by a specified amount
70
# el = driver.find_element(id: "some_id")
71
# origin = WheelActions::ScrollOrigin.element(el)
72
# driver.action.scroll_from(origin, 0, 200).perform
73
#
74
# @example Scroll from element by a specified amount with an offset
75
# el = driver.find_element(id: "some_id")
76
# origin = WheelActions::ScrollOrigin.element(el, 10, 10)
77
# driver.action.scroll_from(origin, 100, 200).perform
78
#
79
# @example Scroll viewport by a specified amount with an offset
80
# origin = WheelActions::ScrollOrigin.viewport(10, 10)
81
# driver.action.scroll_from(origin, 0, 200).perform
82
#
83
# @param [ScrollOrigin] scroll_origin Where scroll originates (viewport or element center) plus provided offsets.
84
# @param [Integer] delta_x Distance along X axis to scroll using the wheel. A negative value scrolls left.
85
# @param [Integer] delta_y Distance along Y axis to scroll using the wheel. A negative value scrolls up.
86
# @return [Selenium::WebDriver::WheelActions] A self reference.
87
# @raise [Error::MoveTargetOutOfBoundsError] If the origin with offset is outside the viewport.
88
def scroll_from(scroll_origin, delta_x, delta_y, device: nil)
89
raise TypeError, "#{scroll_origin.inspect} isn't a valid ScrollOrigin" unless scroll_origin.is_a?(ScrollOrigin)
90
91
scroll(x: scroll_origin.x_offset,
92
y: scroll_origin.y_offset,
93
delta_x: delta_x,
94
delta_y: delta_y,
95
origin: scroll_origin.origin,
96
device: device)
97
end
98
99
private
100
101
def scroll(**opts)
102
opts[:duration] = default_scroll_duration
103
wheel = wheel_input(opts.delete(:device))
104
wheel.create_scroll(**opts)
105
tick(wheel)
106
self
107
end
108
109
def wheel_input(name = nil)
110
device(name: name, type: Interactions::WHEEL) || add_wheel_input('wheel')
111
end
112
end # WheelActions
113
end # WebDriver
114
end # Selenium
115
116