Skip to content

Commit

Permalink
add Ferrum::Mouse#scroll_by
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Jan 8, 2025
1 parent 156b020 commit 485a948
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

### Added

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

### Changed

Expand Down
17 changes: 17 additions & 0 deletions lib/ferrum/mouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ def initialize(page)
@buttons = 0
end

#
# Scroll page by the given amount x, y.
#
# @param [Integer] x
# The horizontal pixel value that you want to scroll by.
#
# @param [Integer] y
# The vertical pixel value that you want to scroll by.
#
# @example
# browser.go_to("https://www.google.com/search?q=Ruby+headless+driver+for+Capybara")
# browser.mouse.scroll_by(0, 400)
#
def scroll_by(x, y)
tap { @page.execute("window.scrollBy(#{x}, #{y})") }
end

#
# Scroll page to a given x, y coordinates.
#
Expand Down
13 changes: 13 additions & 0 deletions spec/mouse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
end
end

describe "#scroll_by" do
it "allows the page to be scrolled" do
browser.go_to("/ferrum/long_page")
browser.resize(width: 10, height: 10)
browser.mouse.scroll_by(30, 70)
browser.mouse.scroll_by(40, -50)
browser.mouse.scroll_by(-60, 0)
expect(
browser.evaluate("[window.scrollX, window.scrollY]")
).to eq([10, 20])
end
end

describe "#scroll_to" do
it "allows the page to be scrolled" do
browser.go_to("/ferrum/long_page")
Expand Down

0 comments on commit 485a948

Please sign in to comment.