Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/click_scrolling_tests.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import pytest
19
20
from selenium.common.exceptions import MoveTargetOutOfBoundsException
21
from selenium.webdriver.common.by import By
22
from selenium.webdriver.support import expected_conditions as EC
23
from selenium.webdriver.support.ui import WebDriverWait
24
25
26
def test_clicking_on_anchor_scrolls_page(driver, pages):
27
scrollScript = """var pageY;
28
if (typeof(window.pageYOffset) == 'number') {
29
pageY = window.pageYOffset;
30
} else {
31
pageY = document.documentElement.scrollTop;
32
}
33
return pageY;"""
34
35
pages.load("macbeth.html")
36
37
driver.find_element(By.PARTIAL_LINK_TEXT, "last speech").click()
38
39
yOffset = driver.execute_script(scrollScript)
40
41
# Focusing on to click, but not actually following,
42
# the link will scroll it in to view, which is a few pixels further than 0
43
assert yOffset > 300
44
45
46
def test_should_scroll_to_click_on_an_element_hidden_by_overflow(driver, pages):
47
pages.load("click_out_of_bounds_overflow.html")
48
49
link = driver.find_element(By.ID, "link")
50
try:
51
link.click()
52
except MoveTargetOutOfBoundsException as e:
53
AssertionError("Should not be out of bounds: %s" % e.msg)
54
55
56
def test_should_be_able_to_click_on_an_element_hidden_by_overflow(driver, pages):
57
pages.load("scroll.html")
58
59
link = driver.find_element(By.ID, "line8")
60
# This used to throw a MoveTargetOutOfBoundsException - we don't expect it to
61
link.click()
62
assert "line8" == driver.find_element(By.ID, "clicked").text
63
64
65
@pytest.mark.xfail_firefox
66
@pytest.mark.xfail_remote
67
def test_should_be_able_to_click_on_an_element_hidden_by_double_overflow(driver, pages):
68
pages.load("scrolling_tests/page_with_double_overflow_auto.html")
69
70
driver.find_element(By.ID, "link").click()
71
WebDriverWait(driver, 3).until(EC.title_is("Clicked Successfully!"))
72
73
74
def test_should_be_able_to_click_on_an_element_hidden_by_yoverflow(driver, pages):
75
pages.load("scrolling_tests/page_with_y_overflow_auto.html")
76
77
driver.find_element(By.ID, "link").click()
78
WebDriverWait(driver, 3).until(EC.title_is("Clicked Successfully!"))
79
80
81
def test_should_not_scroll_overflow_elements_which_are_visible(driver, pages):
82
pages.load("scroll2.html")
83
list = driver.find_element(By.TAG_NAME, "ul")
84
item = list.find_element(By.ID, "desired")
85
item.click()
86
yOffset = driver.execute_script("return arguments[0].scrollTop", list)
87
assert 0 == yOffset, "Should not have scrolled"
88
89
90
@pytest.mark.xfail_firefox
91
@pytest.mark.xfail_remote
92
def test_should_not_scroll_if_already_scrolled_and_element_is_in_view(driver, pages):
93
pages.load("scroll3.html")
94
driver.find_element(By.ID, "button2").click()
95
scrollTop = get_scroll_top(driver)
96
driver.find_element(By.ID, "button1").click()
97
assert scrollTop == get_scroll_top(driver)
98
99
100
def test_should_be_able_to_click_radio_button_scrolled_into_view(driver, pages):
101
pages.load("scroll4.html")
102
driver.find_element(By.ID, "radio").click()
103
# If we don't throw, we're good
104
105
106
@pytest.mark.xfail_safari
107
def test_should_scroll_overflow_elements_if_click_point_is_out_of_view_but_element_is_in_view(driver, pages):
108
pages.load("scroll5.html")
109
driver.find_element(By.ID, "inner").click()
110
assert "clicked" == driver.find_element(By.ID, "clicked").text
111
112
113
@pytest.mark.xfail_firefox(reason="https://github.com/w3c/webdriver/issues/408")
114
@pytest.mark.xfail_remote(reason="https://github.com/w3c/webdriver/issues/408")
115
@pytest.mark.xfail_safari
116
def test_should_be_able_to_click_element_in_aframe_that_is_out_of_view(driver, pages):
117
pages.load("scrolling_tests/page_with_frame_out_of_view.html")
118
driver.switch_to.frame(driver.find_element(By.NAME, "frame"))
119
element = driver.find_element(By.NAME, "checkbox")
120
element.click()
121
assert element.is_selected()
122
123
124
def test_should_be_able_to_click_element_that_is_out_of_view_in_aframe(driver, pages):
125
pages.load("scrolling_tests/page_with_scrolling_frame.html")
126
driver.switch_to.frame(driver.find_element(By.NAME, "scrolling_frame"))
127
element = driver.find_element(By.NAME, "scroll_checkbox")
128
element.click()
129
assert element.is_selected()
130
131
132
def test_should_not_be_able_to_click_element_that_is_out_of_view_in_anon_scrollable_frame(driver, pages):
133
pages.load("scrolling_tests/page_with_non_scrolling_frame.html")
134
driver.switch_to.frame("scrolling_frame")
135
element = driver.find_element(By.NAME, "scroll_checkbox")
136
element.click()
137
# TODO we should assert that the click was unsuccessful
138
139
140
@pytest.mark.xfail_safari
141
def test_should_be_able_to_click_element_that_is_out_of_view_in_aframe_that_is_out_of_view(driver, pages):
142
pages.load("scrolling_tests/page_with_scrolling_frame_out_of_view.html")
143
driver.switch_to.frame(driver.find_element(By.NAME, "scrolling_frame"))
144
element = driver.find_element(By.NAME, "scroll_checkbox")
145
element.click()
146
assert element.is_selected()
147
148
149
@pytest.mark.xfail_firefox
150
@pytest.mark.xfail_chrome
151
@pytest.mark.xfail_remote
152
def test_should_be_able_to_click_element_that_is_out_of_view_in_anested_frame(driver, pages):
153
pages.load("scrolling_tests/page_with_nested_scrolling_frames.html")
154
driver.switch_to.frame(driver.find_element(By.NAME, "scrolling_frame"))
155
driver.switch_to.frame(driver.find_element(By.NAME, "nested_scrolling_frame"))
156
element = driver.find_element(By.NAME, "scroll_checkbox")
157
element.click()
158
assert element.is_selected()
159
160
161
@pytest.mark.xfail_firefox
162
@pytest.mark.xfail_safari
163
@pytest.mark.xfail_chrome
164
@pytest.mark.xfail_remote
165
def test_should_be_able_to_click_element_that_is_out_of_view_in_anested_frame_that_is_out_of_view(driver, pages):
166
pages.load("scrolling_tests/page_with_nested_scrolling_frames_out_of_view.html")
167
driver.switch_to.frame(driver.find_element(By.NAME, "scrolling_frame"))
168
driver.switch_to.frame(driver.find_element(By.NAME, "nested_scrolling_frame"))
169
element = driver.find_element(By.NAME, "scroll_checkbox")
170
element.click()
171
assert element.is_selected()
172
173
174
def test_should_not_scroll_when_getting_element_size(driver, pages):
175
pages.load("scroll3.html")
176
scrollTop = get_scroll_top(driver)
177
driver.find_element(By.ID, "button1").size
178
assert scrollTop == get_scroll_top(driver)
179
180
181
def get_scroll_top(driver):
182
return driver.execute_script("return document.body.scrollTop")
183
184