Example usage for org.openqa.selenium WebElement getLocation

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

Introduction

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

Prototype

Point getLocation();

Source Link

Document

Where on the page is the top left-hand corner of the rendered element?

Usage

From source file:com.mycompany.fullslidertest.FullsliderTest.java

public void addSlideAndReorder() {
    createPresentation("Add slide and reorder it");

    js.executeScript("$('#addslidebtn').click()");
    js.executeScript("$('#addtextbtn').click()");
    WebElement draggableText = driver/*from   www  .  java 2  s . com*/
            .findElement(By.xpath("//div[@id='workspace']/div[2]/div/section[2]/div[3]"));
    waitForAction();
    actions.dragAndDropBy(draggableText, 10, 200).build().perform();
    draggableText.click();

    WebElement draggableSlide1 = driver
            .findElement(By.xpath("//div[@id='vertical-toolbar']/div/div/div/div[1]"));
    WebElement draggableSlide2 = driver
            .findElement(By.xpath("//div[@id='vertical-toolbar']/div/div/div/div[2]"));

    int offset = draggableSlide2.getLocation().getY() - draggableSlide1.getLocation().getY();
    Actions builder = new Actions(driver);
    Actions dragAndDrop = builder.moveToElement(draggableSlide1).clickAndHold().moveByOffset(0, offset + 10)
            .pause(500); // I found the pauses were necessary to get the test working in other
    dragAndDrop.release().perform();

    waitForAction();
    driver.quit();
}

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

public static Integer calculateHorizontalSpacingBetweenTwoElements(WebElement webElement1,
        WebElement webElement2) {/*w ww .  java  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   w  ww .  j a va  2s .c o  m*/
@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  w  ww .ja  v a  2  s  .  c  o m
    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 . java 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.
 *///www. j  a  v a  2  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.
 *//* ww  w . j  a va 2 s. co  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.issues.cdf.CDF486.java

License:Apache License

/**
 * ############################### Test Case 1 ###############################
 *
 * Test Case Name://  ww  w . j a va 2s .co  m
 *    Assert dashboardType=clean has desired effect.
 *
 * Description:
 *    When adding dashboardType=clean to dashboard's url it renders without any css associated.
 *
 * Steps:
 *    1. Open CDF blueprint sample and assert position of specific elements
 *    2. Open same sample adding clean option to URL and assert position of elements is different
 */
@Test
public void tc1_CdfDashboardType_CleanStyle() {
    this.log.info("tc1_CdfDashboardType_CleanStyle");

    /*
     * ## Step 1
     */
    //Go to New CDE Dashboard
    driver.get(baseUrl
            + "api/repos/%3Apublic%3Aplugin-samples%3Apentaho-cdf%3A20-samples%3Ablueprint%3Ablueprint.xcdf/generatedContent");

    // Wait for loading disappear
    this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay"));
    WebElement element = this.elemHelper.WaitForElementPresenceAndVisible(driver,
            By.xpath("//div[@class='panelTitle']/div[@class='container']/div[2]"));
    assertNotNull(element);
    int imagex = element.getLocation().x;
    int imagey = element.getLocation().y;
    element = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("pieChart_object"));
    assertNotNull(element);
    element.getLocation();
    element.getLocation();
    element = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("barChart_object"));
    assertNotNull(element);
    int barChartx = element.getLocation().x;
    int barCharty = element.getLocation().y;
    element = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("dialChart_object"));
    assertNotNull(element);
    int dialChartx = element.getLocation().x;
    int dialCharty = element.getLocation().y;

    /*
     * ## Step 2
     */
    //Go to New CDE Dashboard
    driver.get(baseUrl
            + "api/repos/%3Apublic%3Aplugin-samples%3Apentaho-cdf%3A20-samples%3Ablueprint%3Ablueprint.xcdf/generatedContent?dashboardType=clean");

    // Wait for loading disappear
    this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay"));
    element = this.elemHelper.WaitForElementPresenceAndVisible(driver,
            By.xpath("//div[@class='panelTitle']/div[@class='container']/div[2]"));
    assertNotNull(element);
    int imagex1 = element.getLocation().x;
    int imagey1 = element.getLocation().y;
    element = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("pieChart_object"));
    assertNotNull(element);
    element.getLocation();
    element.getLocation();
    element = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("barChart_object"));
    assertNotNull(element);
    int barChartx1 = element.getLocation().x;
    int barCharty1 = element.getLocation().y;
    element = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("dialChart_object"));
    assertNotNull(element);
    int dialChartx1 = element.getLocation().x;
    int dialCharty1 = element.getLocation().y;
    assertTrue(imagex != imagex1);
    assertTrue(imagey != imagey1);
    assertTrue(barChartx != barChartx1);
    assertTrue(barCharty != barCharty1);
    assertTrue(dialChartx != dialChartx1);
    assertTrue(dialCharty != dialCharty1);
}

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  2  s.  c  om
 *
 * @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;
}