# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from six.moves.urllib.parse import quote
from marionette_driver.by import By
from marionette_driver.errors import MoveTargetOutOfBoundsException
from marionette_harness import MarionetteTestCase
def inline(doc):
return "data:text/html;charset=utf-8,{}".format(quote(doc))
class TestClickScrolling(MarionetteTestCase):
def test_clicking_on_anchor_scrolls_page(self):
self.marionette.navigate(
inline(
"""
Link to content
Text
"""
)
)
# Focusing on to click, but not actually following,
# the link will scroll it in to view, which is a few
# pixels further than 0
self.marionette.find_element(By.CSS_SELECTOR, "a").click()
y_offset = self.marionette.execute_script(
"""
var pageY;
if (typeof(window.pageYOffset) == 'number') {
pageY = window.pageYOffset;
} else {
pageY = document.documentElement.scrollTop;
}
return pageY;
"""
)
self.assertGreater(y_offset, 300)
def test_should_scroll_to_click_on_an_element_hidden_by_overflow(self):
test_html = self.marionette.absolute_url("click_out_of_bounds_overflow.html")
self.marionette.navigate(test_html)
link = self.marionette.find_element(By.ID, "link")
try:
link.click()
except MoveTargetOutOfBoundsException:
self.fail("Should not be out of bounds")
def test_should_not_scroll_elements_if_click_point_is_in_view(self):
test_html = self.marionette.absolute_url("element_outside_viewport.html")
for s in ["top", "right", "bottom", "left"]:
for p in ["50", "30"]:
self.marionette.navigate(test_html)
scroll = self.marionette.execute_script(
"return [window.scrollX, window.scrollY];"
)
self.marionette.find_element(By.ID, "{0}-{1}".format(s, p)).click()
self.assertEqual(
scroll,
self.marionette.execute_script(
"return [window.scrollX, window.scrollY];"
),
)
def test_do_not_scroll_again_if_element_is_already_in_view(self):
self.marionette.navigate(
inline(
"""