Skip to content

Commit d692f80

Browse files
committed
[rb] change ruby implementation of scroll to match other languages
1 parent c5fbd61 commit d692f80

15 files changed

Lines changed: 97 additions & 199 deletions

File tree

py/selenium/webdriver/common/action_chains.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def send_keys_to_element(self, element, *keys_to_send):
321321
self.send_keys(*keys_to_send)
322322
return self
323323

324-
def scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int = 0, origin: str = "viewport"):
324+
def scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int = 0, origin= "viewport"):
325325
"""
326326
Sends wheel scroll information to the browser to be processed.
327327
@@ -330,6 +330,7 @@ def scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int = 0,
330330
- y: starting Y coordinate
331331
- delta_x: the distance the mouse will scroll on the x axis
332332
- delta_y: the distance the mouse will scroll on the y axis
333+
- origin: element whose center is the origination point of the scroll; the default is upper left of viewport.
333334
"""
334335
self.w3c_actions.wheel_action.scroll(x=x, y=y, delta_x=delta_x, delta_y=delta_y,
335336
duration=duration, origin=origin)

py/test/selenium/webdriver/common/interactions_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def test_can_scroll_from_viewport_by_amount(driver, pages):
320320
footer = driver.find_element(By.TAG_NAME, "footer")
321321
y = footer.rect['y']
322322

323-
ActionChains(driver).scroll(0, 0, 0, y, origin="viewport").pause(0.2).perform()
323+
ActionChains(driver).scroll(0, 0, 0, y).pause(0.2).perform()
324324

325325
assert _in_viewport(driver, footer)
326326

@@ -331,7 +331,7 @@ def test_can_scroll_from_viewport_with_offset_by_amount(driver, pages):
331331
pages.load("scrolling_tests/frame_with_nested_scrolling_frame.html")
332332
iframe = driver.find_element(By.TAG_NAME, "iframe")
333333

334-
ActionChains(driver).scroll(10, 10, 0, 200, origin="viewport").pause(0.2).perform()
334+
ActionChains(driver).scroll(10, 10, 0, 200).pause(0.2).perform()
335335

336336
driver.switch_to.frame(iframe)
337337
checkbox = driver.find_element(By.NAME, "scroll_checkbox")
@@ -345,7 +345,7 @@ def test_errors_when_origin_offset_not_in_viewport(driver, pages):
345345
pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")
346346

347347
with pytest.raises(MoveTargetOutOfBoundsException):
348-
ActionChains(driver).scroll(-10, -10, 0, 200, origin="viewport").perform()
348+
ActionChains(driver).scroll(-10, -10, 0, 200).perform()
349349

350350

351351
@pytest.mark.xfail_firefox

rb/.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,5 @@ RSpec/FactoryBot/SyntaxMethods: # new in 2.7
355355
Enabled: true
356356
RSpec/Rails/AvoidSetupHook: # new in 2.4
357357
Enabled: true
358+
Style/EnvHome: # new in 1.29
359+
Enabled: true

rb/lib/selenium/webdriver/common.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
require 'selenium/webdriver/common/interactions/pointer_input'
5555
require 'selenium/webdriver/common/interactions/scroll'
5656
require 'selenium/webdriver/common/interactions/wheel_input'
57-
require 'selenium/webdriver/common/interactions/scroll_origin'
5857
require 'selenium/webdriver/common/interactions/wheel_actions'
5958
require 'selenium/webdriver/common/action_builder'
6059
require 'selenium/webdriver/common/html5/shared_web_storage'

rb/lib/selenium/webdriver/common/action_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def initialize(bridge, deprecated_mouse = nil, deprecated_keyboard = nil, deprec
6363
add_input(deprecated_mouse)
6464
add_input(deprecated_keyboard)
6565
else
66-
devices.each { |device| add_input(device) }
66+
Array(devices).each { |device| add_input(device) }
6767
end
6868
end
6969

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ def manage
123123
# @see ActionBuilder
124124
#
125125

126-
def action
127-
bridge.action
126+
def action(**opts)
127+
bridge.action(**opts)
128128
end
129129

130130
def mouse

rb/lib/selenium/webdriver/common/interactions/scroll.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ module Interactions
2727
#
2828

2929
class Scroll < Interaction
30-
VIEWPORT = :viewport
31-
POINTER = :pointer
32-
33-
def initialize(source, duration, delta_x, delta_y, origin: VIEWPORT, x: 0, y: 0)
30+
def initialize(source:, x: 0, y: 0, delta_x: 0, delta_y: 0, origin: :viewport, duration: 0.25)
3431
super(source)
3532
@type = :scroll
3633
@duration = duration * 1000

rb/lib/selenium/webdriver/common/interactions/scroll_origin.rb

Lines changed: 0 additions & 36 deletions
This file was deleted.

rb/lib/selenium/webdriver/common/interactions/wheel_actions.rb

Lines changed: 25 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,86 +25,45 @@ def default_scroll_duration
2525
end
2626

2727
#
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.
28+
# This method uses a Scroll Wheel Input to determine where and by how much to scroll
3029
#
31-
# @example Scroll to element
30+
# The scroll happens from an origin plus an offset.
31+
# The amount to scroll is represented by delta_x and delta_y
32+
# The origin is either the center of a provided element or the upper left of the current viewport
33+
# The offset from the origin is represented by x and y
3234
#
35+
# @example Scroll to element
3336
# el = driver.find_element(id: "some_id")
34-
# driver.action.scroll_to(el).perform
35-
#
36-
# @example Scroll to offset from element
37+
# driver.action.scroll(origin: element).perform
3738
#
39+
# @example Scroll from element by a specified amount
3840
# 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-
#
49-
50-
def scroll_to(element, x = 0, y = 0, device: nil)
51-
scroll(x, y, origin: ScrollOrigin.element(element, 0, 0), device: device)
52-
end
53-
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)
70-
end
71-
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
41+
# driver.action.scroll(delta_x: 100, delta_y: 200, origin: element).perform
8442
#
43+
# @example Scroll from element by a specified amount with an offset
8544
# 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
45+
# driver.action.scroll(x: 10, y: 10, delta_x: 100, delta_y: 200, origin: element).perform
8946
#
47+
# @example Scroll viewport by a specified amount
9048
# el = driver.find_element(id: "some_id")
91-
# driver.action.scroll(0, 100, ScrollOrigin.element(element, x: -400, 100)).perform
49+
# driver.action.scroll(delta_x: 100, delta_y: 200).perform
9250
#
93-
# @see ScrollOrigin
51+
# @example Scroll viewport by a specified amount with an offset
52+
# el = driver.find_element(id: "some_id")
53+
# driver.action.scroll(x: 10, y: 10, delta_x: 100, delta_y: 200).perform
9454
#
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
55+
# @option opts [Integer] x The horizontal offset from the origin from which to start the scroll.
56+
# @option opts [Integer] y The vertical offset from the origin from which to start the scroll.
57+
# @option opts [Integer] delta_x Distance along X axis to scroll using the wheel. A negative value scrolls left.
58+
# @option opts [Integer] delta_y Distance along Y axis to scroll using the wheel. A negative value scrolls up.
59+
# @option opts [String, Element] origin The origin of the scroll, either the viewport or the center of an element.
9960
# @return [ActionBuilder] A self reference.
100-
# @raise [MoveTargetOutOfBoundsError] if the origin value is outside the viewport.
61+
# @raise [MoveTargetOutOfBoundsError] if the origin plus offset is located outside the viewport.
10162
#
10263

103-
def scroll(x, y, origin: ScrollOrigin.viewport(0, 0), device: nil)
104-
wheel = wheel_input(device)
105-
opts = {delta_x: Integer(x),
106-
delta_y: Integer(y),
107-
duration: default_scroll_duration}.merge!(origin)
64+
def scroll(**opts)
65+
opts[:duration] = default_scroll_duration
66+
wheel = wheel_input(opts.delete(:device))
10867
wheel.create_scroll(**opts)
10968
tick(wheel)
11069
self

rb/lib/selenium/webdriver/common/interactions/wheel_input.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def initialize(name = nil)
3232
@type = Interactions::WHEEL
3333
end
3434

35-
def create_scroll(duration: 0, x: 0, y: 0, delta_x: 0, delta_y: 0, origin: nil)
36-
add_action(Scroll.new(self, duration, delta_x, delta_y, origin: origin, x: x, y: y))
35+
def create_scroll(**opts)
36+
opts[:source] = self
37+
add_action(Scroll.new(**opts))
3738
end
3839
end # PointerInput
3940
end # Interactions

0 commit comments

Comments
 (0)