diff --git a/CHANGELOG.md b/CHANGELOG.md index dec10244..6bf76bd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ferrum/mouse.rb b/lib/ferrum/mouse.rb index 6b1c340e..e38d5b57 100644 --- a/lib/ferrum/mouse.rb +++ b/lib/ferrum/mouse.rb @@ -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. # diff --git a/spec/mouse_spec.rb b/spec/mouse_spec.rb index 73ae1a37..8db0f73c 100644 --- a/spec/mouse_spec.rb +++ b/spec/mouse_spec.rb @@ -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")