Path: blob/trunk/java/test/org/openqa/selenium/ClickScrollingTest.java
1865 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;21import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;22import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;23import static org.openqa.selenium.testing.drivers.Browser.IE;24import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2526import org.junit.jupiter.api.Test;27import org.openqa.selenium.testing.Ignore;28import org.openqa.selenium.testing.JupiterTestBase;29import org.openqa.selenium.testing.NotYetImplemented;30import org.openqa.selenium.testing.SwitchToTopAfterTest;3132class ClickScrollingTest extends JupiterTestBase {3334@Test35void testClickingOnAnchorScrollsPage() {36String scrollScript = "";37scrollScript += "var pageY;";38scrollScript += "if (typeof(window.pageYOffset) == 'number') {";39scrollScript += " pageY = window.pageYOffset;";40scrollScript += "} else {";41scrollScript += " pageY = document.documentElement.scrollTop;";42scrollScript += "}";43scrollScript += "return pageY;";4445driver.get(pages.macbethPage);4647driver.findElement(By.partialLinkText("last speech")).click();4849Object x = ((JavascriptExecutor) driver).executeScript(scrollScript);50// Focusing on to click, but not actually following,51// the link will scroll it in to view, which is a few pixels further than 052// According to documentation at53// https://developer.mozilla.org/en-US/docs/Web/API/Window/pageYOffset54// window.pageYOffset may not return integer value.55// With the following changes in below we are checking the type of returned object and assigning56// respectively57// the value of 'yOffset'58if (x instanceof Long) {59long yOffset = (Long) x;60assertThat(yOffset).describedAs("Did not scroll").isGreaterThan(300L);61} else if (x instanceof Double) {62double yOffset = (Double) x;63assertThat(yOffset).describedAs("Did not scroll").isGreaterThan(300.0);64}65}6667@Test68void testShouldScrollToClickOnAnElementHiddenByOverflow() {69String url = appServer.whereIs("click_out_of_bounds_overflow.html");70driver.get(url);7172WebElement link = driver.findElement(By.id("link"));73link.click();74}7576@Test77void testShouldBeAbleToClickOnAnElementHiddenByOverflow() {78driver.get(appServer.whereIs("scroll.html"));7980WebElement link = driver.findElement(By.id("line8"));81// This used to throw a MoveTargetOutOfBoundsException - we don't expect it to82link.click();83assertThat(driver.findElement(By.id("clicked")).getText()).isEqualTo("line8");84}8586@Test87@Ignore(88value = FIREFOX,89reason = "horizontal scroll bar gets in the way",90issue = "https://github.com/mozilla/geckodriver/issues/2013")91public void testShouldBeAbleToClickOnAnElementHiddenByDoubleOverflow() {92driver.get(appServer.whereIs("scrolling_tests/page_with_double_overflow_auto.html"));9394driver.findElement(By.id("link")).click();95wait.until(titleIs("Clicked Successfully!"));96}9798@Test99void testShouldBeAbleToClickOnAnElementHiddenByYOverflow() {100driver.get(appServer.whereIs("scrolling_tests/page_with_y_overflow_auto.html"));101102driver.findElement(By.id("link")).click();103wait.until(titleIs("Clicked Successfully!"));104}105106@Test107@Ignore(value = IE, issue = "716")108@Ignore(109value = FIREFOX,110reason = "horizontal scroll bar gets in the way",111issue = "https://github.com/mozilla/geckodriver/issues/2013")112public void testShouldBeAbleToClickOnAnElementPartiallyHiddenByOverflow() {113driver.get(appServer.whereIs("scrolling_tests/page_with_partially_hidden_element.html"));114115driver.findElement(By.id("btn")).click();116wait.until(titleIs("Clicked Successfully!"));117}118119@Test120void testShouldNotScrollOverflowElementsWhichAreVisible() {121driver.get(appServer.whereIs("scroll2.html"));122WebElement list = driver.findElement(By.tagName("ul"));123WebElement item = list.findElement(By.id("desired"));124item.click();125long yOffset =126(Long) ((JavascriptExecutor) driver).executeScript("return arguments[0].scrollTop;", list);127assertThat(yOffset).describedAs("Should not have scrolled").isZero();128}129130@Test131@Ignore(132value = FIREFOX,133reason = "horizontal scroll bar gets in the way",134issue = "https://github.com/mozilla/geckodriver/issues/2013")135@NotYetImplemented(IE)136public void testShouldNotScrollIfAlreadyScrolledAndElementIsInView() {137driver.get(appServer.whereIs("scroll3.html"));138driver.findElement(By.id("button2")).click();139long scrollTop = getScrollTop();140driver.findElement(By.id("button1")).click();141assertThat(getScrollTop()).isEqualTo(scrollTop);142}143144@Test145void testShouldBeAbleToClickRadioButtonScrolledIntoView() {146driver.get(appServer.whereIs("scroll4.html"));147driver.findElement(By.id("radio")).click();148// If we don't throw, we're good149}150151@Test152@Ignore(value = IE, reason = "IE has special overflow handling")153@NotYetImplemented(SAFARI)154public void testShouldScrollOverflowElementsIfClickPointIsOutOfViewButElementIsInView() {155driver.get(appServer.whereIs("scroll5.html"));156driver.findElement(By.id("inner")).click();157assertThat(driver.findElement(By.id("clicked")).getText()).isEqualTo("clicked");158}159160@SwitchToTopAfterTest161@Test162@NotYetImplemented(SAFARI)163@Ignore(164value = FIREFOX,165reason = "frame not scrolled into view",166issue = "https://bugzilla.mozilla.org/show_bug.cgi?id=1314462")167public void testShouldBeAbleToClickElementInAFrameThatIsOutOfView() {168driver.get(appServer.whereIs("scrolling_tests/page_with_frame_out_of_view.html"));169driver.switchTo().frame("frame");170WebElement element = driver.findElement(By.name("checkbox"));171element.click();172assertThat(element.isSelected()).isTrue();173}174175@SwitchToTopAfterTest176@Test177void testShouldBeAbleToClickElementThatIsOutOfViewInAFrame() {178driver.get(appServer.whereIs("scrolling_tests/page_with_scrolling_frame.html"));179driver.switchTo().frame("scrolling_frame");180WebElement element = driver.findElement(By.name("scroll_checkbox"));181element.click();182assertThat(element.isSelected()).isTrue();183}184185@SwitchToTopAfterTest186@Test187@NotYetImplemented(SAFARI)188public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrameThatIsOutOfView() {189driver.get(appServer.whereIs("scrolling_tests/page_with_scrolling_frame_out_of_view.html"));190driver.switchTo().frame("scrolling_frame");191WebElement element = driver.findElement(By.name("scroll_checkbox"));192element.click();193assertThat(element.isSelected()).isTrue();194}195196@SwitchToTopAfterTest197@Test198@Ignore(199value = FIREFOX,200reason = "horizontal scroll bar gets in the way",201issue = "https://github.com/mozilla/geckodriver/issues/2013")202public void testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrame() {203driver.get(appServer.whereIs("scrolling_tests/page_with_nested_scrolling_frames.html"));204driver.switchTo().frame("scrolling_frame");205driver.switchTo().frame("nested_scrolling_frame");206WebElement element = driver.findElement(By.name("scroll_checkbox"));207element.click();208assertThat(element.isSelected()).isTrue();209}210211@SwitchToTopAfterTest212@Test213@Ignore(214value = FIREFOX,215reason = "horizontal scroll bar gets in the way",216issue = "https://github.com/mozilla/geckodriver/issues/2013")217@NotYetImplemented(SAFARI)218public void testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrameThatIsOutOfView() {219driver.get(220appServer.whereIs("scrolling_tests/page_with_nested_scrolling_frames_out_of_view.html"));221driver.switchTo().frame("scrolling_frame");222driver.switchTo().frame("nested_scrolling_frame");223WebElement element = driver.findElement(By.name("scroll_checkbox"));224element.click();225226assertThat(element.isSelected()).isTrue();227}228229@Test230void testShouldNotScrollWhenGettingElementSize() {231driver.get(appServer.whereIs("scroll3.html"));232long scrollTop = getScrollTop();233driver.findElement(By.id("button1")).getSize();234assertThat(getScrollTop()).isEqualTo(scrollTop);235}236237private long getScrollTop() {238wait.until(presenceOfElementLocated(By.tagName("body")));239return (Long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollTop;");240}241242@SwitchToTopAfterTest243@Test244@NotYetImplemented(SAFARI)245@Ignore(246value = FIREFOX,247reason = "frame not scrolled into view",248issue = "https://bugzilla.mozilla.org/show_bug.cgi?id=1314462")249public void testShouldBeAbleToClickElementInATallFrame() {250driver.get(appServer.whereIs("scrolling_tests/page_with_tall_frame.html"));251driver.switchTo().frame("tall_frame");252WebElement element = driver.findElement(By.name("checkbox"));253element.click();254assertThat(element.isSelected()).isTrue();255}256}257258259