1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/spec/session/scroll_spec.rb
Eric Anderson b2e2a27777
Correct document scroll element
Previously was using "body" tag but that only works in quirks mode.
This uses the "html" tag which is what works in standards mode.

Updated the expectations to also not use the body tag but instead
use `documentElement` which is the suggested accessor in JS to use as
it will return the right element depending on the mode. This ensures
the measurement is done correctly in standards mode also.

----

The previous commit indicated some failures with the frame specs also.
I ran the entire test suite twice and both times saw those frame
failures. But now when running just those frame tests I get no failures.
Futhermore when I run the entire test suite with these scroll fixes in
place I get no failures either.

For now will wait to see what CI says but I think it might have been
some random failure or state leftover from another test.
2021-07-22 15:28:21 -04:00

117 lines
5.6 KiB
Ruby

# frozen_string_literal: true
Capybara::SpecHelper.spec '#scroll_to', requires: [:scroll] do
before do
@session.visit('/scroll')
end
it 'can scroll an element to the top of the viewport' do
el = @session.find(:css, '#scroll')
@session.scroll_to(el, align: :top)
expect(el.evaluate_script('this.getBoundingClientRect().top')).to be_within(1).of(0)
end
it 'can scroll an element to the bottom of the viewport' do
el = @session.find(:css, '#scroll')
@session.scroll_to(el, align: :bottom)
el_bottom = el.evaluate_script('this.getBoundingClientRect().bottom')
viewport_bottom = el.evaluate_script('document.documentElement.clientHeight')
expect(el_bottom).to be_within(1).of(viewport_bottom)
end
it 'can scroll an element to the center of the viewport' do
el = @session.find(:css, '#scroll')
@session.scroll_to(el, align: :center)
el_center = el.evaluate_script('(function(rect){return (rect.top + rect.bottom)/2})(this.getBoundingClientRect())')
viewport_bottom = el.evaluate_script('document.documentElement.clientHeight')
expect(el_center).to be_within(2).of(viewport_bottom / 2)
end
it 'can scroll the window to the vertical top' do
@session.scroll_to :bottom
@session.scroll_to :top
expect(@session.evaluate_script('[window.scrollX || window.pageXOffset, window.scrollY || window.pageYOffset]')).to eq [0, 0]
end
it 'can scroll the window to the vertical bottom' do
@session.scroll_to :bottom
max_scroll = @session.evaluate_script('document.documentElement.scrollHeight - document.documentElement.clientHeight')
expect(@session.evaluate_script('[window.scrollX || window.pageXOffset, window.scrollY || window.pageYOffset]')).to eq [0, max_scroll]
end
it 'can scroll the window to the vertical center' do
@session.scroll_to :center
max_scroll = @session.evaluate_script('document.documentElement.scrollHeight - document.documentElement.clientHeight')
expect(@session.evaluate_script('[window.scrollX || window.pageXOffset, window.scrollY || window.pageYOffset]')).to eq [0, max_scroll / 2]
end
it 'can scroll the window to specific location' do
@session.scroll_to 100, 100
expect(@session.evaluate_script('[window.scrollX || window.pageXOffset, window.scrollY || window.pageYOffset]')).to eq [100, 100]
end
it 'can scroll an element to the top of the scrolling element' do
scrolling_element = @session.find(:css, '#scrollable')
el = scrolling_element.find(:css, '#inner')
scrolling_element.scroll_to(el, align: :top)
scrolling_element_top = scrolling_element.evaluate_script('this.getBoundingClientRect().top')
expect(el.evaluate_script('this.getBoundingClientRect().top')).to be_within(3).of(scrolling_element_top)
end
it 'can scroll an element to the bottom of the scrolling element' do
scrolling_element = @session.find(:css, '#scrollable')
el = scrolling_element.find(:css, '#inner')
scrolling_element.scroll_to(el, align: :bottom)
el_bottom = el.evaluate_script('this.getBoundingClientRect().bottom')
scroller_bottom = scrolling_element.evaluate_script('this.getBoundingClientRect().top + this.clientHeight')
expect(el_bottom).to be_within(1).of(scroller_bottom)
end
it 'can scroll an element to the center of the scrolling element' do
scrolling_element = @session.find(:css, '#scrollable')
el = scrolling_element.find(:css, '#inner')
scrolling_element.scroll_to(el, align: :center)
el_center = el.evaluate_script('(function(rect){return (rect.top + rect.bottom)/2})(this.getBoundingClientRect())')
scrollable_center = scrolling_element.evaluate_script('(this.clientHeight / 2) + this.getBoundingClientRect().top')
expect(el_center).to be_within(1).of(scrollable_center)
end
it 'can scroll the scrolling element to the top' do
scrolling_element = @session.find(:css, '#scrollable')
scrolling_element.scroll_to :bottom
scrolling_element.scroll_to :top
expect(scrolling_element.evaluate_script('[this.scrollLeft, this.scrollTop]')).to eq [0, 0]
end
it 'can scroll the scrolling element to the bottom' do
scrolling_element = @session.find(:css, '#scrollable')
scrolling_element.scroll_to :bottom
max_scroll = scrolling_element.evaluate_script('this.scrollHeight - this.clientHeight')
expect(scrolling_element.evaluate_script('[this.scrollLeft, this.scrollTop]')).to eq [0, max_scroll]
end
it 'can scroll the scrolling element to the vertical center' do
scrolling_element = @session.find(:css, '#scrollable')
scrolling_element.scroll_to :center
max_scroll = scrolling_element.evaluate_script('this.scrollHeight - this.clientHeight')
expect(scrolling_element.evaluate_script('[this.scrollLeft, this.scrollTop]')).to eq [0, max_scroll / 2]
end
it 'can scroll the scrolling element to specific location' do
scrolling_element = @session.find(:css, '#scrollable')
scrolling_element.scroll_to 100, 100
expect(scrolling_element.evaluate_script('[this.scrollLeft, this.scrollTop]')).to eq [100, 100]
end
it 'can scroll the window by a specific amount' do
@session.scroll_to(:current, offset: [50, 75])
expect(@session.evaluate_script('[window.scrollX || window.pageXOffset, window.scrollY || window.pageYOffset]')).to eq [50, 75]
end
it 'can scroll the scroll the scrolling element by a specific amount' do
scrolling_element = @session.find(:css, '#scrollable')
scrolling_element.scroll_to 100, 100
scrolling_element.scroll_to(:current, offset: [-50, 50])
expect(scrolling_element.evaluate_script('[this.scrollLeft, this.scrollTop]')).to eq [50, 150]
end
end