Skip to content

Commit 4f88ad1

Browse files
committed
add Ferrum::Mouse#scroll_by
1 parent 156b020 commit 4f88ad1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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)