diff --git a/CHANGELOG.md b/CHANGELOG.md index 36a939c..696e2df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ [rubycdp/ferrum#689] ### Fixed +- `scroll_to` with an element or a location waits for the scroll like `scroll_to(x, y)` and `scroll_by` already do, + instead of making Ferrum warn that the script returns a promise [#362] ### Removed - Methods deprecated in 0.20, see its Deprecated section for replacements diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index a7a81b8..c17f190 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -338,7 +338,7 @@ def scroll_element_to_location(element, location) else raise ArgumentError, "Invalid scroll_to location: #{location}" end - element.node.evaluate("this.scrollIntoView(#{scroll_opts})") + element.node.evaluate("await this.scrollIntoView(#{scroll_opts})") end def scroll_to_location(location) @@ -346,7 +346,7 @@ def scroll_to_location(location) bottom: "this.scrollHeight", center: "(this.scrollHeight - this.clientHeight) / 2" } - node.evaluate("this.scrollTo(0, #{height[location]})") + node.evaluate("await this.scrollTo(0, #{height[location]})") end def scroll_to_coords(x, y) diff --git a/spec/node_spec.rb b/spec/node_spec.rb index 9343a4b..e2a17c2 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -1201,5 +1201,32 @@ @driver.evaluate_script("[window.scrollX, window.scrollY]") ).to eq([200, 500]) end + + context "when Ferrum raises on deprecations" do + around do |example| + previous = ENV.fetch("FERRUM_DEPRECATION_WARNINGS", nil) + ENV["FERRUM_DEPRECATION_WARNINGS"] = "raise" + example.run + ensure + ENV["FERRUM_DEPRECATION_WARNINGS"] = previous + end + + it "scrolls an element to the bottom of the window" do + @session.visit("/cuprite/scroll_long_page") + element = @session.find(:css, "#middleish") + @session.scroll_to(element, align: :bottom) + + bottom = element.evaluate_script("this.getBoundingClientRect().bottom") + expect(bottom).to be_within(1).of(@driver.evaluate_script("window.innerHeight")) + end + + it "scrolls the window to the bottom" do + @session.visit("/cuprite/scroll_long_page") + @session.scroll_to(:bottom) + + max_scroll = @driver.evaluate_script("document.documentElement.scrollHeight - window.innerHeight") + expect(@driver.evaluate_script("window.scrollY")).to be_within(0.5).of(max_scroll) + end + end end end