|
17 | 17 |
|
18 | 18 | """Tests for advanced user interactions."""
|
19 | 19 | import pytest
|
| 20 | +from selenium.common.exceptions import MoveTargetOutOfBoundsException |
20 | 21 |
|
21 | 22 | from selenium.webdriver.common.by import By
|
22 | 23 | from selenium.webdriver.common.keys import Keys
|
@@ -260,6 +261,93 @@ def test_can_pause(driver, pages):
|
260 | 261 | assert "Clicked" == toDoubleClick.get_attribute('value')
|
261 | 262 |
|
262 | 263 |
|
| 264 | +@pytest.mark.xfail_firefox |
| 265 | +@pytest.mark.xfail_remote |
| 266 | +def test_can_scroll_to_element(driver, pages): |
| 267 | + pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html") |
| 268 | + iframe = driver.find_element(By.TAG_NAME, "iframe") |
| 269 | + |
| 270 | + assert not _in_viewport(driver, iframe) |
| 271 | + |
| 272 | + ActionChains(driver).scroll(0, 0, 0, 0, origin=iframe).perform() |
| 273 | + |
| 274 | + assert _in_viewport(driver, iframe) |
| 275 | + |
| 276 | + |
| 277 | +@pytest.mark.xfail_firefox |
| 278 | +@pytest.mark.xfail_remote |
| 279 | +def test_can_scroll_from_element_by_amount(driver, pages): |
| 280 | + pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html") |
| 281 | + iframe = driver.find_element(By.TAG_NAME, "iframe") |
| 282 | + |
| 283 | + ActionChains(driver).scroll(0, 0, 0, 200, origin=iframe).pause(0.2).perform() |
| 284 | + |
| 285 | + driver.switch_to.frame(iframe) |
| 286 | + checkbox = driver.find_element(By.NAME, "scroll_checkbox") |
| 287 | + |
| 288 | + assert _in_viewport(driver, checkbox) |
| 289 | + |
| 290 | + |
| 291 | +@pytest.mark.xfail_firefox |
| 292 | +@pytest.mark.xfail_remote |
| 293 | +def test_can_scroll_from_element_with_offset_by_amount(driver, pages): |
| 294 | + pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html") |
| 295 | + footer = driver.find_element(By.TAG_NAME, "footer") |
| 296 | + |
| 297 | + ActionChains(driver).scroll(0, -50, 0, 200, origin=footer).pause(0.2).perform() |
| 298 | + |
| 299 | + iframe = driver.find_element(By.TAG_NAME, "iframe") |
| 300 | + driver.switch_to.frame(iframe) |
| 301 | + checkbox = driver.find_element(By.NAME, "scroll_checkbox") |
| 302 | + |
| 303 | + assert _in_viewport(driver, checkbox) |
| 304 | + |
| 305 | + |
| 306 | +@pytest.mark.xfail_firefox |
| 307 | +@pytest.mark.xfail_remote |
| 308 | +def test_errors_when_element_offset_not_in_viewport(driver, pages): |
| 309 | + pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html") |
| 310 | + footer = driver.find_element(By.TAG_NAME, "footer") |
| 311 | + |
| 312 | + with pytest.raises(MoveTargetOutOfBoundsException): |
| 313 | + ActionChains(driver).scroll(0, 50, 0, 200, origin=footer).perform() |
| 314 | + |
| 315 | + |
| 316 | +@pytest.mark.xfail_firefox |
| 317 | +@pytest.mark.xfail_remote |
| 318 | +def test_can_scroll_from_viewport_by_amount(driver, pages): |
| 319 | + pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html") |
| 320 | + footer = driver.find_element(By.TAG_NAME, "footer") |
| 321 | + y = footer.rect['y'] |
| 322 | + |
| 323 | + ActionChains(driver).scroll(0, 0, 0, y, origin="viewport").pause(0.2).perform() |
| 324 | + |
| 325 | + assert _in_viewport(driver, footer) |
| 326 | + |
| 327 | + |
| 328 | +@pytest.mark.xfail_firefox |
| 329 | +@pytest.mark.xfail_remote |
| 330 | +def test_can_scroll_from_viewport_with_offset_by_amount(driver, pages): |
| 331 | + pages.load("scrolling_tests/frame_with_nested_scrolling_frame.html") |
| 332 | + iframe = driver.find_element(By.TAG_NAME, "iframe") |
| 333 | + |
| 334 | + ActionChains(driver).scroll(10, 10, 0, 200, origin="viewport").pause(0.2).perform() |
| 335 | + |
| 336 | + driver.switch_to.frame(iframe) |
| 337 | + checkbox = driver.find_element(By.NAME, "scroll_checkbox") |
| 338 | + |
| 339 | + assert _in_viewport(driver, checkbox) |
| 340 | + |
| 341 | + |
| 342 | +@pytest.mark.xfail_firefox |
| 343 | +@pytest.mark.xfail_remote |
| 344 | +def test_errors_when_origin_offset_not_in_viewport(driver, pages): |
| 345 | + pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html") |
| 346 | + |
| 347 | + with pytest.raises(MoveTargetOutOfBoundsException): |
| 348 | + ActionChains(driver).scroll(-10, -10, 0, 200, origin="viewport").perform() |
| 349 | + |
| 350 | + |
263 | 351 | @pytest.mark.xfail_firefox
|
264 | 352 | @pytest.mark.xfail_remote
|
265 | 353 | def test_can_scroll_mouse_wheel(driver, pages):
|
@@ -294,3 +382,13 @@ def _get_events(driver):
|
294 | 382 | if "code" in e and e["code"] == "Unidentified":
|
295 | 383 | e["code"] = ""
|
296 | 384 | return events
|
| 385 | + |
| 386 | + |
| 387 | +def _in_viewport(driver, element): |
| 388 | + script = ( |
| 389 | + "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" |
| 390 | + "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" |
| 391 | + "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" |
| 392 | + "window.pageYOffset&&t+o>window.pageXOffset" |
| 393 | + ) |
| 394 | + return driver.execute_script(script, element) |
0 commit comments