spec/watirspec/elements/element_spec.rb in watir-7.0.0 vs spec/watirspec/elements/element_spec.rb in watir-7.1.0

- old
+ new

@@ -621,20 +621,70 @@ expect(h2.flash(:long)).to eq h2 end end describe '#hover' do - it 'should hover over the element', except: {browser: :ie} do - browser.goto WatirSpec.url_for('hover.html') - link = browser.a + def element_color(element) + case element.style('color') + when 'rgba(0, 0, 255, 1)' + :blue + when 'rgba(255, 165, 0, 1)', 'rgb(255, 165, 0)' + :orange + else + raise rgba + end + end - expect(link.style('font-size')).to eq '10px' - link.scroll.to - link.hover - link.wait_until { |l| l.style('font-size') == '20px' } - expect(link.style('font-size')).to eq '20px' + it 'allows scrolling to top', except: {browser: :ie, + reason: 'needs require_window_focus'} do + browser.goto(WatirSpec.url_for('scroll.html')) + element = browser.div(id: 'center') + + element.hover(scroll_to: :top) + expect(element_color(element)).to eq :orange + + element_top = browser.execute_script('return arguments[0].getBoundingClientRect().top', element) + expect(element_top).to be_within(1).of(0) end + + it 'scrolls to center by default', except: {browser: :ie, + reason: 'needs require_window_focus'} do + browser.goto(WatirSpec.url_for('scroll.html')) + element = browser.div(id: 'center') + + element.hover + expect(element_color(element)).to eq :orange + + element_rect = browser.execute_script('return arguments[0].getBoundingClientRect()', element) + + expect(element_rect['top']).to eq(element_rect['bottom'] - element_rect['height']) + end + + it 'allows scrolling to bottom', except: {browser: :ie, + reason: 'needs require_window_focus'} do + browser.goto(WatirSpec.url_for('scroll.html')) + element = browser.div(id: 'center') + + element.hover(scroll_to: :bottom) + expect(element_color(element)).to eq :orange + + element_bottom = browser.execute_script('return arguments[0].getBoundingClientRect().bottom', element) + window_height = browser.execute_script('return window.innerHeight') + + expect(element_bottom).to be_within(1).of(window_height) + end + + it 'allows not scrolling', except: {browser: %i[chrome edge], + reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3955'} do + browser.goto(WatirSpec.url_for('scroll.html')) + element = browser.div(id: 'center') + + browser.execute_script('return window.pageYOffset;') + browser.execute_script('return window.innerHeight;') + + expect { element.hover(scroll_to: nil) }.to raise_exception Selenium::WebDriver::Error::MoveTargetOutOfBoundsError + end end describe '#inspect' do before(:each) { browser.goto(WatirSpec.url_for('nested_iframes.html')) } @@ -912,43 +962,66 @@ describe '#obscured?' do before { browser.goto WatirSpec.url_for('obscured.html') } it 'returns false if element center is not covered' do btn = browser.button(id: 'not_obscured') - expect(btn).not_to be_obscured + btn.scroll.to :center expect { btn.click }.not_to raise_exception + expect(btn).not_to be_obscured end it 'returns false if element center is covered by its descendant' do btn = browser.button(id: 'has_descendant') - expect(btn).not_to be_obscured + btn.scroll.to :center expect { btn.click }.not_to raise_exception + expect(btn).not_to be_obscured end it 'returns true if element center is covered by a non-descendant', except: {browser: :safari, reason: 'not getting element click intercepted'} do btn = browser.button(id: 'obscured') - expect(btn).to be_obscured - + btn.scroll.to :center expect { btn.click }.to raise_exception(Selenium::WebDriver::Error::ElementClickInterceptedError) + expect(btn).to be_obscured end it 'returns false if element center is surrounded by non-descendants' do btn = browser.button(id: 'surrounded') - expect(btn).not_to be_obscured + btn.scroll.to :center expect { btn.click }.not_to raise_exception + expect(btn).not_to be_obscured end - it 'scrolls interactive element into view before checking if obscured' do - btn = browser.button(id: 'requires_scrolling') - expect(btn).not_to be_obscured - expect { btn.click }.not_to raise_exception + it 'correctly scrolls element below viewport' do + browser.goto WatirSpec.url_for('sticky_elements.html') + + element = browser.div(id: 'center') + + browser.scroll.to :top + expect { element.click }.not_to raise_exception + + browser.scroll.to :top + expect(element).not_to be_obscured end + it 'correctly scrolls element above viewport', + except: {browser: %i[chrome edge], + reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3954'} do + browser.goto WatirSpec.url_for('sticky_elements.html') + element = browser.div(id: 'center') + + browser.scroll.to :bottom + expect { element.click }.not_to raise_exception + + browser.scroll.to :bottom + expect(element).not_to be_obscured + end + it 'scrolls non-interactive element into view before checking if obscured' do div = browser.div(id: 'requires_scrolling_container') - expect(div).not_to be_obscured expect { div.click }.not_to raise_exception + browser.scroll.to :top + expect(div).not_to be_obscured end it 'returns true if element cannot be scrolled into view' do btn = browser.button(id: 'off_screen') expect(btn).to be_obscured