20
20
module Selenium
21
21
module WebDriver
22
22
module WheelActions
23
+ def default_scroll_duration
24
+ @default_scroll_duration ||= 0.25 # 250 milliseconds
25
+ end
26
+
27
+ #
28
+ # Scrolls the viewport so that the provided element is at the bottom. Then the viewport
29
+ # is further scrolled by the provided x and y offsets.
30
+ #
31
+ # @example Scroll to element
32
+ #
33
+ # el = driver.find_element(id: "some_id")
34
+ # driver.action.scroll_to(el).perform
35
+ #
36
+ # @example Scroll to offset from element
37
+ #
38
+ # el = driver.find_element(id: "some_id")
39
+ # driver.action.scroll_to(el, 0, 1000).perform
40
+ #
41
+ # @param [Selenium::WebDriver::Element] element to scroll to.
42
+ # @param [Integer] x Optional horizontal offset to scroll from the center of the element.
43
+ # A negative value means scrolling left.
44
+ # @param [Integer] y Optional vertical offset to scroll from the center of the element.
45
+ # A negative value means scrolling up.
46
+ # @param [Symbol || String] device optional name of the WheelInput device to scroll with.
47
+ # @return [ActionBuilder] A self reference.
48
+ #
23
49
24
- # This scrolls an element to the bottom of the viewport
25
- def scroll_to ( element , device : nil )
26
- scroll_from ( element , device : device )
50
+ def scroll_to ( element , x = 0 , y = 0 , device : nil )
51
+ scroll ( x , y , origin : ScrollOrigin . element ( element , 0 , 0 ) , device : device )
27
52
end
28
53
29
- # This scrolls from the provided element
30
- # The origin of the scroll is the center of the element plus the
31
- # offset amounts in origin_offset_x and origin_offset_y
32
- # The amount of scrolling is the value of right_by and down_by
33
- def scroll_from ( element , right_by = 0 , down_by = 0 , origin_offset_x : 0 , origin_offset_y : 0 , device : nil )
34
- wheel = wheel_input ( device )
35
- wheel . create_scroll ( x : Integer ( origin_offset_x ) ,
36
- y : Integer ( origin_offset_y ) ,
37
- delta_x : Integer ( right_by ) ,
38
- delta_y : Integer ( down_by ) ,
39
- origin : element ,
40
- duration : default_move_duration )
41
- tick ( wheel )
42
- self
54
+ #
55
+ # Scrolls the viewport from its current position by the provided offset.
56
+ # The origin source is the upper left corner of the viewport
57
+ #
58
+ # @example Scroll by the provided amount
59
+ #
60
+ # driver.action.scroll_by(0, 1000).perform
61
+ #
62
+ # @param [Integer] x horizontal offset. A negative value means scrolling left.
63
+ # @param [Integer] y vertical offset. A negative value means scrolling up.
64
+ # @param [Symbol || String] device Optional name of the WheelInput device to scroll with
65
+ # @return [ActionBuilder] A self reference.
66
+ #
67
+
68
+ def scroll_by ( x = 0 , y = 0 , device : nil )
69
+ scroll ( x , y , origin : ScrollOrigin . viewport ( 0 , 0 ) , device : device )
43
70
end
44
71
45
- # The origin of the scroll will the upper left corner of the viewport plus the
46
- # offset amounts in origin_x and origin_y
47
- # The amount of scrolling is the value of right_by and down_by
48
- def scroll_by ( right_by = 0 , down_by = 0 , origin_x : 0 , origin_y : 0 , device : nil )
72
+ #
73
+ # Scrolls the viewport based on a ScrollOrigin.
74
+ #
75
+ # This method is needed instead of #scroll_to or #scroll_by
76
+ # when what needs to be scrolled is only in a portion of the viewport.
77
+ # The origin can be thought of as where on the screen you put the mouse when
78
+ # executing a wheel scroll, or where you put your cursor when swiping a touch pad, etc.
79
+ #
80
+ # The offset for the origin is referenced to either the upper left of the viewport or the center of the element
81
+ # The methods ScrollOrigin.viewport and ScrollOrigin.element are provided to ensure correct syntax
82
+ #
83
+ # @example Scroll by the provided amount originating from a source offset from upper left of the viewport
84
+ #
85
+ # el = driver.find_element(id: "some_id")
86
+ # driver.action.scroll(0, 100, ScrollOrigin.viewport(400, 200)).perform
87
+ #
88
+ # @example Scroll by the provided amount originating from a source offset from the center of the provided element
89
+ #
90
+ # el = driver.find_element(id: "some_id")
91
+ # driver.action.scroll(0, 100, ScrollOrigin.element(element, x: -400, 100)).perform
92
+ #
93
+ # @see ScrollOrigin
94
+ #
95
+ # @param [Integer] x horizontal offset. A negative value means scrolling left.
96
+ # @param [Integer] y vertical offset. A negative value means scrolling up.
97
+ # @param [Hash] origin The location the scroll originates from
98
+ # @param [Symbol || String] device Optional name of the WheelInput device to scroll with
99
+ # @return [ActionBuilder] A self reference.
100
+ # @raise [MoveTargetOutOfBoundsError] if the origin value is outside the viewport.
101
+ #
102
+
103
+ def scroll ( x , y , origin : ScrollOrigin . viewport ( 0 , 0 ) , device : nil )
49
104
wheel = wheel_input ( device )
50
- wheel . create_scroll ( x : Integer ( origin_x ) ,
51
- y : Integer ( origin_y ) ,
52
- delta_x : Integer ( right_by ) ,
53
- delta_y : Integer ( down_by ) ,
54
- origin : Interactions ::Scroll ::VIEWPORT ,
55
- duration : default_move_duration )
105
+ opts = { delta_x : Integer ( x ) ,
106
+ delta_y : Integer ( y ) ,
107
+ duration : default_scroll_duration } . merge! ( origin )
108
+ wheel . create_scroll ( **opts )
56
109
tick ( wheel )
57
110
self
58
111
end
@@ -62,6 +115,6 @@ def scroll_by(right_by = 0, down_by = 0, origin_x: 0, origin_y: 0, device: nil)
62
115
def wheel_input ( name = nil )
63
116
device ( name : name , type : Interactions ::WHEEL ) || add_wheel_input ( 'wheel' )
64
117
end
65
- end # KeyActions
118
+ end # WheelActions
66
119
end # WebDriver
67
120
end # Selenium
0 commit comments