Path: blob/trunk/java/test/org/openqa/selenium/PositionAndSizeTest.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.testing.drivers.Browser.CHROME;21import static org.openqa.selenium.testing.drivers.Browser.EDGE;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.interactions.Locatable;28import org.openqa.selenium.testing.Ignore;29import org.openqa.selenium.testing.JupiterTestBase;30import org.openqa.selenium.testing.NotYetImplemented;31import org.openqa.selenium.testing.SwitchToTopAfterTest;3233class PositionAndSizeTest extends JupiterTestBase {3435@Test36void testShouldBeAbleToDetermineTheLocationOfAnElement() {37driver.get(pages.xhtmlTestPage);3839WebElement element = driver.findElement(By.id("username"));40Point location = element.getLocation();4142assertThat(location.getX()).isPositive();43assertThat(location.getY()).isPositive();44}4546@Test47void testShouldGetCoordinatesOfAnElement() {48driver.get(appServer.whereIs("coordinates_tests/simple_page.html"));49assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));50assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));51}5253@Test54void testShouldGetCoordinatesOfAnEmptyElement() {55driver.get(appServer.whereIs("coordinates_tests/page_with_empty_element.html"));56assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));57assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));58}5960@Test61void testShouldGetCoordinatesOfATransparentElement() {62driver.get(appServer.whereIs("coordinates_tests/page_with_transparent_element.html"));63assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));64assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));65}6667@Test68@NotYetImplemented(SAFARI)69public void testShouldGetCoordinatesOfAHiddenElement() {70driver.get(appServer.whereIs("coordinates_tests/page_with_hidden_element.html"));71assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(10, 10));72assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));73}7475@Test76@NotYetImplemented(SAFARI)77public void testShouldGetCoordinatesOfAnInvisibleElement() {78driver.get(appServer.whereIs("coordinates_tests/page_with_invisible_element.html"));79assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(0, 0));80assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(0, 0));81}8283@Test84void testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort() {85driver.get(appServer.whereIs("coordinates_tests/page_with_element_out_of_view.html"));86int windowHeight = driver.manage().window().getSize().getHeight();87Point location = getLocationInViewPort(By.id("box"));88assertThat(location.getX()).isEqualTo(10);89assertThat(location.getY()).isGreaterThanOrEqualTo(0);90assertThat(location.getY()).isLessThanOrEqualTo(windowHeight - 100);91assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 5010));92}9394@SwitchToTopAfterTest95@Test96@NotYetImplemented(SAFARI)97public void testShouldGetCoordinatesOfAnElementInAFrame() {98driver.get(appServer.whereIs("coordinates_tests/element_in_frame.html"));99driver.switchTo().frame("ifr");100WebElement box = driver.findElement(By.id("box"));101assertThat(box.getLocation()).isEqualTo(new Point(10, 10));102assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));103}104105@SwitchToTopAfterTest106@Test107@NotYetImplemented(SAFARI)108@NotYetImplemented(FIREFOX)109@NotYetImplemented(IE)110@NotYetImplemented(CHROME)111@NotYetImplemented(EDGE)112public void testShouldGetCoordinatesInViewPortOfAnElementInAFrame() {113driver.get(appServer.whereIs("coordinates_tests/element_in_frame.html"));114driver.switchTo().frame("ifr");115assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(25, 25));116assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));117}118119@SwitchToTopAfterTest120@Test121@NotYetImplemented(SAFARI)122@NotYetImplemented(FIREFOX)123@NotYetImplemented(IE)124@NotYetImplemented(CHROME)125@NotYetImplemented(EDGE)126public void testShouldGetCoordinatesInViewPortOfAnElementInANestedFrame() {127driver.get(appServer.whereIs("coordinates_tests/element_in_nested_frame.html"));128driver.switchTo().frame("ifr");129driver.switchTo().frame("ifr");130assertThat(getLocationInViewPort(By.id("box"))).isEqualTo(new Point(40, 40));131assertThat(getLocationOnPage(By.id("box"))).isEqualTo(new Point(10, 10));132}133134@Test135void testShouldGetCoordinatesOfAnElementWithFixedPosition() {136driver.get(appServer.whereIs("coordinates_tests/page_with_fixed_element.html"));137assertThat(getLocationInViewPort(By.id("fixed")).getY()).isZero();138assertThat(getLocationOnPage(By.id("fixed")).getY()).isZero();139140driver.findElement(By.id("bottom")).click();141assertThat(getLocationInViewPort(By.id("fixed")).getY()).isZero();142assertThat(getLocationOnPage(By.id("fixed")).getY()).isPositive();143}144145@Test146void testShouldCorrectlyIdentifyThatAnElementHasWidthAndHeight() {147driver.get(pages.xhtmlTestPage);148149WebElement shrinko = driver.findElement(By.id("linkId"));150Dimension size = shrinko.getSize();151assertThat(size.width).isPositive();152assertThat(size.height).isPositive();153}154155// TODO: This test's value seems dubious at best. The CSS spec does not define how browsers156// should handle sub-pixel rendering, and every browser seems to be different anyhow:157// http://ejohn.org/blog/sub-pixel-problems-in-css/158@Test159@Ignore(IE)160@NotYetImplemented(value = CHROME, reason = "WebKit bug 28804")161@NotYetImplemented(value = EDGE, reason = "WebKit bug 28804")162@NotYetImplemented(SAFARI)163@Ignore(FIREFOX)164public void testShouldHandleNonIntegerPositionAndSize() {165driver.get(pages.rectanglesPage);166167WebElement r2 = driver.findElement(By.id("r2"));168String left = r2.getCssValue("left");169assertThat(left).startsWith("10.9");170String top = r2.getCssValue("top");171assertThat(top).startsWith("10.1");172assertThat(r2.getLocation()).isEqualTo(new Point(11, 10));173String width = r2.getCssValue("width");174assertThat(width).startsWith("48.6");175String height = r2.getCssValue("height");176assertThat(height).startsWith("49.3");177assertThat(r2.getSize()).isEqualTo(new Dimension(49, 49));178}179180private Point getLocationInViewPort(By locator) {181WebElement element = driver.findElement(locator);182return ((Locatable) element).getCoordinates().inViewPort();183}184185private Point getLocationOnPage(By locator) {186WebElement element = driver.findElement(locator);187return ((Locatable) element).getCoordinates().onPage();188}189}190191192