Skip to content

Commit d7e221b

Browse files
authored
add Ferrum::Mouse#scroll_by (#514)
1 parent cbdcb07 commit d7e221b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
### Added
44

5-
`Ferrum::Network::Request#headers` are enhanced and supplemented with `Network.requestWillBeSentExtraInfo` [#506]
6-
`Ferrum::Page#off` to unsubscribe from CDP events [#455]
5+
- `Ferrum::Network::Request#headers` are enhanced and supplemented with `Network.requestWillBeSentExtraInfo` [#506]
6+
- `Ferrum::Page#off` to unsubscribe from CDP events [#455]
7+
- `Ferrum::Mouse#scroll_by` to be able to scroll by, as alternative to `scroll_to` [#514]
78

89
### Changed
910

lib/ferrum/mouse.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ def initialize(page)
1818
@buttons = 0
1919
end
2020

21+
#
22+
# Scroll page by the given amount x, y.
23+
#
24+
# @param [Integer] x
25+
# The horizontal pixel value that you want to scroll by.
26+
#
27+
# @param [Integer] y
28+
# The vertical pixel value that you want to scroll by.
29+
#
30+
# @example
31+
# browser.go_to("https://www.google.com/search?q=Ruby+headless+driver+for+Capybara")
32+
# browser.mouse.scroll_by(0, 400)
33+
#
34+
def scroll_by(x, y)
35+
tap { @page.execute("window.scrollBy(#{x}, #{y})") }
36+
end
37+
2138
#
2239
# Scroll page to a given x, y coordinates.
2340
#

spec/mouse_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@
3232
end
3333
end
3434

35+
describe "#scroll_by" do
36+
it "allows the page to be scrolled" do
37+
browser.go_to("/ferrum/long_page")
38+
browser.resize(width: 10, height: 10)
39+
browser.mouse.scroll_by(30, 70)
40+
browser.mouse.scroll_by(40, -50)
41+
browser.mouse.scroll_by(-60, 0)
42+
expect(
43+
browser.evaluate("[window.scrollX, window.scrollY]")
44+
).to eq([10, 20])
45+
end
46+
end
47+
3548
describe "#scroll_to" do
3649
it "allows the page to be scrolled" do
3750
browser.go_to("/ferrum/long_page")

0 commit comments

Comments
 (0)