Example usage for org.openqa.selenium WebElement getSize

List of usage examples for org.openqa.selenium WebElement getSize

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getSize.

Prototype

Dimension getSize();

Source Link

Document

What is the width and height of the rendered element?

Usage

From source file:com.mengge.AppiumDriver.java

License:Apache License

/**
 * Convenience method for "zooming in" on an element on the screen.
 * "zooming in" refers to the action of two appendages pressing the screen and sliding
 * away from each other.// ww  w  .  java2 s.  com
 * NOTE:
 * This convenience method slides touches away from the element, if this would happen
 * to place one of them off the screen, appium will return an outOfBounds error.
 * In this case, revert to using the MultiTouchAction api instead of this method.
 *
 * @param el The element to pinch.
 */
public void zoom(WebElement el) {
    MultiTouchAction multiTouch = new MultiTouchAction(this);

    Dimension dimensions = el.getSize();
    Point upperLeft = el.getLocation();
    Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2,
            upperLeft.getY() + dimensions.getHeight() / 2);
    int yOffset = center.getY() - upperLeft.getY();

    TouchAction action0 = new TouchAction(this).press(center.getX(), center.getY())
            .moveTo(el, center.getX(), center.getY() - yOffset).release();
    TouchAction action1 = new TouchAction(this).press(center.getX(), center.getY())
            .moveTo(el, center.getX(), center.getY() + yOffset).release();

    multiTouch.add(action0).add(action1);

    multiTouch.perform();
}

From source file:com.mycompany.browserManipulationUtilities.ScreenShotUtility.java

/**
 * Captures the image of the element/*from   w w  w. j  a  va 2  s  .  c o  m*/
 * @param browser
 * @param element
 * @return
 * @throws IOException 
 */
public static BufferedImage getScreenShotOfElement(WebDriver browser, WebElement element) throws IOException {
    BufferedImage fullScreenCapture = takeScreenShot(browser);
    //get location and dimensions of target element
    Point topLeft = element.getLocation();
    Dimension elementDims = element.getSize();

    //crop out the desired part of the image
    BufferedImage elementImage = fullScreenCapture.getSubimage(topLeft.getX(), topLeft.getY(),
            elementDims.getWidth(), elementDims.getHeight());
    return elementImage;
}

From source file:com.mycompany.layoutTests.HorizontalLayoutTester.java

public static Integer calculateHorizontalSpacingBetweenTwoElements(WebElement webElement1,
        WebElement webElement2) {//from   w w w .  j  av  a  2 s . co  m
    Integer horizontalSpacing = null;
    WebElement leftWebElement = webElement1;
    WebElement rightWebElement = webElement2;
    if (rightWebElement.getLocation().getX() < leftWebElement.getLocation().getX()) {
        leftWebElement = webElement2;
        rightWebElement = webElement1;
    }
    int rightEdgeOfLeftElement = leftWebElement.getLocation().getX() + leftWebElement.getSize().getWidth();
    int leftEdgeOfRightElement = rightWebElement.getLocation().getX();
    horizontalSpacing = leftEdgeOfRightElement - rightEdgeOfLeftElement;

    return horizontalSpacing;
}

From source file:com.mycompany.layoutTests.HorizontalLayoutTesterTest.java

/**
 * Test of calculateHorizontalSpacingBetweenTwoElements method, of class HorizontalLayoutTester.
 *//*from www.ja v  a  2 s  .  c om*/
@Test
public void testCalculateHorizontalSpacingBetweenTwoElements_overlapCase() {
    System.out.println("calculateHorizontalSpacingBetweenTwoElements");

    Point webElem1Orig = new Point(10, 10);
    Dimension webElem1Dim = new Dimension(30, 30);
    Point webElem2Orig = new Point(10, 100);
    Dimension webElem2Dim = new Dimension(30, 30);

    WebElement webElement1 = Mockito.mock(WebElement.class);
    Mockito.when(webElement1.getLocation()).thenReturn(webElem1Orig);
    Mockito.when(webElement1.getSize()).thenReturn(webElem1Dim);

    WebElement webElement2 = Mockito.mock(WebElement.class);
    Mockito.when(webElement2.getLocation()).thenReturn(webElem2Orig);
    Mockito.when(webElement2.getSize()).thenReturn(webElem2Dim);

    Integer expResult = -30;
    Integer result = HorizontalLayoutTester.calculateHorizontalSpacingBetweenTwoElements(webElement1,
            webElement2);
    assertEquals(expResult, result);
}

From source file:com.mycompany.layoutTests.HorizontalLayoutTesterTest.java

@Test
public void testCalculateHorizontalSpacingBetweenTwoElements_nonOverlapCase() {
    System.out.println("calculateHorizontalSpacingBetweenTwoElements");

    Point webElem1Orig = new Point(10, 10);
    Dimension webElem1Dim = new Dimension(30, 30);
    Point webElem2Orig = new Point(100, 100);
    Dimension webElem2Dim = new Dimension(30, 30);

    WebElement webElement1 = Mockito.mock(WebElement.class);
    Mockito.when(webElement1.getLocation()).thenReturn(webElem1Orig);
    Mockito.when(webElement1.getSize()).thenReturn(webElem1Dim);

    WebElement webElement2 = Mockito.mock(WebElement.class);
    Mockito.when(webElement2.getLocation()).thenReturn(webElem2Orig);
    Mockito.when(webElement2.getSize()).thenReturn(webElem2Dim);

    Integer expResult = 60;/*from   ww  w .  ja va  2 s.  c  om*/
    Integer result = HorizontalLayoutTester.calculateHorizontalSpacingBetweenTwoElements(webElement1,
            webElement2);
    assertEquals(expResult, result);
}

From source file:com.mycompany.layoutTests.VerticalLayoutTester.java

public static int calculateVerticalSpacingBetweenElements(WebElement webElement1, WebElement webElement2) {
    WebElement upperWebElement = webElement1;
    WebElement lowerWebElement = webElement2;
    if (lowerWebElement.getLocation().getY() < upperWebElement.getLocation().getY()) {
        upperWebElement = webElement2;/*from  w  ww  .j a v a 2  s. c  om*/
        lowerWebElement = webElement1;
    }
    int bottomOfTopElement = upperWebElement.getLocation().getY() + upperWebElement.getSize().getHeight();
    int topOfBottomElement = lowerWebElement.getLocation().getY();
    int verticalSpacingBetweenElements = topOfBottomElement - bottomOfTopElement;
    return verticalSpacingBetweenElements;
}

From source file:com.mycompany.layoutTests.VerticalLayoutTesterTest.java

/**
 * Test of testVerticalSpacingBetweenTwoElements method, of class VerticalLayoutTester.
 *//* w  w  w  . j av  a2 s. c o  m*/
@Test
public void testTestVerticalSpacingBetweenTwoElements() {
    System.out.println("testVerticalSpacingBetweenTwoElements");

    Point webElem1Orig = new Point(10, 10);
    Dimension webElem1Dim = new Dimension(30, 30);
    Point webElem2Orig = new Point(10, 100);
    Dimension webElem2Dim = new Dimension(30, 30);

    WebElement webElement1 = Mockito.mock(WebElement.class);
    Mockito.when(webElement1.getLocation()).thenReturn(webElem1Orig);
    Mockito.when(webElement1.getSize()).thenReturn(webElem1Dim);

    WebElement webElement2 = Mockito.mock(WebElement.class);
    Mockito.when(webElement2.getLocation()).thenReturn(webElem2Orig);
    Mockito.when(webElement2.getSize()).thenReturn(webElem2Dim);

    int minVerticalPixelSpacing = 50;
    int maxVerticalPixelSpacing = 70;
    boolean expResult = true;
    boolean result = VerticalLayoutTester.testVerticalSpacingBetweenTwoElements(webElement1, webElement2,
            minVerticalPixelSpacing, maxVerticalPixelSpacing);
    assertEquals(expResult, result);
}

From source file:com.mycompany.layoutTests.VerticalLayoutTesterTest.java

/**
 * Test of calculateVerticalSpacingBetweenElements method, of class VerticalLayoutTester.
 */// w w  w.  ja va  2 s.  c o  m
@Test
public void testCalculateVerticalSpacingBetweenElements() {
    System.out.println("calculateVerticalSpacingBetweenElements");

    Point webElem1Orig = new Point(10, 10);
    Dimension webElem1Dim = new Dimension(30, 30);
    Point webElem2Orig = new Point(10, 100);
    Dimension webElem2Dim = new Dimension(30, 30);

    WebElement webElement1 = Mockito.mock(WebElement.class);
    Mockito.when(webElement1.getLocation()).thenReturn(webElem1Orig);
    Mockito.when(webElement1.getSize()).thenReturn(webElem1Dim);

    WebElement webElement2 = Mockito.mock(WebElement.class);
    Mockito.when(webElement2.getLocation()).thenReturn(webElem2Orig);
    Mockito.when(webElement2.getSize()).thenReturn(webElem2Dim);

    int expResult = 60;
    int result = VerticalLayoutTester.calculateVerticalSpacingBetweenElements(webElement1, webElement2);
    assertEquals(expResult, result);
}

From source file:com.nabla.project.fronter.selenium.tests.helper.WebElementExtender.java

License:Open Source License

public static File captureElementBitmap(final WebElement element) throws IOException {
    final WrapsDriver wrapsDriver = (WrapsDriver) element;

    final File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);

    final BufferedImage img = ImageIO.read(screen);

    final int width = element.getSize().getWidth();
    final int height = element.getSize().getHeight();

    final Rectangle rect = new Rectangle(width, height);

    final Point p = element.getLocation();

    final BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);

    ImageIO.write(dest, "png", screen);
    return screen;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method intends to check if two elements overlap or are contained inside each other, returns true if
 * elements don't overlap./*from w  ww .ja v  a  2s.co m*/
 *
 * @param driver
 * @param element1
 * @param element2
 * @return
 */
public boolean ElementsNotOverlap(WebDriver driver, By element1, By element2) {
    this.log.debug("ElementsNotOverlap::Enter");
    this.log.debug("Locator1: " + element1.toString());
    this.log.debug("Locator2: " + element2.toString());

    WebElement elem1 = FindElement(driver, element1);
    WebElement elem2 = FindElement(driver, element2);

    // get borders of first element
    Point firstLocation = elem1.getLocation();
    Dimension firstDimension = elem1.getSize();
    int firstLeft = firstLocation.getX();
    int firstTop = firstLocation.getY();
    int firstRight = firstLeft + firstDimension.getWidth();
    int firstBottom = firstTop + firstDimension.getHeight();
    // get borders of second element
    Point secondLocation = elem2.getLocation();
    Dimension secondDimension = elem2.getSize();
    int secondLeft = secondLocation.getX();
    int secondTop = secondLocation.getY();
    int secondRight = secondLeft + secondDimension.getWidth();
    int secondBottom = secondTop + secondDimension.getHeight();
    this.log.debug(firstTop + " " + firstBottom + " " + firstLeft + " " + firstRight);
    this.log.debug(secondTop + " " + secondBottom + " " + secondLeft + " " + secondRight);
    // if firstElement is either to the left, the right, above or below the second return true
    boolean notIntersected = firstBottom < secondTop || firstTop > secondBottom || firstLeft > secondRight
            || firstRight < secondLeft;

    this.log.debug("ElementsNotOverlap::Exit");
    return notIntersected;
}